requestid.go 2.8 KB

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