diff options
author | Dave Henderson <dhenderson@gmail.com> | 2023-04-26 22:46:41 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-27 02:46:41 +0000 |
commit | f0e39817746903bb24948893348ae1cab67f1e46 (patch) | |
tree | 8cd863ea0dc3ede6b553eb298d214b29f8ef3bc6 /modules/caddyhttp/tracing | |
parent | 1c9ea0113d007d57bb8f793ebe7a64a3dcad7bc7 (diff) |
logging: Add traceID field to access logs when tracing is active (#5507)
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
Diffstat (limited to 'modules/caddyhttp/tracing')
-rw-r--r-- | modules/caddyhttp/tracing/tracer.go | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/modules/caddyhttp/tracing/tracer.go b/modules/caddyhttp/tracing/tracer.go index cb72743..c6dd7aa 100644 --- a/modules/caddyhttp/tracing/tracer.go +++ b/modules/caddyhttp/tracing/tracer.go @@ -14,6 +14,7 @@ import ( "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) @@ -81,8 +82,15 @@ func newOpenTelemetryWrapper( // serveHTTP injects a tracing context and call the next handler. func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request) { - ot.propagators.Inject(r.Context(), propagation.HeaderCarrier(r.Header)) - next := r.Context().Value(nextCallCtxKey).(*nextCall) + ctx := r.Context() + ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header)) + spanCtx := trace.SpanContextFromContext(ctx) + if spanCtx.IsValid() { + if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok { + extra.Add(zap.String("traceID", spanCtx.TraceID().String())) + } + } + next := ctx.Value(nextCallCtxKey).(*nextCall) next.err = next.next.ServeHTTP(w, r) } |