diff --git a/context/http.go b/context/http.go index 68f977ce0d..c3e692118e 100644 --- a/context/http.go +++ b/context/http.go @@ -62,6 +62,16 @@ func RemoteIP(r *http.Request) string { return addr } +// getXRequestID get X-Request-ID from HTTP header +// or generate an UUID if not found in header +func getXRequestID(r *http.Request) string { + rid := r.Header.Get("X-Request-ID") + if rid != "" { + return rid + } + return uuid.Generate().String() +} + // WithRequest places the request on the context. The context of the request // is assigned a unique id, available at "http.request.id". The request itself // is available at "http.request". Other common attributes are available under @@ -78,7 +88,7 @@ func WithRequest(ctx context.Context, r *http.Request) context.Context { return &httpRequestContext{ Context: ctx, startedAt: time.Now(), - id: uuid.Generate().String(), + id: getXRequestID(r), r: r, } }