Skip to content

Commit

Permalink
logger: add request id to log context for traceability
Browse files Browse the repository at this point in the history
this allows requests to be traced through the logs

Fixes quay#1547

Signed-off-by: Paul Aldridge <paulaldridge@uk.ibm.com>
  • Loading branch information
paulaldridge committed Apr 11, 2022
1 parent 53e944f commit cf97e78
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions httptransport/indexer_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions middleware/introspection/instrumentedhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down

0 comments on commit cf97e78

Please sign in to comment.