-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracing_handler.go
More file actions
64 lines (54 loc) · 1.81 KB
/
tracing_handler.go
File metadata and controls
64 lines (54 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package slogx
import (
"context"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"log/slog"
)
const TraceIDKey = "trace_id"
type TracingHandler struct {
handler slog.Handler
}
func NewTracingHandler(h slog.Handler) *TracingHandler {
// avoid chains of TracingHandlers.
if lh, ok := h.(*TracingHandler); ok {
h = lh.Handler()
}
return &TracingHandler{h}
}
// Enabled implements Handler.Enabled by reporting whether
// level is at least as large as h's level.
func (h *TracingHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.handler.Enabled(ctx, level)
}
// Handle implements Handler.Handle.
func (h *TracingHandler) Handle(ctx context.Context, r slog.Record) error {
span := trace.SpanFromContext(ctx)
if span.IsRecording() {
if r.Level >= slog.LevelError {
span.SetStatus(codes.Error, r.Message)
}
if spanCtx := span.SpanContext(); spanCtx.HasTraceID() {
// With() lost attrs bug has been fixed
// see https://github.com/golang/go/discussions/54763#discussioncomment-4504780
// and https://go.dev/cl/459615
r.AddAttrs(slog.String(TraceIDKey, spanCtx.TraceID().String()))
// do NOT using h.handler = h.handler.WithAttrs, will get duplicated trace_id
// h.handler = h.handler.WithAttrs([]slog.Attr{slog.String(TraceIDKey, traceID)})
}
}
return h.handler.Handle(ctx, r)
}
// WithAttrs implements Handler.WithAttrs.
func (h *TracingHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return NewTracingHandler(h.handler.WithAttrs(attrs))
}
// WithGroup implements Handler.WithGroup.
func (h *TracingHandler) WithGroup(name string) slog.Handler {
return NewTracingHandler(h.handler.WithGroup(name))
}
// Handler returns the Handler wrapped by h.
func (h *TracingHandler) Handler() slog.Handler {
return h.handler
}
var _ slog.Handler = (*TracingHandler)(nil)