From 4609bb0c90040eb6209372572f1f1f1b0a576b63 Mon Sep 17 00:00:00 2001 From: bin liu Date: Tue, 29 Aug 2023 15:49:05 +0800 Subject: [PATCH] pass X-Request-ID from upstream Signed-off-by: bin liu --- context/http.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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, } }