-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmetrics.go
88 lines (80 loc) · 2.32 KB
/
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
package middleware
import (
"time"
handler_utils "github.com/content-services/content-sources-backend/pkg/handler/utils"
"github.com/content-services/content-sources-backend/pkg/instrumentation"
"github.com/labstack/echo/v4"
echo_middleware "github.com/labstack/echo/v4/middleware"
"github.com/prometheus/client_golang/prometheus"
)
type MetricsConfig struct {
Skipper echo_middleware.Skipper
Metrics *instrumentation.Metrics
}
var defaultConfig MetricsConfig = MetricsConfig{
Skipper: echo_middleware.DefaultSkipper,
Metrics: instrumentation.NewMetrics(prometheus.NewRegistry()),
}
func mapStatus(status int) string {
switch {
case status >= 100 && status < 200:
return "1xx"
case status >= 200 && status < 300:
return "2xx"
case status >= 300 && status < 400:
return "3xx"
case status >= 400 && status < 500:
return "4xx"
case status >= 500 && status < 600:
return "5xx"
default:
return ""
}
}
func MetricsMiddlewareWithConfig(config *MetricsConfig) echo.MiddlewareFunc {
if config == nil {
config = &defaultConfig
}
if config.Skipper == nil {
config.Skipper = echo_middleware.DefaultSkipper
}
if config.Metrics == nil {
panic("config.Metrics can not be nil")
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
start := time.Now()
if config.Skipper != nil && config.Skipper(ctx) {
return next(ctx)
}
method := ctx.Request().Method
path := MatchedRoute(ctx)
err := next(ctx)
timeStart := time.Since(start)
status := mapStatus(ctx.Response().Status)
defer config.Metrics.HttpStatusHistogram.WithLabelValues(status, method, path).Observe(timeStart.Seconds())
return err
}
}
}
// See: https://echo.labstack.com/middleware/prometheus/#skipping-certain-urls
func metricsMiddlewareSkipper(ctx echo.Context) bool {
path := ctx.Request().URL.Path
switch {
case path == "/ping" || path == "/ping/":
return true
case path == "/metrics" || path == "/metrics/":
return true
}
pathItemsWithoutPrefixes := handler_utils.NewPathWithString(path).RemovePrefixes()
return pathItemsWithoutPrefixes.StartWithResources(
[]string{"ping"},
)
}
func CreateMetricsMiddleware(metrics *instrumentation.Metrics) echo.MiddlewareFunc {
return MetricsMiddlewareWithConfig(
&MetricsConfig{
Skipper: metricsMiddlewareSkipper,
Metrics: metrics,
})
}