From 38d8aa0b249bbdb7015c5d1ef96d59d1af432f7e Mon Sep 17 00:00:00 2001 From: Paul Aldridge Date: Mon, 11 Apr 2022 08:38:33 +0100 Subject: [PATCH] logger: add request id to log context for traceability this allows requests to be traced through the logs Fixes #1547 --- httptransport/indexer_v1.go | 2 ++ middleware/introspection/instrumentedhandler.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/httptransport/indexer_v1.go b/httptransport/indexer_v1.go index 944a90cad6..3ea13911f1 100644 --- a/httptransport/indexer_v1.go +++ b/httptransport/indexer_v1.go @@ -55,6 +55,8 @@ var _ http.Handler = (*IndexerV1)(nil) // ServeHTTP implements http.Handler. func (h *IndexerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) { start := time.Now() + ctx := zlog.ContextWithValues(r.Context(), "request_id", r.Header.Get("x-request-id")) + r = r.WithContext(ctx) wr := responserecorder.NewResponseRecorder(w) defer func() { if f, ok := wr.(http.Flusher); ok { diff --git a/middleware/introspection/instrumentedhandler.go b/middleware/introspection/instrumentedhandler.go index 61df99e9b6..3240c05881 100644 --- a/middleware/introspection/instrumentedhandler.go +++ b/middleware/introspection/instrumentedhandler.go @@ -66,6 +66,8 @@ func InstrumentedHandler(endpoint string, traceOpts othttp.Option, next http.Han h = promhttp.InstrumentHandlerResponseSize(ResponseSize, h) h = promhttp.InstrumentHandlerTimeToWriteHeader(RequestDuration, h) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := zlog.ContextWithValues(r.Context(), "request_id", r.Header.Get("x-request-id")) + r = r.WithContext(ctx) recorder := rr.NewResponseRecorder(w) h.ServeHTTP(recorder, r) zlog.Info(r.Context()).