2018-12-18 19:52:03 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tracing is a middleware to trace HTTP request
|
2020-02-29 08:15:12 +00:00
|
|
|
func Tracing(nextRequestID func() string) Middleware {
|
2018-12-18 19:52:03 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestID := r.Header.Get("X-Request-Id")
|
|
|
|
if requestID == "" {
|
|
|
|
requestID = nextRequestID()
|
|
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
|
|
|
|
w.Header().Set("X-Request-Id", requestID)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|