requestid.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package requestid
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/rs/xid"
  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 {
  57. requestID, err := randutil.UUIDv4()
  58. if err != nil {
  59. requestID = xid.New().String()
  60. }
  61. return requestID
  62. }
  63. type contextKey struct{}
  64. // NewContext returns a new context with the given request ID added to the
  65. // context.
  66. func NewContext(ctx context.Context, requestID string) context.Context {
  67. return context.WithValue(ctx, contextKey{}, requestID)
  68. }
  69. // FromContext returns the request ID from the context if it exists and
  70. // is not the empty value.
  71. func FromContext(ctx context.Context) (string, bool) {
  72. v, ok := ctx.Value(contextKey{}).(string)
  73. return v, ok && v != ""
  74. }