webhookd/pkg/middleware/tracing.go
2020-02-29 08:18:12 +00:00

22 lines
547 B
Go

package middleware
import (
"context"
"net/http"
)
// Tracing is a middleware to trace HTTP request
func Tracing(nextRequestID func() string) Middleware {
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))
})
}
}