-
Notifications
You must be signed in to change notification settings - Fork 92
/
prometheus.go
440 lines (384 loc) · 13.2 KB
/
prometheus.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2017 LabStack and Echo contributors
/*
Package echoprometheus provides middleware to add Prometheus metrics.
*/
package echoprometheus
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/expfmt"
"io"
"net/http"
"sort"
"strconv"
"time"
)
const (
defaultSubsystem = "echo"
)
const (
_ = iota // ignore first value by assigning to blank identifier
bKB float64 = 1 << (10 * iota)
bMB
)
// sizeBuckets is the buckets for request/response size. Here we define a spectrum from 1KB through 1NB up to 10MB.
var sizeBuckets = []float64{1.0 * bKB, 2.0 * bKB, 5.0 * bKB, 10.0 * bKB, 100 * bKB, 500 * bKB, 1.0 * bMB, 2.5 * bMB, 5.0 * bMB, 10.0 * bMB}
// MiddlewareConfig contains the configuration for creating prometheus middleware collecting several default metrics.
type MiddlewareConfig struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Namespace is components of the fully-qualified name of the Metric (created by joining Namespace,Subsystem and Name components with "_")
// Optional
Namespace string
// Subsystem is components of the fully-qualified name of the Metric (created by joining Namespace,Subsystem and Name components with "_")
// Defaults to: "echo"
Subsystem string
// LabelFuncs allows adding custom labels in addition to default labels. When key has same name with default label
// it replaces default one.
LabelFuncs map[string]LabelValueFunc
// HistogramOptsFunc allows to change options for metrics of type histogram before metric is registered to Registerer
HistogramOptsFunc func(opts prometheus.HistogramOpts) prometheus.HistogramOpts
// CounterOptsFunc allows to change options for metrics of type counter before metric is registered to Registerer
CounterOptsFunc func(opts prometheus.CounterOpts) prometheus.CounterOpts
// Registerer sets the prometheus.Registerer instance the middleware will register these metrics with.
// Defaults to: prometheus.DefaultRegisterer
Registerer prometheus.Registerer
// BeforeNext is callback that is executed before next middleware/handler is called. Useful for case when you have own
// metrics that need data to be stored for AfterNext.
BeforeNext func(c echo.Context)
// AfterNext is callback that is executed after next middleware/handler returns. Useful for case when you have own
// metrics that need incremented/observed.
AfterNext func(c echo.Context, err error)
timeNow func() time.Time
}
type LabelValueFunc func(c echo.Context, err error) string
// HandlerConfig contains the configuration for creating HTTP handler for metrics.
type HandlerConfig struct {
// Gatherer sets the prometheus.Gatherer instance the middleware will use when generating the metric endpoint handler.
// Defaults to: prometheus.DefaultGatherer
Gatherer prometheus.Gatherer
}
// PushGatewayConfig contains the configuration for pushing to a Prometheus push gateway.
type PushGatewayConfig struct {
// PushGatewayURL is push gateway URL in format http://domain:port
PushGatewayURL string
// PushInterval in ticker interval for pushing gathered metrics to the Gateway
// Defaults to: 1 minute
PushInterval time.Duration
// Gatherer sets the prometheus.Gatherer instance the middleware will use when generating the metric endpoint handler.
// Defaults to: prometheus.DefaultGatherer
Gatherer prometheus.Gatherer
// ErrorHandler is function that is called when errors occur. When callback returns error StartPushGateway also returns.
ErrorHandler func(err error) error
// ClientTransport specifies the mechanism by which individual HTTP POST requests are made.
// Defaults to: http.DefaultTransport
ClientTransport http.RoundTripper
}
// NewHandler creates new instance of Handler using Prometheus default registry.
func NewHandler() echo.HandlerFunc {
return NewHandlerWithConfig(HandlerConfig{})
}
// NewHandlerWithConfig creates new instance of Handler using given configuration.
func NewHandlerWithConfig(config HandlerConfig) echo.HandlerFunc {
if config.Gatherer == nil {
config.Gatherer = prometheus.DefaultGatherer
}
h := promhttp.HandlerFor(config.Gatherer, promhttp.HandlerOpts{})
if r, ok := config.Gatherer.(prometheus.Registerer); ok {
h = promhttp.InstrumentMetricHandler(r, h)
}
return func(c echo.Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}
// NewMiddleware creates new instance of middleware using Prometheus default registry.
func NewMiddleware(subsystem string) echo.MiddlewareFunc {
return NewMiddlewareWithConfig(MiddlewareConfig{Subsystem: subsystem})
}
// NewMiddlewareWithConfig creates new instance of middleware using given configuration.
func NewMiddlewareWithConfig(config MiddlewareConfig) echo.MiddlewareFunc {
mw, err := config.ToMiddleware()
if err != nil {
panic(err)
}
return mw
}
// ToMiddleware converts configuration to middleware or returns an error.
func (conf MiddlewareConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if conf.timeNow == nil {
conf.timeNow = time.Now
}
if conf.Subsystem == "" {
conf.Subsystem = defaultSubsystem
}
if conf.Registerer == nil {
conf.Registerer = prometheus.DefaultRegisterer
}
if conf.CounterOptsFunc == nil {
conf.CounterOptsFunc = func(opts prometheus.CounterOpts) prometheus.CounterOpts {
return opts
}
}
if conf.HistogramOptsFunc == nil {
conf.HistogramOptsFunc = func(opts prometheus.HistogramOpts) prometheus.HistogramOpts {
return opts
}
}
labelNames, customValuers := createLabels(conf.LabelFuncs)
requestCount := prometheus.NewCounterVec(
conf.CounterOptsFunc(prometheus.CounterOpts{
Namespace: conf.Namespace,
Subsystem: conf.Subsystem,
Name: "requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
}),
labelNames,
)
// we do not allow skipping or replacing default collector but developer can use `conf.CounterOptsFunc` to rename
// this middleware default collector, so they can have own collector with that same name.
// and we treat all register errors as returnable failures
if err := conf.Registerer.Register(requestCount); err != nil {
return nil, err
}
requestDuration := prometheus.NewHistogramVec(
conf.HistogramOptsFunc(prometheus.HistogramOpts{
Namespace: conf.Namespace,
Subsystem: conf.Subsystem,
Name: "request_duration_seconds",
Help: "The HTTP request latencies in seconds.",
// Here, we use the prometheus defaults which are for ~10s request length max: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
Buckets: prometheus.DefBuckets,
}),
labelNames,
)
if err := conf.Registerer.Register(requestDuration); err != nil {
return nil, err
}
responseSize := prometheus.NewHistogramVec(
conf.HistogramOptsFunc(prometheus.HistogramOpts{
Namespace: conf.Namespace,
Subsystem: conf.Subsystem,
Name: "response_size_bytes",
Help: "The HTTP response sizes in bytes.",
Buckets: sizeBuckets,
}),
labelNames,
)
if err := conf.Registerer.Register(responseSize); err != nil {
return nil, err
}
requestSize := prometheus.NewHistogramVec(
conf.HistogramOptsFunc(prometheus.HistogramOpts{
Namespace: conf.Namespace,
Subsystem: conf.Subsystem,
Name: "request_size_bytes",
Help: "The HTTP request sizes in bytes.",
Buckets: sizeBuckets,
}),
labelNames,
)
if err := conf.Registerer.Register(requestSize); err != nil {
return nil, err
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// NB: we do not skip metrics handler path by default. This can be added with custom Skipper but for default
// behaviour we measure metrics path request/response metrics also
if conf.Skipper != nil && conf.Skipper(c) {
return next(c)
}
if conf.BeforeNext != nil {
conf.BeforeNext(c)
}
reqSz := computeApproximateRequestSize(c.Request())
start := conf.timeNow()
err := next(c)
elapsed := float64(conf.timeNow().Sub(start)) / float64(time.Second)
if conf.AfterNext != nil {
conf.AfterNext(c, err)
}
url := c.Path() // contains route path ala `/users/:id`
if url == "" {
// as of Echo v4.10.1 path is empty for 404 cases (when router did not find any matching routes)
// in this case we use actual path from request to have some distinction in Prometheus
url = c.Request().URL.Path
}
status := c.Response().Status
if err != nil {
var httpError *echo.HTTPError
if errors.As(err, &httpError) {
status = httpError.Code
}
if status == 0 || status == http.StatusOK {
status = http.StatusInternalServerError
}
}
values := make([]string, len(labelNames))
values[0] = strconv.Itoa(status)
values[1] = c.Request().Method
values[2] = c.Request().Host
values[3] = url
for _, cv := range customValuers {
values[cv.index] = cv.valueFunc(c, err)
}
requestDuration.WithLabelValues(values...).Observe(elapsed)
requestCount.WithLabelValues(values...).Inc()
requestSize.WithLabelValues(values...).Observe(float64(reqSz))
responseSize.WithLabelValues(values...).Observe(float64(c.Response().Size))
return err
}
}, nil
}
type customLabelValuer struct {
index int
label string
valueFunc LabelValueFunc
}
func createLabels(customLabelFuncs map[string]LabelValueFunc) ([]string, []customLabelValuer) {
labelNames := []string{"code", "method", "host", "url"}
if len(customLabelFuncs) == 0 {
return labelNames, nil
}
customValuers := make([]customLabelValuer, 0)
// we create valuers in two passes for a reason - first to get fixed order, and then we know to assign correct indexes
for label, labelFunc := range customLabelFuncs {
customValuers = append(customValuers, customLabelValuer{
label: label,
valueFunc: labelFunc,
})
}
sort.Slice(customValuers, func(i, j int) bool {
return customValuers[i].label < customValuers[j].label
})
for cvIdx, cv := range customValuers {
idx := containsAt(labelNames, cv.label)
if idx == -1 {
idx = len(labelNames)
labelNames = append(labelNames, cv.label)
}
customValuers[cvIdx].index = idx
}
return labelNames, customValuers
}
func containsAt[K comparable](haystack []K, needle K) int {
for i, v := range haystack {
if v == needle {
return i
}
}
return -1
}
func computeApproximateRequestSize(r *http.Request) int {
s := 0
if r.URL != nil {
s = len(r.URL.Path)
}
s += len(r.Method)
s += len(r.Proto)
for name, values := range r.Header {
s += len(name)
for _, value := range values {
s += len(value)
}
}
s += len(r.Host)
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
if r.ContentLength != -1 {
s += int(r.ContentLength)
}
return s
}
// RunPushGatewayGatherer starts pushing collected metrics and waits for it context to complete or ErrorHandler to return error.
//
// Example:
// ```
//
// go func() {
// config := echoprometheus.PushGatewayConfig{
// PushGatewayURL: "https://host:9080",
// PushInterval: 10 * time.Millisecond,
// }
// if err := echoprometheus.RunPushGatewayGatherer(context.Background(), config); !errors.Is(err, context.Canceled) {
// log.Fatal(err)
// }
// }()
//
// ```
func RunPushGatewayGatherer(ctx context.Context, config PushGatewayConfig) error {
if config.PushGatewayURL == "" {
return errors.New("push gateway URL is missing")
}
if config.PushInterval <= 0 {
config.PushInterval = 1 * time.Minute
}
if config.Gatherer == nil {
config.Gatherer = prometheus.DefaultGatherer
}
if config.ErrorHandler == nil {
config.ErrorHandler = func(err error) error {
log.Error(err)
return nil
}
}
client := &http.Client{
Transport: config.ClientTransport,
}
out := &bytes.Buffer{}
ticker := time.NewTicker(config.PushInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
out.Reset()
err := WriteGatheredMetrics(out, config.Gatherer)
if err != nil {
if hErr := config.ErrorHandler(fmt.Errorf("failed to create metrics: %w", err)); hErr != nil {
return hErr
}
continue
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, config.PushGatewayURL, out)
if err != nil {
if hErr := config.ErrorHandler(fmt.Errorf("failed to create push gateway request: %w", err)); hErr != nil {
return hErr
}
continue
}
res, err := client.Do(req)
if err != nil {
if hErr := config.ErrorHandler(fmt.Errorf("error sending to push gateway: %w", err)); hErr != nil {
return hErr
}
}
if res.StatusCode != http.StatusOK {
if hErr := config.ErrorHandler(echo.NewHTTPError(res.StatusCode, "post metrics request did not succeed")); hErr != nil {
return hErr
}
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// WriteGatheredMetrics gathers collected metrics and writes them to given writer
func WriteGatheredMetrics(writer io.Writer, gatherer prometheus.Gatherer) error {
metricFamilies, err := gatherer.Gather()
if err != nil {
return err
}
for _, mf := range metricFamilies {
if _, err := expfmt.MetricFamilyToText(writer, mf); err != nil {
return err
}
}
return nil
}