Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using WithNewRoot, don't use the parent context for sampling #2032

Merged
merged 2 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- When using WithNewRoot, don't use the parent context for making sampling decisions. (#2032)

### Security

## [1.0.0-RC1] / 0.21.0 - 2021-06-18
Expand Down
4 changes: 3 additions & 1 deletion sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// as a parent which contains an invalid trace ID and is not remote.
var psc trace.SpanContext
if !o.NewRoot() {
if o.NewRoot() {
ctx = trace.ContextWithSpanContext(ctx, psc)
} else {
psc = trace.SpanContextFromContext(ctx)
}

Expand Down
24 changes: 24 additions & 0 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
te := NewTestExporter()
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))
Expand Down