requestid.go 2.5 KB

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