-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrace.go
48 lines (37 loc) · 1.18 KB
/
trace.go
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
package clog
import (
"context"
"fmt"
"log/slog"
"go.opentelemetry.io/otel/trace"
"go.nownabe.dev/clog/internal/keys"
)
// WithTrace returns an Option that sets the trace attributes to the log record.
func WithTrace(projectID string) Option {
return optionFunc(func(h slog.Handler) slog.Handler {
return &traceHandler{h, projectID}
})
}
type traceHandler struct {
slog.Handler
projectID string
}
func (h *traceHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.Handler.Enabled(ctx, level)
}
func (h *traceHandler) Handle(ctx context.Context, r slog.Record) error {
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() {
r.AddAttrs(
slog.String(keys.Trace, fmt.Sprintf("projects/%s/traces/%s", h.projectID, spanCtx.TraceID().String())),
slog.String(keys.SpanID, spanCtx.SpanID().String()),
slog.Bool(keys.TraceSampled, spanCtx.IsSampled()),
)
}
return h.Handler.Handle(ctx, r)
}
func (h *traceHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &traceHandler{h.Handler.WithAttrs(attrs), h.projectID}
}
func (h *traceHandler) WithGroup(group string) slog.Handler {
return &traceHandler{h.Handler.WithGroup(group), h.projectID}
}