1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-13 19:53:11 +02:00

when using WithNewRoot, don't use the parent context for sampling (#2032)

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
David Ashpole
2021-06-28 12:17:12 -04:00
committed by GitHub
parent 62af6c7077
commit 9e8f523d75
3 changed files with 29 additions and 1 deletions

View File

@@ -26,6 +26,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed ### Fixed
- When using WithNewRoot, don't use the parent context for making sampling decisions. (#2032)
### Security ### Security
## [1.0.0-RC1] / 0.21.0 - 2021-06-18 ## [1.0.0-RC1] / 0.21.0 - 2021-06-18

View File

@@ -559,7 +559,9 @@ func startSpanInternal(ctx context.Context, tr *tracer, name string, o *trace.Sp
// If told explicitly to make this a new root use a zero value SpanContext // If told explicitly to make this a new root use a zero value SpanContext
// as a parent which contains an invalid trace ID and is not remote. // as a parent which contains an invalid trace ID and is not remote.
var psc trace.SpanContext var psc trace.SpanContext
if !o.NewRoot() { if o.NewRoot() {
ctx = trace.ContextWithSpanContext(ctx, psc)
} else {
psc = trace.SpanContextFromContext(ctx) psc = trace.SpanContextFromContext(ctx)
} }

View File

@@ -376,6 +376,30 @@ func TestStartSpanWithParent(t *testing.T) {
} }
} }
func TestStartSpanNewRootNotSampled(t *testing.T) {
alwaysSampleTp := NewTracerProvider()
sampledTr := alwaysSampleTp.Tracer("AlwaysSampled")
neverSampleTp := NewTracerProvider(WithSampler(ParentBased(NeverSample())))
neverSampledTr := neverSampleTp.Tracer("ParentBasedNeverSample")
ctx := context.Background()
ctx, s1 := sampledTr.Start(trace.ContextWithRemoteSpanContext(ctx, sc), "span1-sampled")
if err := checkChild(t, sc, s1); err != nil {
t.Error(err)
}
_, s2 := neverSampledTr.Start(ctx, "span2-no-newroot")
if !s2.SpanContext().IsSampled() {
t.Error(fmt.Errorf("got child span is not sampled, want child span with sampler: ParentBased(NeverSample()) to be sampled"))
}
// Adding WithNewRoot causes child spans to not sample based on parent context
_, s3 := neverSampledTr.Start(ctx, "span3-newroot", trace.WithNewRoot())
if s3.SpanContext().IsSampled() {
t.Error(fmt.Errorf("got child span is sampled, want child span WithNewRoot() and with sampler: ParentBased(NeverSample()) to not be sampled"))
}
}
func TestSetSpanAttributesOnStart(t *testing.T) { func TestSetSpanAttributesOnStart(t *testing.T) {
te := NewTestExporter() te := NewTestExporter()
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty())) tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))