Skip to content

Commit 1ef3efe

Browse files
committed
chore(contrib/log/slog): remove singleton dependency
Fix the test failure below by removing global state from the implementation. ``` $ go test -test.shuffle 1761195191840193457 ./contrib/log/slog/ --- FAIL: TestWrapHandler (0.03s) --- FAIL: TestWrapHandler/testLogger (0.03s) slog_test.go:103: {"time":"2025-10-23T07:14:27.195674+02:00","level":"INFO","msg":"this is an info log with tracing information","dd.trace_id":"2097471805985655682","dd.span_id":"2097471805985655682"} slog_test.go:103: Error Trace: /Users/felix.geisendoerfer/go/src/github.com/DataDog/dd-trace-go/contrib/log/slog/slog_test.go:42 /Users/felix.geisendoerfer/go/src/github.com/DataDog/dd-trace-go/contrib/log/slog/slog_test.go:103 /Users/felix.geisendoerfer/go/src/github.com/DataDog/dd-trace-go/contrib/log/slog/slog_test.go:149 Error: Not equal: expected: "68f9b9b3000000001d1bb77ea22c0b82" actual : "2097471805985655682" Diff: --- Expected +++ Actual @@ -1 +1 @@ -68f9b9b3000000001d1bb77ea22c0b82 +2097471805985655682 Test: TestWrapHandler/testLogger Messages: trace id not found slog_test.go:104: {"time":"2025-10-23T07:14:27.195681+02:00","level":"ERROR","msg":"this is an error log with tracing information","dd.trace_id":"2097471805985655682","dd.span_id":"2097471805985655682"} slog_test.go:104: Error Trace: /Users/felix.geisendoerfer/go/src/github.com/DataDog/dd-trace-go/contrib/log/slog/slog_test.go:42 /Users/felix.geisendoerfer/go/src/github.com/DataDog/dd-trace-go/contrib/log/slog/slog_test.go:104 /Users/felix.geisendoerfer/go/src/github.com/DataDog/dd-trace-go/contrib/log/slog/slog_test.go:149 Error: Not equal: expected: "68f9b9b3000000001d1bb77ea22c0b82" actual : "2097471805985655682" Diff: --- Expected +++ Actual @@ -1 +1 @@ -68f9b9b3000000001d1bb77ea22c0b82 +2097471805985655682 Test: TestWrapHandler/testLogger Messages: trace id not found slog_test.go:132: {"time":"2025-10-23T07:14:27.203777+02:00","level":"INFO","msg":"this is an info log with tracing information"} slog_test.go:133: {"time":"2025-10-23T07:14:27.20378+02:00","level":"ERROR","msg":"this is an error log with tracing information"} ```
1 parent 56a1413 commit 1ef3efe

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

contrib/log/slog/slog.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
1919
)
2020

21-
var cfg = newConfig()
22-
2321
func init() {
2422
_ = instrumentation.Load(instrumentation.PackageLogSlog)
2523
}
@@ -31,16 +29,6 @@ type group struct {
3129
attrs []slog.Attr
3230
}
3331

34-
type config struct {
35-
log128bits bool
36-
}
37-
38-
func newConfig() *config {
39-
return &config{
40-
log128bits: options.GetBoolEnv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", true),
41-
}
42-
}
43-
4432
// NewJSONHandler is a convenience function that returns a *slog.JSONHandler logger enhanced with
4533
// tracing information.
4634
func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
@@ -52,9 +40,18 @@ func WrapHandler(h slog.Handler) slog.Handler {
5240
return &handler{wrapped: h}
5341
}
5442

43+
type trilean uint8
44+
45+
const (
46+
trileanUnknown trilean = iota
47+
trileanTrue
48+
trileanFalse
49+
)
50+
5551
type handler struct {
56-
wrapped slog.Handler
57-
groups []group
52+
log128BitsState trilean
53+
wrapped slog.Handler
54+
groups []group
5855
}
5956

6057
// Enabled calls the wrapped handler Enabled method.
@@ -73,7 +70,7 @@ func (h *handler) Handle(ctx context.Context, rec slog.Record) error {
7370
span, ok := tracer.SpanFromContext(ctx)
7471
if ok && span.Context().TraceID() != tracer.TraceIDZero {
7572
var traceID string
76-
if cfg.log128bits {
73+
if h.log128Bits() {
7774
traceID = span.Context().TraceID()
7875
} else {
7976
traceID = strconv.FormatUint(span.Context().TraceIDLower(), 10)
@@ -101,8 +98,9 @@ func (h *handler) Handle(ctx context.Context, rec slog.Record) error {
10198
func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
10299
if len(h.groups) == 0 {
103100
return &handler{
104-
wrapped: h.wrapped.WithAttrs(attrs),
105-
groups: h.groups,
101+
wrapped: h.wrapped.WithAttrs(attrs),
102+
groups: h.groups,
103+
log128BitsState: h.log128BitsState,
106104
}
107105
}
108106
groups := append([]group{}, h.groups...)
@@ -111,17 +109,31 @@ func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
111109
groups[len(groups)-1] = curGroup
112110

113111
return &handler{
114-
wrapped: h.wrapped,
115-
groups: groups,
112+
wrapped: h.wrapped,
113+
groups: groups,
114+
log128BitsState: h.log128BitsState,
116115
}
117116
}
118117

119118
// WithGroup saves the provided group to be used later in the Handle method.
120119
func (h *handler) WithGroup(name string) slog.Handler {
121120
return &handler{
122-
wrapped: h.wrapped,
123-
groups: append(h.groups, group{name: name}),
121+
wrapped: h.wrapped,
122+
groups: append(h.groups, group{name: name}),
123+
log128BitsState: h.log128BitsState,
124+
}
125+
}
126+
127+
// log128Bits determines the value of the log128bits field.
128+
func (h *handler) log128Bits() bool {
129+
if h.log128BitsState == trileanUnknown {
130+
if options.GetBoolEnv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", true) {
131+
h.log128BitsState = trileanTrue
132+
} else {
133+
h.log128BitsState = trileanFalse
134+
}
124135
}
136+
return h.log128BitsState == trileanTrue
125137
}
126138

127139
// IsAlreadyWrapped checks whether the given handler is already wrapped by this package.

contrib/log/slog/slog_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ func testLogger(t *testing.T, createLogger func(b io.Writer) *slog.Logger, asser
8383
var traceID string
8484
spanID := strconv.FormatUint(span.Context().SpanID(), 10)
8585
if os.Getenv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED") == "false" {
86-
// Re-initialize to account for race condition between setting env var in the test and reading it in the contrib
87-
cfg = newConfig()
8886
traceID = strconv.FormatUint(span.Context().TraceIDLower(), 10)
8987
} else {
9088
traceID = span.Context().TraceID()

0 commit comments

Comments
 (0)