context.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package logging
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/rs/xid"
  6. )
  7. type key int
  8. const (
  9. // RequestIDKey is the context key that should store the request identifier.
  10. RequestIDKey key = iota
  11. // UserIDKey is the context key that should store the user identifier.
  12. UserIDKey
  13. )
  14. // NewRequestID creates a new request id using github.com/rs/xid.
  15. func NewRequestID() string {
  16. return xid.New().String()
  17. }
  18. // requestIDHeader is the header name used for propagating request IDs. If
  19. // available in an HTTP request, it'll be used instead of the X-Smallstep-Id
  20. // header. It'll always be used in response and set to the request ID.
  21. const requestIDHeader = "X-Request-Id"
  22. // RequestID returns a new middleware that obtains the current request ID
  23. // and sets it in the context. It first tries to read the request ID from
  24. // the "X-Request-Id" header. If that's not set, it tries to read it from
  25. // the provided header name. If the header does not exist or its value is
  26. // the empty string, it uses github.com/rs/xid to create a new one.
  27. func RequestID(headerName string) func(next http.Handler) http.Handler {
  28. if headerName == "" {
  29. headerName = defaultTraceIDHeader
  30. }
  31. return func(next http.Handler) http.Handler {
  32. fn := func(w http.ResponseWriter, req *http.Request) {
  33. requestID := req.Header.Get(requestIDHeader)
  34. if requestID == "" {
  35. requestID = req.Header.Get(headerName)
  36. }
  37. if requestID == "" {
  38. requestID = NewRequestID()
  39. req.Header.Set(headerName, requestID)
  40. }
  41. // immediately set the request ID to be reflected in the response
  42. w.Header().Set(requestIDHeader, requestID)
  43. // continue down the handler chain
  44. ctx := WithRequestID(req.Context(), requestID)
  45. next.ServeHTTP(w, req.WithContext(ctx))
  46. }
  47. return http.HandlerFunc(fn)
  48. }
  49. }
  50. // WithRequestID returns a new context with the given requestID added to the
  51. // context.
  52. func WithRequestID(ctx context.Context, requestID string) context.Context {
  53. return context.WithValue(ctx, RequestIDKey, requestID)
  54. }
  55. // GetRequestID returns the request id from the context if it exists.
  56. func GetRequestID(ctx context.Context) (string, bool) {
  57. v, ok := ctx.Value(RequestIDKey).(string)
  58. return v, ok
  59. }
  60. // WithUserID decodes the token, extracts the user from the payload and stores
  61. // it in the context.
  62. func WithUserID(ctx context.Context, userID string) context.Context {
  63. return context.WithValue(ctx, UserIDKey, userID)
  64. }
  65. // GetUserID returns the request id from the context if it exists.
  66. func GetUserID(ctx context.Context) (string, bool) {
  67. v, ok := ctx.Value(UserIDKey).(string)
  68. return v, ok
  69. }