Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 13:15 UTC
tracing: add trace_id var (`http.vars.trace_id` placeholder) (#6308)
Matthew Penner committed
May 8, 2024 at 16:40 UTC
0b5720faa548269c865941c384956ba45e98f015
1 file changed
+5
-1
modules/caddyhttp/tracing/tracer.go
+5
-1
@@ -87,8 +87,12 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
87
ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header))
88
spanCtx := trace.SpanContextFromContext(ctx)
89
if spanCtx.IsValid() {
90
+ traceID := spanCtx.TraceID().String()
91
+ // Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
92
+ caddyhttp.SetVar(ctx, "trace_id", traceID)
93
+ // Add the trace id to the log fields for the request.
94
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
91
- extra.Add(zap.String("traceID", spanCtx.TraceID().String()))
95
+ extra.Add(zap.String("traceID", traceID))
96
}
97
}
98
next := ctx.Value(nextCallCtxKey).(*nextCall)