Newer
Older
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
package server
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
namespacesMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "namespaces",
Help: "Number of namespaces",
})
displayListernersMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "display_listeners",
Help: "Active clients listening to /display",
})
counterMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "requests_total",
Help: "Count the total number of requests.",
},
[]string{"path", "code"},
)
durationMetric = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "requests_duration_seconds",
Help: "A histogram of duration for requests.",
Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
},
[]string{"path"},
)
)
func init() {
prometheus.MustRegister(namespacesMetric, displayListernersMetric, counterMetric, durationMetric)
}
func monitoringMiddleware(handler string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return promhttp.InstrumentHandlerDuration(
durationMetric.MustCurryWith(prometheus.Labels{"path": handler}),
promhttp.InstrumentHandlerCounter(counterMetric.MustCurryWith(prometheus.Labels{"path": handler}), next))
}
}