Skip to content

Commit

Permalink
fix global state pollution in a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Oct 23, 2024
1 parent 17d94f1 commit 535a60a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ddtrace/tracer/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func Test_slogHandler(t *testing.T) {
rl := &log.RecordLogger{}
defer log.UseLogger(rl)()

// Ensure the logger is set to the default level. This may not be the case
// when previous tests pollute the global state. We leave the logger in the
// state we found it to not contribute to this pollution ourselves.
oldLevel := log.GetLevel()
log.SetLevel(log.LevelWarn)
defer log.SetLevel(oldLevel)

// Log a few messages at different levels. The debug message gets discarded
// because the internal logger does not have debug enabled by default.
l := slog.New(slogHandler{})
Expand Down
2 changes: 0 additions & 2 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,6 @@ func BenchmarkGenSpanID(b *testing.B) {

// startTestTracer returns a Tracer with a DummyTransport
func startTestTracer(t testing.TB, opts ...StartOption) (trc *tracer, transport *dummyTransport, flush func(n int), stop func()) {
oldLevel := log.GetLevel()
transport = newDummyTransport()
tick := make(chan time.Time)
o := append([]StartOption{
Expand Down Expand Up @@ -2182,7 +2181,6 @@ func startTestTracer(t testing.TB, opts ...StartOption) (trc *tracer, transport
tracer.Stop()
// clear any service name that was set: we want the state to be the same as startup
globalconfig.SetServiceName("")
log.SetLevel(oldLevel) // avoids test failures because of global state
}
}

Expand Down

0 comments on commit 535a60a

Please sign in to comment.