-
Notifications
You must be signed in to change notification settings - Fork 76
/
metrics_middleware.go
62 lines (53 loc) · 2.42 KB
/
metrics_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
package middleware
import (
"net/http"
"github.com/launchdarkly/ld-relay/v6/internal/core/internal/metrics"
"github.com/gorilla/mux"
)
func withCount(handler http.Handler, measure metrics.Measure) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := GetEnvContextInfo(req.Context()).Env
userAgent := getUserAgent(req)
metrics.WithCount(ctx.GetMetricsContext(), userAgent, func() {
handler.ServeHTTP(w, req)
}, measure)
})
}
func withGauge(handler http.Handler, measure metrics.Measure) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := GetEnvContextInfo(req.Context())
userAgent := getUserAgent(req)
metrics.WithGauge(ctx.Env.GetMetricsContext(), userAgent, func() {
handler.ServeHTTP(w, req)
}, measure)
})
}
// CountMobileConns is a middleware function that increments the total number of mobile connections,
// and also increments the number of active mobile connections until the handler ends.
func CountMobileConns(handler http.Handler) http.Handler {
return withCount(withGauge(handler, metrics.MobileConns), metrics.NewMobileConns)
}
// CountBrowserConns is a middleware function that increments the total number of browser connections,
// and also increments the number of active browser connections until the handler ends.
func CountBrowserConns(handler http.Handler) http.Handler {
return withCount(withGauge(handler, metrics.BrowserConns), metrics.NewBrowserConns)
}
// CountServerConns is a middleware function that increments the total number of server-side connections,
// and also increments the number of active server-side connections until the handler ends.
func CountServerConns(handler http.Handler) http.Handler {
return withCount(withGauge(handler, metrics.ServerConns), metrics.NewServerConns)
}
// RequestCount is a middleware function that increments the specified metric for each request.
func RequestCount(measure metrics.Measure) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := GetEnvContextInfo(req.Context())
userAgent := getUserAgent(req)
// Ignoring internal routing error that would have been ignored anyway
route, _ := mux.CurrentRoute(req).GetPathTemplate()
metrics.WithRouteCount(ctx.Env.GetMetricsContext(), userAgent, route, req.Method, func() {
next.ServeHTTP(w, req)
}, measure)
})
}
}