-
Notifications
You must be signed in to change notification settings - Fork 13
/
instrumentation.go
61 lines (54 loc) · 2.05 KB
/
instrumentation.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
package main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/grafana/dskit/instrument"
"github.com/grafana/dskit/middleware"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type metrics struct {
RequestDuration *prometheus.HistogramVec
ReceivedMessageSize *prometheus.HistogramVec
SentMessageSize *prometheus.HistogramVec
InflightRequests *prometheus.GaugeVec
}
func newMetrics(reg prometheus.Registerer) *metrics {
return &metrics{
RequestDuration: promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{
Name: "rollout_operator_request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: instrument.DefBuckets,
}, []string{"method", "route", "status_code", "ws"}),
ReceivedMessageSize: promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{
Name: "rollout_operator_request_message_bytes",
Help: "Size (in bytes) of messages received in the request.",
Buckets: middleware.BodySizeBuckets,
}, []string{"method", "route"}),
SentMessageSize: promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{
Name: "rollout_operator_response_message_bytes",
Help: "Size (in bytes) of messages sent in response.",
Buckets: middleware.BodySizeBuckets,
}, []string{"method", "route"}),
InflightRequests: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Name: "rollout_operator_inflight_requests",
Help: "Current number of inflight requests.",
}, []string{"method", "route"}),
}
}
func newInstrumentedRouter(metrics *metrics) (*mux.Router, http.Handler) {
router := mux.NewRouter()
httpMiddleware := []middleware.Interface{
middleware.Tracer{
RouteMatcher: router,
},
middleware.Instrument{
RouteMatcher: router,
Duration: metrics.RequestDuration,
RequestBodySize: metrics.ReceivedMessageSize,
ResponseBodySize: metrics.SentMessageSize,
InflightRequests: metrics.InflightRequests,
},
}
return router, middleware.Merge(httpMiddleware...).Wrap(router)
}