Skip to content

Commit

Permalink
tracer: honor tracecontext propagation style precedence (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
purple4reina authored Oct 11, 2023
1 parent 7895436 commit f72e6dd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ddtrace/tracer/textmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func getPropagators(cfg *PropagatorConfig, ps string) ([]Propagator, string) {
list = append(list, dd)
listNames = append(listNames, v)
case "tracecontext":
list = append([]Propagator{&propagatorW3c{}}, list...)
list = append(list, &propagatorW3c{})
listNames = append(listNames, v)
case "b3", "b3multi":
if !cfg.B3 {
Expand Down
63 changes: 63 additions & 0 deletions ddtrace/tracer/textmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,69 @@ func TestEnvVars(t *testing.T) {
}
}
})

t.Run("datadog extract precedence", func(t *testing.T) {
testEnvs = []map[string]string{
{headerPropagationStyleExtract: "datadog,tracecontext"},
{headerPropagationStyleExtract: "datadog,b3"},
{headerPropagationStyleExtract: "datadog,b3multi"},
}
for _, testEnv := range testEnvs {
for k, v := range testEnv {
t.Setenv(k, v)
}
var tests = []struct {
in TextMapCarrier
out []uint64 // contains [<span_id>, <sampling_decision>]
tid traceID
}{
{
in: TextMapCarrier{
DefaultTraceIDHeader: "1",
DefaultParentIDHeader: "1",
DefaultPriorityHeader: "1",
traceparentHeader: "00-00000000000000000000000000000002-0000000000000002-00",
b3SingleHeader: "3-3",
b3TraceIDHeader: "0000000000000004",
b3SpanIDHeader: "0000000000000004",
b3SampledHeader: "4",
},
out: []uint64{1, 1},
tid: traceIDFrom64Bits(1),
},
{
in: TextMapCarrier{
traceparentHeader: "00-00000000000000000000000000000001-0000000000000001-01",
b3SingleHeader: "1-1",
b3TraceIDHeader: "0000000000000001",
b3SpanIDHeader: "0000000000000001",
b3SampledHeader: "1",
},
out: []uint64{1, 1},
tid: traceIDFrom64Bits(1),
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("#%v extract with env=%q", i, testEnv), func(t *testing.T) {
tracer := newTracer(WithHTTPClient(c), withStatsdClient(&statsd.NoOpClient{}))
defer tracer.Stop()
assert := assert.New(t)
ctx, err := tracer.Extract(tc.in)
if err != nil {
t.Fatal(err)
}
sctx, ok := ctx.(*spanContext)
assert.True(ok)

assert.Equal(tc.tid, sctx.traceID)
assert.Equal(tc.out[0], sctx.spanID)
p, ok := sctx.samplingPriority()
assert.True(ok)
assert.Equal(int(tc.out[1]), p)
})
}
}
})
}

func checkSameElements(assert *assert.Assertions, want, got string) {
Expand Down

0 comments on commit f72e6dd

Please sign in to comment.