1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

tracing: add trace_id var (http.vars.trace_id placeholder) (#6308)

This commit is contained in:
Matthew Penner 2024-05-08 16:40:40 -06:00 committed by GitHub
parent dd203ad41f
commit 0b5720faa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -87,8 +87,12 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header)) ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header))
spanCtx := trace.SpanContextFromContext(ctx) spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.IsValid() { if spanCtx.IsValid() {
traceID := spanCtx.TraceID().String()
// Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
caddyhttp.SetVar(ctx, "trace_id", traceID)
// Add the trace id to the log fields for the request.
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok { if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
extra.Add(zap.String("traceID", spanCtx.TraceID().String())) extra.Add(zap.String("traceID", traceID))
} }
} }
next := ctx.Value(nextCallCtxKey).(*nextCall) next := ctx.Value(nextCallCtxKey).(*nextCall)