You've already forked opentelemetry-go
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:
@@ -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
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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()))
|
||||
|
Reference in New Issue
Block a user