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

ddtrace/tracer: Add B3 flag to PropagatorConfig #1148

Merged
merged 3 commits into from Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions ddtrace/tracer/textmap.go
Expand Up @@ -110,11 +110,14 @@ type PropagatorConfig struct {
ParentHeader string

// PriorityHeader specifies the map key that will be used to store the sampling priority.
// It deafults to DefaultPriorityHeader.
// It defaults to DefaultPriorityHeader.
PriorityHeader string

// MaxTagsHeaderLen specifies the maximum length of trace tags header value.
MaxTagsHeaderLen int

// B3 specifies if B3 headers should be added for trace propagation
ajgajg1134 marked this conversation as resolved.
Show resolved Hide resolved
B3 bool
}

// NewPropagator returns a new propagator which uses TextMap to inject
Expand Down Expand Up @@ -151,28 +154,39 @@ type chainedPropagator struct {
}

// getPropagators returns a list of propagators based on the list found in the
// given environment variable. If the list doesn't contain a value or has invalid
// values, the default propagator will be returned.
// given environment variable. If the list doesn't contain any valid values the
// default propagator will be returned. Any invalid values in the list will log
// a warning and be ignored.
func getPropagators(cfg *PropagatorConfig, env string) []Propagator {
dd := &propagator{cfg}
ps := os.Getenv(env)
defaultPs := []Propagator{dd}
if cfg.B3 {
defaultPs = append(defaultPs, &propagatorB3{})
}
if ps == "" {
return []Propagator{dd}
return defaultPs
}
var list []Propagator
if cfg.B3 {
list = append(list, &propagatorB3{})
}
for _, v := range strings.Split(ps, ",") {
switch strings.ToLower(v) {
case "datadog":
list = append(list, dd)
case "b3":
list = append(list, &propagatorB3{})
if !cfg.B3 {
// propagatorB3 hasn't already been added, add a new one.
list = append(list, &propagatorB3{})
}
default:
log.Warn("unrecognized propagator: %s\n", v)
}
}
if len(list) == 0 {
// return the default
return []Propagator{dd}
return defaultPs
}
return list
}
Expand Down
52 changes: 52 additions & 0 deletions ddtrace/tracer/textmap_test.go
Expand Up @@ -458,6 +458,58 @@ func TestB3(t *testing.T) {
assert.True(ok)
assert.Equal(2, p)
})

t.Run("config", func(t *testing.T) {
os.Setenv("DD_PROPAGATION_STYLE_INJECT", "datadog")
defer os.Unsetenv("DD_PROPAGATION_STYLE_INJECT")

var tests = []struct {
in []uint64
out map[string]string
}{
{
[]uint64{1412508178991881, 1842642739201064},
map[string]string{
b3TraceIDHeader: "000504ab30404b09",
b3SpanIDHeader: "00068bdfb1eb0428",
},
},
{
[]uint64{9530669991610245, 9455715668862222},
map[string]string{
b3TraceIDHeader: "0021dc1807524785",
b3SpanIDHeader: "002197ec5d8a250e",
},
},
{
[]uint64{1, 1},
map[string]string{
b3TraceIDHeader: "0000000000000001",
b3SpanIDHeader: "0000000000000001",
},
},
}

for _, test := range tests {
t.Run("", func(t *testing.T) {
tracer := newTracer(WithPropagator(NewPropagator(&PropagatorConfig{B3: true})))
root := tracer.StartSpan("web.request").(*span)
root.SetTag(ext.SamplingPriority, -1)
root.SetBaggageItem("item", "x")
ctx, ok := root.Context().(*spanContext)
ctx.traceID = test.in[0]
ctx.spanID = test.in[1]
headers := TextMapCarrier(map[string]string{})
err := tracer.Inject(ctx, headers)

assert := assert.New(t)
assert.True(ok)
assert.Nil(err)
assert.Equal(test.out[b3TraceIDHeader], headers[b3TraceIDHeader])
assert.Equal(test.out[b3SpanIDHeader], headers[b3SpanIDHeader])
})
}
})
}

func assertTraceTags(t *testing.T, expected, actual string) {
Expand Down