Skip to content

feat(coderd/httpmw): log start timestamp for http requests #9776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions coderd/httpmw/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
slog.F("path", r.URL.Path),
slog.F("proto", r.Proto),
slog.F("remote_addr", r.RemoteAddr),
// Include the start timestamp in the log so that we have the
// source of truth. There is at least a theoretical chance that
// there can be a delay between `next.ServeHTTP` ending and us
// actually logging the request. This can also be useful when
// filtering logs that started at a certain time (compared to
// trying to compute the value).
slog.F("start", start),
)

next.ServeHTTP(sw, r)

end := time.Now()

// Don't log successful health check requests.
if r.URL.Path == "/api/v2" && sw.Status == http.StatusOK {
return
}

httplog = httplog.With(
slog.F("took", time.Since(start)),
slog.F("took", end.Sub(start)),
slog.F("status_code", sw.Status),
slog.F("latency_ms", float64(time.Since(start)/time.Millisecond)),
slog.F("latency_ms", float64(end.Sub(start)/time.Millisecond)),
)

// For status codes 400 and higher we
Expand Down