forked from Equanox/petasos-rewriter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
145 lines (123 loc) · 3.89 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
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
144
145
package main
import (
"strconv"
"time"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
)
var labelNames = []string{"code", "method", "host", "url"}
type metricRegistry struct {
TotalRequests *prometheus.CounterVec
ServerRequestDuration *prometheus.HistogramVec
RequestsWithoutAuthHeader *prometheus.CounterVec
RequestsWithAuthHeader *prometheus.CounterVec
}
func provideMetrics(e *echo.Echo) {
metrics := echo.New()
metrics.Use(middleware.Logger())
metrics.Use(middleware.Recover())
mr := registerMetrics()
if err := prometheus.Register(mr.TotalRequests); err != nil {
metrics.Logger.Fatal(err)
}
if err := prometheus.Register(mr.ServerRequestDuration); err != nil {
metrics.Logger.Fatal(err)
}
if err := prometheus.Register(mr.RequestsWithoutAuthHeader); err != nil {
metrics.Logger.Fatal(err)
}
if err := prometheus.Register(mr.RequestsWithAuthHeader); err != nil {
metrics.Logger.Fatal(err)
}
metrics.Use(mr.getMiddleware())
metrics.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
go func() {
metrics.Logger.Fatal(metrics.Start(":" + viper.GetString(metricsServerPort)))
}()
e.Use(mr.getMiddleware())
}
func registerMetrics() *metricRegistry {
namespace := viper.GetString("metricsOptions.namespace")
if len(namespace) == 0 {
namespace = "xmidt"
}
subsystem := viper.GetString("metricsOptions.subsystem")
if len(subsystem) == 0 {
subsystem = "petasos_rewriter"
}
totalRequests := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "server_request_count",
Help: "total incoming HTTP requests",
},
labelNames,
)
serverRequestDuration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "server_request_duration_seconds",
Help: "tracks incoming request durations in seconds",
Buckets: []float64{0.1, 0.5, 1, 1.5, 2, 2.5, 3},
},
labelNames,
)
requestsWithoutAuthHeader := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "server_request_without_auth_header_count",
Help: "total requests without authorization header",
},
labelNames,
)
requestsWithAuthHeader := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "server_request_with_auth_header_count",
Help: "total requests with authorization header",
},
labelNames,
)
return &metricRegistry{
TotalRequests: totalRequests,
ServerRequestDuration: serverRequestDuration,
RequestsWithoutAuthHeader: requestsWithoutAuthHeader,
RequestsWithAuthHeader: requestsWithAuthHeader,
}
}
func (mr *metricRegistry) getMiddleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
isCpeRedirectRequest := strings.Contains(c.Request().RequestURI, authHeaderCheckRequestPath)
isAuthHeaderPresent := len(c.Request().Header.Get("Authorization")) > 0
c.Request().Header.Set("Accept-Encoding", "identity")
startTime := time.Now()
err := next(c)
elapsed := time.Since(startTime).Seconds()
status := strconv.Itoa(c.Response().Status)
values := make([]string, len(labelNames))
values[0] = status
values[1] = c.Request().Method
values[2] = c.Request().Host
values[3] = c.Path()
mr.TotalRequests.WithLabelValues(values...).Inc()
mr.ServerRequestDuration.WithLabelValues(values...).Observe(elapsed)
if isCpeRedirectRequest {
if !isAuthHeaderPresent {
mr.RequestsWithoutAuthHeader.WithLabelValues(values...).Inc()
} else {
mr.RequestsWithAuthHeader.WithLabelValues(values...).Inc()
}
}
return err
}
}
}