-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetrics.go
35 lines (29 loc) · 943 Bytes
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"net/http"
"github.com/go-chi/chi/v5/middleware"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
uploadCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "uploads",
Help: "The total number of uploads",
})
downloadCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "downloads",
Help: "The total number of files fetched",
})
httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_response_duration_seconds",
Help: "Latency of requests in second.",
}, []string{"path"})
)
func metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(httpDuration.WithLabelValues(r.URL.Path))
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(ww, r)
timer.ObserveDuration()
})
}