Skip to content

Commit

Permalink
api: log binned request latencies
Browse files Browse the repository at this point in the history
changelog
  • Loading branch information
Andrew7234 committed Mar 27, 2024
1 parent 0952d2e commit 134cfc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .changelog/676.api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve api request logging

- log binned request latencies
- log request query params
24 changes: 22 additions & 2 deletions api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ func MetricsMiddleware(m metrics.RequestMetrics, logger log.Logger) func(next ht
// like we're doing here if we don't want to use it.
timer.ObserveDuration()
}
latency := time.Since(t)
logger.Info("ending request",
"endpoint", r.URL.Path,
"query_path", r.URL.Path,
"query_params", r.URL.RawQuery,
"request_id", requestID,
"time", time.Since(t),
"latency", latency,
"latency_bin", binQueryLatency(latency),
"status_code", httpStatus,
)

Expand All @@ -104,6 +107,23 @@ func MetricsMiddleware(m metrics.RequestMetrics, logger log.Logger) func(next ht
}
}

// Bin request durations to make it easier to search
// for slow queries in Nexus logs.
func binQueryLatency(t time.Duration) string {
switch {
case t < 100*time.Millisecond:
return "<100ms"
case t < 300*time.Millisecond:
return "100-300ms"
case t < 500*time.Millisecond:
return "300-500ms"
case t < 1000*time.Millisecond:
return "500-1000ms"
default:
return ">1000ms"
}
}

// Sets a value for `Limit` and `Offset` fields of the given struct, if present and nil-valued.
// oapi-codegen ignores the specified default value in the openapi spec:
//
Expand Down

0 comments on commit 134cfc6

Please sign in to comment.