feat: add `request_id` to HTTP trace spans (#10145)

This commit is contained in:
Colin Adler 2023-10-09 14:05:10 -05:00 committed by GitHub
parent 2881b8b252
commit 3bbfcc593e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 0 deletions

View File

@ -5,6 +5,8 @@ import (
"net/http"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"cdr.dev/slog"
)
@ -29,6 +31,9 @@ func AttachRequestID(next http.Handler) http.Handler {
ctx := context.WithValue(r.Context(), requestIDContextKey{}, rid)
ctx = slog.With(ctx, slog.F("request_id", rid))
trace.SpanFromContext(ctx).
SetAttributes(attribute.String("request_id", rid.String()))
rw.Header().Set("X-Coder-Request-Id", ridString)
next.ServeHTTP(rw, r.WithContext(ctx))
})