first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package tracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type OtelContextHandler struct {
|
||||
Handler slog.Handler
|
||||
}
|
||||
|
||||
func (h OtelContextHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
|
||||
sc := span.SpanContext()
|
||||
r.AddAttrs(
|
||||
slog.String("trace_id", sc.TraceID().String()),
|
||||
slog.String("span_id", sc.SpanID().String()),
|
||||
)
|
||||
}
|
||||
return h.Handler.Handle(ctx, r)
|
||||
}
|
||||
|
||||
func (h OtelContextHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
return h.Handler.Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h OtelContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return OtelContextHandler{Handler: h.Handler.WithAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h OtelContextHandler) WithGroup(name string) slog.Handler {
|
||||
return OtelContextHandler{Handler: h.Handler.WithGroup(name)}
|
||||
}
|
||||
Reference in New Issue
Block a user