4
0

requestid.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Package requestid provides HTTP request ID functionality
  2. package requestid
  3. import (
  4. "context"
  5. "net/http"
  6. "go.step.sm/crypto/randutil"
  7. )
  8. const (
  9. // requestIDHeader is the header name used for propagating request IDs. If
  10. // available in an HTTP request, it'll be used instead of the X-Smallstep-Id
  11. // header. It'll always be used in response and set to the request ID.
  12. requestIDHeader = "X-Request-Id"
  13. // defaultTraceHeader is the default Smallstep tracing header that's currently
  14. // in use. It is used as a fallback to retrieve a request ID from, if the
  15. // "X-Request-Id" request header is not set.
  16. defaultTraceHeader = "X-Smallstep-Id"
  17. )
  18. type Handler struct {
  19. legacyTraceHeader string
  20. }
  21. // New creates a new request ID [handler]. It takes a trace header,
  22. // which is used keep the legacy behavior intact, which relies on the
  23. // X-Smallstep-Id header instead of X-Request-Id.
  24. func New(legacyTraceHeader string) *Handler {
  25. if legacyTraceHeader == "" {
  26. legacyTraceHeader = defaultTraceHeader
  27. }
  28. return &Handler{legacyTraceHeader: legacyTraceHeader}
  29. }
  30. // Middleware wraps an [http.Handler] with request ID extraction
  31. // from the X-Reqeust-Id header by default, or from the X-Smallstep-Id
  32. // header if not set. If both are not set, a new request ID is generated.
  33. // In all cases, the request ID is added to the request context, and
  34. // set to be reflected in the response.
  35. func (h *Handler) Middleware(next http.Handler) http.Handler {
  36. fn := func(w http.ResponseWriter, req *http.Request) {
  37. requestID := req.Header.Get(requestIDHeader)
  38. if requestID == "" {
  39. requestID = req.Header.Get(h.legacyTraceHeader)
  40. }
  41. if requestID == "" {
  42. requestID = newRequestID()
  43. req.Header.Set(h.legacyTraceHeader, requestID) // legacy behavior
  44. }
  45. // immediately set the request ID to be reflected in the response
  46. w.Header().Set(requestIDHeader, requestID)
  47. // continue down the handler chain
  48. ctx := NewContext(req.Context(), requestID)
  49. next.ServeHTTP(w, req.WithContext(ctx))
  50. }
  51. return http.HandlerFunc(fn)
  52. }
  53. // newRequestID generates a new random UUIDv4 request ID. If UUIDv4
  54. // generation fails, it'll fallback to generating a random ID using
  55. // github.com/rs/xid.
  56. func newRequestID() string { return randutil.UUIDv4() }
  57. type contextKey struct{}
  58. // NewContext returns a new context with the given request ID added to the
  59. // context.
  60. func NewContext(ctx context.Context, requestID string) context.Context {
  61. return context.WithValue(ctx, contextKey{}, requestID)
  62. }
  63. // FromContext returns the request ID from the context if it exists and
  64. // is not the empty value.
  65. func FromContext(ctx context.Context) (string, bool) {
  66. v, ok := ctx.Value(contextKey{}).(string)
  67. return v, ok && v != ""
  68. }