From 19f45478bbb2efc8ac777cb05fc44b6273d579ff Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Fri, 11 Oct 2024 22:15:26 -0400 Subject: [PATCH 1/3] Fix logic and enable tests --- ddtrace/tracer/log.go | 17 +++++++-- ddtrace/tracer/log_test.go | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 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..52b4ebc441 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,73 @@ 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 + if customProp != nil { + tracer = newTracer(WithLogger(tp), WithPropagator(customProp)) + } else { + tracer = newTracer(WithLogger(tp)) + } + defer tracer.Stop() + tp.Reset() + tp.Ignore("appsec: ", telemetry.LogPrefix) + logStartup(tracer) + require.Len(t, tp.Logs(), 1) + return tp.Logs()[0] +} From bb1e79364d1ac749cf52334e630ba8033a21019f Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Tue, 15 Oct 2024 11:56:03 -0400 Subject: [PATCH 2/3] Fix TestLogPropagators setup to account for agent communicaition logs --- ddtrace/tracer/log_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ddtrace/tracer/log_test.go b/ddtrace/tracer/log_test.go index 52b4ebc441..0edb415d79 100644 --- a/ddtrace/tracer/log_test.go +++ b/ddtrace/tracer/log_test.go @@ -246,15 +246,18 @@ func (p *prop) Extract(carrier interface{}) (sctx ddtrace.SpanContext, e error) func setup(t *testing.T, customProp Propagator) string { tp := new(log.RecordLogger) var tracer *tracer + var stop func() if customProp != nil { - tracer = newTracer(WithLogger(tp), WithPropagator(customProp)) + tracer, _, _, stop = startTestTracer(t, WithLogger(tp), WithPropagator(customProp)) + // tracer = newTracer(WithLogger(tp), WithPropagator(customProp)) } else { - tracer = newTracer(WithLogger(tp)) + tracer, _, _, stop = startTestTracer(t, WithLogger(tp)) + // tracer = newTracer(WithLogger(tp)) } - defer tracer.Stop() + defer stop() tp.Reset() tp.Ignore("appsec: ", telemetry.LogPrefix) logStartup(tracer) - require.Len(t, tp.Logs(), 1) - return tp.Logs()[0] + require.Len(t, tp.Logs(), 2) + return tp.Logs()[1] } From e0e305c4bad30a121e83050504f1fdbdabd8d406 Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Fri, 18 Oct 2024 10:32:09 -0400 Subject: [PATCH 3/3] Remove superfluous comments --- ddtrace/tracer/log_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/ddtrace/tracer/log_test.go b/ddtrace/tracer/log_test.go index 0edb415d79..27d31eb089 100644 --- a/ddtrace/tracer/log_test.go +++ b/ddtrace/tracer/log_test.go @@ -249,10 +249,8 @@ func setup(t *testing.T, customProp Propagator) string { var stop func() if customProp != nil { tracer, _, _, stop = startTestTracer(t, WithLogger(tp), WithPropagator(customProp)) - // tracer = newTracer(WithLogger(tp), WithPropagator(customProp)) } else { tracer, _, _, stop = startTestTracer(t, WithLogger(tp)) - // tracer = newTracer(WithLogger(tp)) } defer stop() tp.Reset()