-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrument.go
executable file
·53 lines (44 loc) · 1.49 KB
/
instrument.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package http
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func (hs *Server) instrument(method string, path string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return promhttp.InstrumentHandlerDuration(
httpRequestDuration(method, path).MustCurryWith(prometheus.Labels{"handler": path}),
promhttp.InstrumentHandlerCounter(
httpRequestsTotalCounterVec(method, path),
next,
),
)
}
}
func httpRequestsTotalCounterVec(method string, path string) *prometheus.CounterVec {
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "A counter for requests to the wrapped handler.",
ConstLabels: prometheus.Labels{"path": path, "method": method},
},
[]string{"code"},
)
prometheus.MustRegister(counter)
return counter
}
func httpRequestDuration(method string, path string) *prometheus.HistogramVec {
// duration is partitioned by the HTTP method and handler. It uses custom
// buckets based on the expected request duration.
duration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
ConstLabels: prometheus.Labels{"path": path, "method": method},
},
[]string{"handler"},
)
prometheus.MustRegister(duration)
return duration
}