Skip to content

Commit 4e44204

Browse files
authored
feat(coderd/httpmw): log start timestamp for http requests (#9776)
1 parent c67db6e commit 4e44204

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

coderd/httpmw/logger.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
2626
slog.F("path", r.URL.Path),
2727
slog.F("proto", r.Proto),
2828
slog.F("remote_addr", r.RemoteAddr),
29+
// Include the start timestamp in the log so that we have the
30+
// source of truth. There is at least a theoretical chance that
31+
// there can be a delay between `next.ServeHTTP` ending and us
32+
// actually logging the request. This can also be useful when
33+
// filtering logs that started at a certain time (compared to
34+
// trying to compute the value).
35+
slog.F("start", start),
2936
)
3037

3138
next.ServeHTTP(sw, r)
3239

40+
end := time.Now()
41+
3342
// Don't log successful health check requests.
3443
if r.URL.Path == "/api/v2" && sw.Status == http.StatusOK {
3544
return
3645
}
3746

3847
httplog = httplog.With(
39-
slog.F("took", time.Since(start)),
48+
slog.F("took", end.Sub(start)),
4049
slog.F("status_code", sw.Status),
41-
slog.F("latency_ms", float64(time.Since(start)/time.Millisecond)),
50+
slog.F("latency_ms", float64(end.Sub(start)/time.Millisecond)),
4251
)
4352

4453
// For status codes 400 and higher we

0 commit comments

Comments
 (0)