-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrequest_metrics.go
143 lines (120 loc) · 3.92 KB
/
request_metrics.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package middleware
import (
"fmt"
"reflect"
"strconv"
"github.com/ankorstore/yokai/httpserver/normalization"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/prometheus/client_golang/prometheus"
)
const (
HttpServerMetricsRequestsCount = "http_server_requests_total"
HttpServerMetricsRequestsDuration = "http_server_requests_duration_seconds"
HttpServerMetricsNotFoundPath = "/not-found"
)
// RequestMetricsMiddlewareConfig is the configuration for the [RequestMetricsMiddleware].
type RequestMetricsMiddlewareConfig struct {
Skipper middleware.Skipper
Registry prometheus.Registerer
Namespace string
Buckets []float64
Subsystem string
NormalizeRequestPath bool
NormalizeResponseStatus bool
}
// DefaultRequestMetricsMiddlewareConfig is the default configuration for the [RequestMetricsMiddleware].
var DefaultRequestMetricsMiddlewareConfig = RequestMetricsMiddlewareConfig{
Skipper: middleware.DefaultSkipper,
Registry: prometheus.DefaultRegisterer,
Namespace: "",
Subsystem: "",
Buckets: prometheus.DefBuckets,
NormalizeRequestPath: true,
NormalizeResponseStatus: true,
}
// RequestMetricsMiddleware returns a [RequestMetricsMiddleware] with the [DefaultRequestMetricsMiddlewareConfig].
func RequestMetricsMiddleware() echo.MiddlewareFunc {
return RequestMetricsMiddlewareWithConfig(DefaultRequestMetricsMiddlewareConfig)
}
// RequestMetricsMiddlewareWithConfig returns a [RequestMetricsMiddleware] for a provided [RequestMetricsMiddlewareConfig].
func RequestMetricsMiddlewareWithConfig(config RequestMetricsMiddlewareConfig) echo.MiddlewareFunc {
if config.Skipper == nil {
config.Skipper = DefaultRequestMetricsMiddlewareConfig.Skipper
}
if config.Registry == nil {
config.Registry = DefaultRequestMetricsMiddlewareConfig.Registry
}
if config.Namespace == "" {
config.Namespace = DefaultRequestMetricsMiddlewareConfig.Namespace
}
if config.Subsystem == "" {
config.Subsystem = DefaultRequestMetricsMiddlewareConfig.Subsystem
}
if len(config.Buckets) == 0 {
config.Buckets = DefaultRequestMetricsMiddlewareConfig.Buckets
}
httpRequestsCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: HttpServerMetricsRequestsCount,
Help: "Number of processed HTTP requests",
},
[]string{
"status",
"method",
"path",
},
)
httpRequestsDuration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: HttpServerMetricsRequestsDuration,
Help: "Time spent processing HTTP requests",
Buckets: config.Buckets,
},
[]string{
"method",
"path",
},
)
config.Registry.MustRegister(httpRequestsCounter, httpRequestsDuration)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// skipper
if config.Skipper(c) {
return next(c)
}
req := c.Request()
var path string
if config.NormalizeRequestPath {
path = c.Path()
} else {
path = req.URL.Path
if req.URL.RawQuery != "" {
path = fmt.Sprintf("%s?%s", path, req.URL.RawQuery)
}
}
// to avoid high cardinality on 404s
if reflect.ValueOf(c.Handler()).Pointer() == reflect.ValueOf(echo.NotFoundHandler).Pointer() {
path = HttpServerMetricsNotFoundPath
}
timer := prometheus.NewTimer(httpRequestsDuration.WithLabelValues(req.Method, path))
err := next(c)
timer.ObserveDuration()
if err != nil {
c.Error(err)
}
status := ""
if config.NormalizeResponseStatus {
status = normalization.NormalizeStatus(c.Response().Status)
} else {
status = strconv.Itoa(c.Response().Status)
}
httpRequestsCounter.WithLabelValues(status, req.Method, path).Inc()
return err
}
}
}