Skip to content

Commit

Permalink
Change Instance.ServeHTTP so PathValue is set on request
Browse files Browse the repository at this point in the history
This means the path pattern can't be logged, but this is fixed in a forthcoming Go release.
  • Loading branch information
infogulch committed May 19, 2024
1 parent 3a2ea00 commit 20530ff
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,27 @@ func (instance *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, requestIdKey, rid)
}

// See handlers.go
handler, handlerPattern := instance.router.Handler(r)

log := instance.config.Logger.With(slog.Group("serve",
slog.String("requestid", rid),
))
log.LogAttrs(r.Context(), slog.LevelDebug, "serving request",
slog.String("user-agent", r.Header.Get("User-Agent")),
slog.String("method", r.Method),
slog.String("requestPath", r.URL.Path),
slog.String("handlerPattern", handlerPattern),
)
ctx = context.WithValue(ctx, loggerKey, log)

r = r.WithContext(context.WithValue(ctx, loggerKey, log))
metrics := httpsnoop.CaptureMetrics(handler, w, r)
r = r.WithContext(ctx)
metrics := httpsnoop.CaptureMetrics(instance.router, w, r)

log.LogAttrs(r.Context(), levelDebug2, "request served",
slog.Group("response",
slog.Duration("duration", metrics.Duration),
slog.Int("statusCode", metrics.Code),
slog.Int64("bytes", metrics.Written)))
slog.Int64("bytes", metrics.Written),
// Uncomment after release with this commit: https://github.com/golang/go/commit/a523152ea1df8d39d923ed90d19662896eff0607
// slog.String("pattern", r.Pattern),
))
}

type requestIdType struct{}
Expand Down

0 comments on commit 20530ff

Please sign in to comment.