From 0ed4161299a599f244b20e5a28256cb1b2eceaf5 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 21 Mar 2024 13:43:26 -0600 Subject: [PATCH] modules(tracing): add `http.vars.trace_id` placeholder --- modules/caddyhttp/tracing/tracer.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/caddyhttp/tracing/tracer.go b/modules/caddyhttp/tracing/tracer.go index ecd415fa0cd..89c617bf4f0 100644 --- a/modules/caddyhttp/tracing/tracer.go +++ b/modules/caddyhttp/tracing/tracer.go @@ -87,8 +87,12 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header)) spanCtx := trace.SpanContextFromContext(ctx) 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 { - extra.Add(zap.String("traceID", spanCtx.TraceID().String())) + extra.Add(zap.String("traceID", traceID)) } } next := ctx.Value(nextCallCtxKey).(*nextCall)