From 98cc53480676890d4add3ed1ee3c8a824bfd4eb4 Mon Sep 17 00:00:00 2001 From: Trong Huu Nguyen Date: Tue, 28 Jan 2025 10:56:32 +0100 Subject: [PATCH] feat(middleware): use trace_id as correlation id, if available Co-authored-by: sindrerh2 --- pkg/middleware/correlationid.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/middleware/correlationid.go b/pkg/middleware/correlationid.go index c0126b3..ccfa95f 100644 --- a/pkg/middleware/correlationid.go +++ b/pkg/middleware/correlationid.go @@ -6,6 +6,7 @@ import ( chi_middleware "github.com/go-chi/chi/v5/middleware" "github.com/google/uuid" + "go.opentelemetry.io/otel/trace" ) func CorrelationIDHandler(next http.Handler) http.Handler { @@ -16,6 +17,11 @@ func CorrelationIDHandler(next http.Handler) http.Handler { } ctx := r.Context() + span := trace.SpanFromContext(ctx) + if span.SpanContext().HasTraceID() { + id = span.SpanContext().TraceID().String() + } + ctx = context.WithValue(ctx, chi_middleware.RequestIDKey, id) next.ServeHTTP(w, r.WithContext(ctx)) }