Skip to content

Commit 0b5720f

Browse files
authored
tracing: add trace_id var (http.vars.trace_id placeholder) (#6308)
1 parent dd203ad commit 0b5720f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

modules/caddyhttp/tracing/tracer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
8787
ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header))
8888
spanCtx := trace.SpanContextFromContext(ctx)
8989
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.
9094
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))
9296
}
9397
}
9498
next := ctx.Value(nextCallCtxKey).(*nextCall)

0 commit comments

Comments
 (0)