Files
tenant-registry/internal/telemetry/slog.go
T
Sharang ParnerkarandClaude Opus 5.5 65ad1b52f6
ci / shared (pull_request) Failing after 12s
ci / test (pull_request) Failing after 15m24s
ci / image (pull_request) Skipped
feat(telemetry): traces to SigNoz for requests, outbound calls and SQL
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
2026-10-28 22:24:34 +01:00

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)}
}