internal/telemetry: OTLP/HTTP tracer provider from the OTEL_* env (already set on stage and prod; unset = off), W3C propagation, one server span per request named after the ServeMux pattern, client spans on the default transport (Keycloak, ERP, LiteLLM, OIDC) and a pgx QueryTracer. Client and DB spans only inside a request, so the background loops add no traces. No raw paths, query strings, bodies, SQL parameters or client addresses in spans; outbound spans carry the host only. slog lines logged with a request context get trace_id/span_id; the access log names the route instead of the raw path (slugs are names). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4
31 lines
911 B
Go
31 lines
911 B
Go
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// LogHandler adds trace_id and span_id to every record logged with a context
|
|
// that belongs to a trace (slog.InfoContext and friends), so SigNoz links the
|
|
// line to its trace.
|
|
type LogHandler struct{ slog.Handler }
|
|
|
|
func NewLogHandler(h slog.Handler) *LogHandler { return &LogHandler{Handler: h} }
|
|
|
|
func (h *LogHandler) Handle(ctx context.Context, r slog.Record) error {
|
|
if sc := trace.SpanContextFromContext(ctx); sc.IsValid() {
|
|
r.AddAttrs(slog.String("trace_id", sc.TraceID().String()), slog.String("span_id", sc.SpanID().String()))
|
|
}
|
|
return h.Handler.Handle(ctx, r)
|
|
}
|
|
|
|
func (h *LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return &LogHandler{Handler: h.Handler.WithAttrs(attrs)}
|
|
}
|
|
|
|
func (h *LogHandler) WithGroup(name string) slog.Handler {
|
|
return &LogHandler{Handler: h.Handler.WithGroup(name)}
|
|
}
|