Skip to content

Commit 9e8f523

Browse files
dashpoleMrAlias
andauthored
when using WithNewRoot, don't use the parent context for sampling (open-telemetry#2032)
Co-authored-by: Tyler Yahn <[email protected]>
1 parent 62af6c7 commit 9e8f523

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2626

2727
### Fixed
2828

29+
- When using WithNewRoot, don't use the parent context for making sampling decisions. (#2032)
30+
2931
### Security
3032

3133
## [1.0.0-RC1] / 0.21.0 - 2021-06-18

sdk/trace/span.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ func startSpanInternal(ctx context.Context, tr *tracer, name string, o *trace.Sp
559559
// If told explicitly to make this a new root use a zero value SpanContext
560560
// as a parent which contains an invalid trace ID and is not remote.
561561
var psc trace.SpanContext
562-
if !o.NewRoot() {
562+
if o.NewRoot() {
563+
ctx = trace.ContextWithSpanContext(ctx, psc)
564+
} else {
563565
psc = trace.SpanContextFromContext(ctx)
564566
}
565567

sdk/trace/trace_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,30 @@ func TestStartSpanWithParent(t *testing.T) {
376376
}
377377
}
378378

379+
func TestStartSpanNewRootNotSampled(t *testing.T) {
380+
alwaysSampleTp := NewTracerProvider()
381+
sampledTr := alwaysSampleTp.Tracer("AlwaysSampled")
382+
neverSampleTp := NewTracerProvider(WithSampler(ParentBased(NeverSample())))
383+
neverSampledTr := neverSampleTp.Tracer("ParentBasedNeverSample")
384+
ctx := context.Background()
385+
386+
ctx, s1 := sampledTr.Start(trace.ContextWithRemoteSpanContext(ctx, sc), "span1-sampled")
387+
if err := checkChild(t, sc, s1); err != nil {
388+
t.Error(err)
389+
}
390+
391+
_, s2 := neverSampledTr.Start(ctx, "span2-no-newroot")
392+
if !s2.SpanContext().IsSampled() {
393+
t.Error(fmt.Errorf("got child span is not sampled, want child span with sampler: ParentBased(NeverSample()) to be sampled"))
394+
}
395+
396+
// Adding WithNewRoot causes child spans to not sample based on parent context
397+
_, s3 := neverSampledTr.Start(ctx, "span3-newroot", trace.WithNewRoot())
398+
if s3.SpanContext().IsSampled() {
399+
t.Error(fmt.Errorf("got child span is sampled, want child span WithNewRoot() and with sampler: ParentBased(NeverSample()) to not be sampled"))
400+
}
401+
}
402+
379403
func TestSetSpanAttributesOnStart(t *testing.T) {
380404
te := NewTestExporter()
381405
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))

0 commit comments

Comments
 (0)