-
Notifications
You must be signed in to change notification settings - Fork 611
/
requestid.go
40 lines (31 loc) · 993 Bytes
/
requestid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package httpmw
import (
"context"
"net/http"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"cdr.dev/slog"
)
type requestIDContextKey struct{}
// RequestID returns the ID of the request.
func RequestID(r *http.Request) uuid.UUID {
rid, ok := r.Context().Value(requestIDContextKey{}).(uuid.UUID)
if !ok {
panic("developer error: request id middleware not provided")
}
return rid
}
// AttachRequestID adds a request ID to each HTTP request.
func AttachRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rid := uuid.New()
ridString := rid.String()
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))
})
}