-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
70 lines (61 loc) · 2.06 KB
/
middleware.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package prom_http_exporter
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type exporter struct {
inFlightGauge prometheus.Gauge
counter *prometheus.CounterVec
duration *prometheus.HistogramVec
responseSize *prometheus.HistogramVec
}
func New() *exporter {
e := &exporter{
inFlightGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "http",
Name: "in_flight_requests",
Help: "A gauge of requests currently being served by the wrapped handler.",
}),
counter: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "http",
Name: "api_requests_total",
Help: "A counter for requests to the wrapped handler.",
},
[]string{"code", "method", "handler"},
),
duration: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "http",
Name: "request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
},
[]string{"handler", "method"},
),
responseSize: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "http",
Name: "response_size_bytes",
Help: "A histogram of response sizes for requests.",
Buckets: []float64{200, 500, 900, 1500},
},
[]string{},
),
}
prometheus.MustRegister(e.inFlightGauge, e.counter, e.duration, e.responseSize)
return e
}
func (e *exporter) Metric(path string, h http.HandlerFunc) (string, http.Handler) {
return path, promhttp.InstrumentHandlerInFlight(e.inFlightGauge,
promhttp.InstrumentHandlerDuration(e.duration.MustCurryWith(prometheus.Labels{"handler": path}),
promhttp.InstrumentHandlerCounter(e.counter.MustCurryWith(prometheus.Labels{"handler": path}),
promhttp.InstrumentHandlerResponseSize(e.responseSize, h),
),
),
)
}