MCPcopy Index your code
hub / github.com/coder/coder / AttachRequestID

Function AttachRequestID

coderd/httpmw/requestid.go:37–51  ·  view source on GitHub ↗

AttachRequestID adds a request ID to each HTTP request.

(next http.Handler)

Source from the content-addressed store, hash-verified

35
36// AttachRequestID adds a request ID to each HTTP request.
37func AttachRequestID(next http.Handler) http.Handler {
38 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
39 rid := uuid.New()
40 ridString := rid.String()
41
42 ctx := context.WithValue(r.Context(), requestIDContextKey{}, rid)
43 ctx = slog.With(ctx, slog.F("request_id", rid))
44
45 trace.SpanFromContext(ctx).
46 SetAttributes(attribute.String("request_id", rid.String()))
47
48 rw.Header().Set("X-Coder-Request-Id", ridString)
49 next.ServeHTTP(rw, r.WithContext(ctx))
50 })
51}

Callers 1

Calls 7

WithContextMethod · 0.80
NewMethod · 0.65
ContextMethod · 0.65
SetMethod · 0.65
StringMethod · 0.45
HeaderMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 1