From f65cab8c4d5a15c3c8d72d21bd8bc3d1be6bff87 Mon Sep 17 00:00:00 2001 From: Mikayla Toffler <46911781+mtoffl01@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:31:30 -0400 Subject: [PATCH] Fix: Support custom propagators in startup log (#2925) --- ddtrace/tracer/log.go | 17 +++++++-- ddtrace/tracer/log_test.go | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/ddtrace/tracer/log.go b/ddtrace/tracer/log.go index b1351edb34..b88d477c97 100644 --- a/ddtrace/tracer/log.go +++ b/ddtrace/tracer/log.go @@ -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), @@ -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) diff --git a/ddtrace/tracer/log_test.go b/ddtrace/tracer/log_test.go index f91047c8bd..27d31eb089 100644 --- a/ddtrace/tracer/log_test.go +++ b/ddtrace/tracer/log_test.go @@ -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" @@ -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] +}