-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
79 lines (67 loc) · 2.19 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
// copyright carousell
// this file coming from : https://github.com/carousell/fasthttp-prometheus-middleware/blob/master/prometheus.go
package middleware
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
// Prometheus contains the metrics gathered by the instance and its path
type Prometheus struct {
uri string
reqCnt *prometheus.CounterVec
duration *prometheus.HistogramVec
}
// NewPrometheus generates a new set of metrics with a certain subsystem name
func NewPrometheus(subsystem, uri string) *Prometheus {
p := &Prometheus{uri: uri}
p.registerMetrics(subsystem)
return p
}
func (p *Prometheus) registerMetrics(subsystem string) {
p.duration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "request latencies",
Buckets: []float64{.005, .01, .02, 0.04, .06, 0.08, .1, 0.15, .25, 0.4, .6, .8, 1, 1.5, 2, 3, 5},
},
[]string{"code", "path"},
)
p.reqCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: subsystem,
Name: "requests_total",
Help: "The HTTP request counts processed.",
},
[]string{"code", "method"},
)
prometheus.MustRegister(p.reqCnt, p.duration)
}
func (p *Prometheus) Handle() func(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
uri := string(ctx.Request.URI().Path())
if uri == p.uri {
// next
h(ctx)
return
}
start := time.Now()
// next
h(ctx)
status := strconv.Itoa(ctx.Response.StatusCode())
elapsed := float64(time.Since(start)) / float64(time.Second)
ep := string(ctx.Method()) + "_" + uri
p.duration.WithLabelValues(status, ep).Observe(elapsed)
p.reqCnt.WithLabelValues(status, string(ctx.Method())).Inc()
}
}
}
// since prometheus/client_golang use net/http we need this net/http adapter for fasthttp
func PrometheusHandler() fasthttp.RequestHandler {
return fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())
}