Skip to content
Draft
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
4 changes: 4 additions & 0 deletions ddtrace/opentelemetry/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ func (c *otelCtxToDDCtx) SpanID() uint64 {
}

func (c *otelCtxToDDCtx) ForeachBaggageItem(_ func(k, v string) bool) {}

func (c *otelCtxToDDCtx) IsSampled() bool {
return c.oc.IsSampled()
}
25 changes: 25 additions & 0 deletions ddtrace/opentelemetry/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,28 @@ func TestMergeOtelDDBaggage(t *testing.T) {
assert.Equal("otelValue", value)
})
}

func TestSamplingDecision(t *testing.T) {
assert := assert.New(t)
tp := NewTracerProvider(
tracer.WithSamplingRules([]tracer.SamplingRule{
{Rate: 0}, // This should be applied only when a brand new root span is started and should be ignored for a non-root span
}),
)
defer tp.Shutdown()
otel.SetTracerProvider(tp)
tr := otel.Tracer("")

parentSpanContext := oteltrace.NewSpanContext(oteltrace.SpanContextConfig{
TraceID: oteltrace.TraceID{0xAA},
SpanID: oteltrace.SpanID{0x01},
TraceFlags: oteltrace.FlagsSampled, // the parent span is sampled, so its child spans should be sampled too
})
ctx := oteltrace.ContextWithSpanContext(context.Background(), parentSpanContext)
_, span := tr.Start(ctx, "test")
span.End()

childSpanContext := span.SpanContext()
assert.Equal(parentSpanContext.TraceID(), childSpanContext.TraceID())
assert.True(childSpanContext.IsSampled(), "parent span is sampled, but child span is not sampled")
}
29 changes: 19 additions & 10 deletions ddtrace/tracer/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
Tags() map[string]string
}

type otelContextAdapter interface {
IsSampled() bool
}

// FromGenericCtx converts a ddtrace.SpanContext to a *SpanContext, which can be used
// to start child spans.
func FromGenericCtx(c ddtrace.SpanContext) *SpanContext {
Expand All @@ -138,16 +142,21 @@
sc.baggage[k] = v
return true
})
ctx, ok := c.(spanContextV1Adapter)
if !ok {
return &sc
}
sc.origin = ctx.Origin()
sc.trace = newTrace()
sc.trace.priority = ctx.Priority()
sc.trace.samplingDecision = samplingDecision(ctx.SamplingDecision())
sc.trace.tags = ctx.Tags()
sc.trace.propagatingTags = ctx.PropagatingTags()
switch ctx := c.(type) {
case spanContextV1Adapter:
sc.origin = ctx.Origin()
sc.trace = newTrace()
sc.trace.priority = ctx.Priority()
sc.trace.samplingDecision = samplingDecision(ctx.SamplingDecision())
sc.trace.tags = ctx.Tags()
sc.trace.propagatingTags = ctx.PropagatingTags()
case otelContextAdapter:
sc.trace = newTrace()
if ctx.IsSampled() {
sc.setSamplingPriority(1, samplernames.Default)
sc.trace.keep()
}
}
return &sc
}

Expand Down Expand Up @@ -252,7 +261,7 @@
if c == nil {
return
}
if atomic.LoadUint32(&c.hasBaggage) == 0 {

Check failure on line 264 in ddtrace/tracer/spancontext.go

View workflow job for this annotation

GitHub Actions / checklocks

unexpected call to atomic function
return
}
c.mu.RLock()
Expand Down
Loading