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/textmap: Added none as a supported propagator for trace context extraction and injection #1610

Merged
merged 5 commits into from Dec 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions ddtrace/tracer/textmap.go
Expand Up @@ -177,6 +177,9 @@ func getPropagators(cfg *PropagatorConfig, env string) []Propagator {
if ps == "" {
return defaultPs
}
if ps == "none" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at like 197 here it seems like setting a value of none,b3 would result in a message "unrecognized propagator: none". Can we add a special case for none that instead warns the user something like Propagator "none" has no effect when combined with other propagators. To disable propagator set only "none" (Feel free to change the message). (And a quick little unit test update to make sure that future users don't accidentally break that message)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍 Updated

return nil
}
var list []Propagator
if cfg.B3 {
list = append(list, &propagatorB3{})
Expand All @@ -190,6 +193,9 @@ func getPropagators(cfg *PropagatorConfig, env string) []Propagator {
// propagatorB3 hasn't already been added, add a new one.
list = append(list, &propagatorB3{})
}
case "none":
log.Warn("Propagator \"none\" has no effect when combined with other propagators. "+
"To disable the propagator, set `%s=none`", env)
default:
log.Warn("unrecognized propagator: %s\n", v)
}
Expand Down
82 changes: 82 additions & 0 deletions ddtrace/tracer/textmap_test.go
Expand Up @@ -501,6 +501,7 @@ func testB3(t *testing.T, b3Header string) {
func TestB3(t *testing.T) {
testB3(t, "b3")
testB3(t, "b3multi")
testB3(t, "none,b3multi")

t.Run("config", func(t *testing.T) {
os.Setenv("DD_PROPAGATION_STYLE_INJECT", "datadog")
Expand Down Expand Up @@ -555,6 +556,87 @@ func TestB3(t *testing.T) {
})
}

func TestNonePropagator(t *testing.T) {
t.Run("inject/none", func(t *testing.T) {
t.Setenv("DD_PROPAGATION_STYLE_INJECT", "none")
tracer := newTracer()
root := tracer.StartSpan("web.request").(*span)
root.SetTag(ext.SamplingPriority, -1)
root.SetBaggageItem("item", "x")
ctx, ok := root.Context().(*spanContext)
ctx.traceID = 1
ctx.spanID = 1
headers := TextMapCarrier(map[string]string{})
err := tracer.Inject(ctx, headers)

assert := assert.New(t)
assert.True(ok)
assert.Nil(err)
assert.Len(headers, 0)
})

t.Run("inject/none,b3", func(t *testing.T) {
t.Setenv("DD_PROPAGATION_STYLE_INJECT", "none,b3")
tp := new(testLogger)
tracer := newTracer(WithLogger(tp))
// reinitializing to capture log output, since propagators are parsed before logger is set
tracer.config.propagator = NewPropagator(&PropagatorConfig{})
root := tracer.StartSpan("web.request").(*span)
root.SetTag(ext.SamplingPriority, -1)
root.SetBaggageItem("item", "x")
ctx, ok := root.Context().(*spanContext)
ctx.traceID = 1
ctx.spanID = 1
headers := TextMapCarrier(map[string]string{})
err := tracer.Inject(ctx, headers)

assert := assert.New(t)
assert.True(ok)
assert.Nil(err)
assert.Equal("0000000000000001", headers[b3TraceIDHeader])
assert.Equal("0000000000000001", headers[b3SpanIDHeader])
assert.Contains(tp.lines[0], "Propagator \"none\" has no effect when combined with other propagators. "+
"To disable the propagator, set `DD_PROPAGATION_STYLE_INJECT=none`")
})

t.Run("extract/none", func(t *testing.T) {
t.Setenv("DD_PROPAGATION_STYLE_EXTRACT", "none")
assert := assert.New(t)
tracer := newTracer()
root := tracer.StartSpan("web.request").(*span)
root.SetTag(ext.SamplingPriority, -1)
root.SetBaggageItem("item", "x")
headers := TextMapCarrier(map[string]string{})

_, err := tracer.Extract(headers)

assert.Equal(err, ErrSpanContextNotFound)
assert.Len(headers, 0)
})

t.Run("inject,extract/none", func(t *testing.T) {
t.Setenv("DD_PROPAGATION_STYLE_INJECT", "none")
t.Setenv("DD_PROPAGATION_STYLE_EXTRACT", "none")
tracer := newTracer()
root := tracer.StartSpan("web.request").(*span)
root.SetTag(ext.SamplingPriority, -1)
root.SetBaggageItem("item", "x")
ctx, ok := root.Context().(*spanContext)
ctx.traceID = 1
ctx.spanID = 1
headers := TextMapCarrier(map[string]string{})
err := tracer.Inject(ctx, headers)

assert := assert.New(t)
assert.True(ok)
assert.Nil(err)
assert.Len(headers, 0)

_, err = tracer.Extract(headers)
assert.Equal(err, ErrSpanContextNotFound)
})
}

func assertTraceTags(t *testing.T, expected, actual string) {
assert.ElementsMatch(t, strings.Split(expected, ","), strings.Split(actual, ","))
}