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

Fix: Support custom propagators in startup log #2925

Merged
merged 5 commits into from
Oct 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ddtrace/tracer/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,18 @@ func logStartup(t *tracer) {
featureFlags = append(featureFlags, f)
}

cp, _ := t.config.propagator.(*chainedPropagator)
var injectorNames, extractorNames string
switch v := t.config.propagator.(type) {
case *chainedPropagator:
injectorNames = v.injectorNames
extractorNames = v.extractorsNames
case nil:
injectorNames = ""
extractorNames = ""
default:
injectorNames = "custom"
extractorNames = "custom"
}

info := startupInfo{
Date: time.Now().Format(time.RFC3339),
Expand Down Expand Up @@ -127,8 +138,8 @@ func logStartup(t *tracer) {
PartialFlushMinSpans: t.config.partialFlushMinSpans,
Orchestrion: t.config.orchestrionCfg,
FeatureFlags: featureFlags,
PropagationStyleInject: cp.injectorNames,
PropagationStyleExtract: cp.extractorsNames,
PropagationStyleInject: injectorNames,
PropagationStyleExtract: extractorNames,
}
if _, _, err := samplingRulesFromEnv(); err != nil {
info.SamplingRulesError = fmt.Sprintf("%s", err)
Expand Down
72 changes: 72 additions & 0 deletions ddtrace/tracer/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
Expand Down Expand Up @@ -187,3 +188,74 @@ func TestLogFormat(t *testing.T) {
assert.Len(tp.Logs(), 1)
assert.Regexp(logPrefixRegexp+` DEBUG: Started Span: dd.trace_id="12345" dd.span_id="12345" dd.parent_id="0", Operation: test, Resource: /, Tags: map.*, map.*`, tp.Logs()[0])
}

func TestLogPropagators(t *testing.T) {
t.Run("default", func(t *testing.T) {
assert := assert.New(t)
substring := `"propagation_style_inject":"datadog,tracecontext","propagation_style_extract":"datadog,tracecontext"`
log := setup(t, nil)
assert.Regexp(substring, log)
})
t.Run("datadog,tracecontext", func(t *testing.T) {
assert := assert.New(t)
t.Setenv("DD_TRACE_PROPAGATION_STYLE", "datadog,tracecontext")
substring := `"propagation_style_inject":"datadog,tracecontext","propagation_style_extract":"datadog,tracecontext"`
log := setup(t, nil)
assert.Regexp(substring, log)
})
t.Run("b3multi", func(t *testing.T) {
assert := assert.New(t)
t.Setenv("DD_TRACE_PROPAGATION_STYLE", "b3multi")
substring := `"propagation_style_inject":"b3multi","propagation_style_extract":"b3multi"`
log := setup(t, nil)
assert.Regexp(substring, log)
})
t.Run("none", func(t *testing.T) {
assert := assert.New(t)
t.Setenv("DD_TRACE_PROPAGATION_STYLE", "none")
substring := `"propagation_style_inject":"","propagation_style_extract":""`
log := setup(t, nil)
assert.Regexp(substring, log)
})
t.Run("different-injector-extractor", func(t *testing.T) {
assert := assert.New(t)
t.Setenv("DD_TRACE_PROPAGATION_STYLE_INJECT", "b3multi")
t.Setenv("DD_TRACE_PROPAGATION_STYLE_EXTRACT", "tracecontext")
substring := `"propagation_style_inject":"b3multi","propagation_style_extract":"tracecontext"`
log := setup(t, nil)
assert.Regexp(substring, log)
})
t.Run("custom-propagator", func(t *testing.T) {
assert := assert.New(t)
substring := `"propagation_style_inject":"custom","propagation_style_extract":"custom"`
p := &prop{}
log := setup(t, p)
assert.Regexp(substring, log)
})
}

type prop struct{}

func (p *prop) Inject(context ddtrace.SpanContext, carrier interface{}) (e error) {
return
}
func (p *prop) Extract(carrier interface{}) (sctx ddtrace.SpanContext, e error) {
return
}

func setup(t *testing.T, customProp Propagator) string {
tp := new(log.RecordLogger)
var tracer *tracer
var stop func()
if customProp != nil {
tracer, _, _, stop = startTestTracer(t, WithLogger(tp), WithPropagator(customProp))
} else {
tracer, _, _, stop = startTestTracer(t, WithLogger(tp))
}
defer stop()
tp.Reset()
tp.Ignore("appsec: ", telemetry.LogPrefix)
logStartup(tracer)
require.Len(t, tp.Logs(), 2)
return tp.Logs()[1]
}
Loading