Skip to content

Commit

Permalink
Fix: Support custom propagators in startup log (#2925)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoffl01 authored and darccio committed Oct 23, 2024
1 parent a0e7ab6 commit e6907e0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
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]
}

0 comments on commit e6907e0

Please sign in to comment.