Skip to content

Commit

Permalink
tracer.startTestTracer: Call SetServiceName("") when stopping (#2260)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanj authored Oct 13, 2023
1 parent 3b5b794 commit b9c67e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,17 @@ func TestSpanSamplingPriority(t *testing.T) {
}

func TestSpanLog(t *testing.T) {
// this test is executed multiple times to ensure we clean up global state correctly
noServiceTest := func(t *testing.T) {
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t)
defer stop()
span := tracer.StartSpan("test.request").(*span)
expect := fmt.Sprintf(`dd.trace_id="%d" dd.span_id="%d" dd.parent_id="0"`, span.TraceID, span.SpanID)
assert.Equal(expect, fmt.Sprintf("%v", span))
}
t.Run("noservice_first", noServiceTest)

t.Run("default", func(t *testing.T) {
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"))
Expand Down Expand Up @@ -684,6 +695,9 @@ func TestSpanLog(t *testing.T) {
assert.Equal(expect, fmt.Sprintf("%v", span))
})

// run no_service again: it should have forgotten the global state
t.Run("no_service_after_full", noServiceTest)

t.Run("subservice", func(t *testing.T) {
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"), WithServiceVersion("1.2.3"), WithEnv("testenv"))
Expand Down Expand Up @@ -722,7 +736,8 @@ func TestSpanLog(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"), WithServiceVersion("1.2.3"), WithEnv("testenv"))
span := tracer.StartSpan("test.request").(*span)
stop()
expect := fmt.Sprintf(`dd.service=tracer.test dd.trace_id="%d" dd.span_id="%d" dd.parent_id="0"`, span.TraceID, span.SpanID)
// no service, env, or version after the tracer is stopped
expect := fmt.Sprintf(`dd.trace_id="%d" dd.span_id="%d" dd.parent_id="0"`, span.TraceID, span.SpanID)
assert.Equal(expect, fmt.Sprintf("%v", span))
})

Expand All @@ -737,7 +752,9 @@ func TestSpanLog(t *testing.T) {
tracer, _, _, stop := startTestTracer(t)
span := tracer.StartSpan("test.request").(*span)
stop()
expect := fmt.Sprintf(`dd.service=tracer.test dd.env=testenv dd.version=1.2.3 dd.trace_id="%d" dd.span_id="%d" dd.parent_id="0"`, span.TraceID, span.SpanID)
// service is not included: it is cleared when we stop the tracer
// env, version are included: it reads the environment variable when there is no tracer
expect := fmt.Sprintf(`dd.env=testenv dd.version=1.2.3 dd.trace_id="%d" dd.span_id="%d" dd.parent_id="0"`, span.TraceID, span.SpanID)
assert.Equal(expect, fmt.Sprintf("%v", span))
})

Expand Down
2 changes: 2 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,8 @@ func startTestTracer(t testing.TB, opts ...StartOption) (trc *tracer, transport
return tracer, transport, flushFunc, func() {
internal.SetGlobalTracer(&internal.NoopTracer{})
tracer.Stop()
// clear any service name that was set: we want the state to be the same as startup
globalconfig.SetServiceName("")
}
}

Expand Down

0 comments on commit b9c67e8

Please sign in to comment.