Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
131 lines (115 loc) · 4.45 KB
/
Copy pathcontext.go
File metadata and controls
131 lines (115 loc) · 4.45 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package log
import (
"context"
"log/slog"
"os"
"go.opentelemetry.io/otel/trace"
)
// traceIDKey and spanIDKey define the key values for log attributes, for tracing metadata
//
// https://opentelemetry.io/docs/specs/otel/compatibility/logging_trace_context/
const (
traceIDKey = "trace_id"
spanIDKey = "span_id"
)
const maxSpanContextAttrs = 2
// SpanContextHandler is a slog.Handler wrapper that adds trace data as log attributes on each
// Handle call, given that the input context to the method contains a valid trace.SpanContext.
type SpanContextHandler struct {
withSpanID bool
handler slog.Handler
}
//nolint:gochecknoglobals // instantiated once on a package level to be used in comparisons in NewSpanContextHandler
var nilHandler = slog.Handler(nil)
// NewSpanContextHandler creates a SpanContextHandler from the input slog.Handler and optional
// configuration.
func NewSpanContextHandler(handler slog.Handler, withSpanID bool) slog.Handler {
if handler == nil || handler == nilHandler {
handler = defaultHandler()
}
return &SpanContextHandler{
withSpanID: withSpanID,
handler: handler,
}
}
func defaultHandler() slog.Handler {
return slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
})
}
// Enabled reports whether the handler handles records at the given level.
// The handler ignores records whose level is lower.
// It is called early, before any arguments are processed,
// to save effort if the log event should be discarded.
// If called from a Logger method, the first argument is the context
// passed to that method, or context.Background() if nil was passed
// or the method does not take a context.
// The context is passed so Enabled can use its values
// to make a decision.
func (h *SpanContextHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.handler.Enabled(ctx, level)
}
// Handle handles the Record.
// It will only be called when Enabled returns true.
// The Context argument is as for Enabled.
// It is present solely to provide Handlers access to the context's values.
// Canceling the context should not affect record processing.
// (Among other things, log messages may be necessary to debug a
// cancellation-related problem.)
//
// Handle methods that produce output should observe the following rules:
// - If r.Time is the zero time, ignore the time.
// - If r.PC is zero, ignore it.
// - Attr's values should be resolved.
// - If an Attr's key and value are both the zero value, ignore the Attr.
// This can be tested with attr.Equal(Attr{}).
// - If a group's key is empty, inline the group's Attrs.
// - If a group has no Attrs (even if it has a non-empty key),
// ignore it.
//
//nolint:gocritic // this method implements the slog.Handler interface
func (h *SpanContextHandler) Handle(ctx context.Context, record slog.Record) error {
if sc := trace.SpanContextFromContext(ctx); sc.IsValid() {
attrs := make([]slog.Attr, 1, maxSpanContextAttrs)
attrs[0] = slog.String(traceIDKey, sc.TraceID().String())
if h.withSpanID {
attrs = append(attrs, slog.String(spanIDKey, sc.SpanID().String()))
}
record.AddAttrs(attrs...)
}
return h.handler.Handle(ctx, record)
}
// WithAttrs returns a new Handler whose attributes consist of
// both the receiver's attributes and the arguments.
// The Handler owns the slice: it may retain, modify or discard it.
func (h *SpanContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &SpanContextHandler{
withSpanID: h.withSpanID,
handler: h.handler.WithAttrs(attrs),
}
}
// WithGroup returns a new Handler with the given group appended to
// the receiver's existing groups.
// The keys of all subsequent attributes, whether added by With or in a
// Record, should be qualified by the sequence of group names.
//
// How this qualification happens is up to the Handler, so long as
// this Handler's attribute keys differ from those of another Handler
// with a different sequence of group names.
//
// A Handler should treat WithGroup as starting a Group of Attrs that ends
// at the end of the log event. That is,
//
// logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))
//
// should behave like
//
// logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))
//
// If the name is empty, WithGroup returns the receiver.
func (h *SpanContextHandler) WithGroup(name string) slog.Handler {
return &SpanContextHandler{
withSpanID: h.withSpanID,
handler: h.handler.WithGroup(name),
}
}