Skip to content

Commit

Permalink
ddtrace/tracer/textmap: Added none as a supported propagator for trac…
Browse files Browse the repository at this point in the history
…e context extraction and injection (#1610)
  • Loading branch information
dianashevchenko committed Dec 12, 2022
1 parent 4b12722 commit 2790627
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
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" {
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, ","))
}

0 comments on commit 2790627

Please sign in to comment.