feat(middleware): use trace_id as correlation id, if available

Co-authored-by: sindrerh2 <sindre.rodseth.hansen@nav.no>
This commit is contained in:
Trong Huu Nguyen
2025-01-28 10:56:32 +01:00
parent 1f730a3d68
commit 98cc534806

View File

@@ -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))
}