Add request_duration_seconds metric

This commit is contained in:
Daan Sprenkels
2021-05-16 20:24:00 +02:00
parent a26894dac8
commit c4ff0ab1b7
2 changed files with 28 additions and 1 deletions

View File

@@ -58,13 +58,18 @@ func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
func (rl *rushlink) metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tick := time.Now()
srw := statusResponseWriter{Inner: w}
next.ServeHTTP(&srw, r)
tock := time.Now()
status := strconv.Itoa(srw.StatusCode)
labels := map[string]string{"code": status, "method": r.Method}
// Update requests counter metric
metricRequestsTotalCounter.With(labels).Inc()
// Update request latency metric
elapsed := tock.Sub(tick)
metricRequestsLatencyNanoSeconds.With(labels).Observe(elapsed.Seconds())
})
}