forked from carousell/fasthttp-prometheus-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
95 lines (84 loc) · 2.92 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
package fasthttpprom
import (
zeroconv "github.com/savsgio/gotils/strconv"
"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 {
reqs *prometheus.CounterVec
latency *prometheus.HistogramVec
}
const (
reqsName = "requests_total"
latencyName = "request_duration_milliseconds"
)
var (
dflBuckets = []float64{300, 1200, 5000}
DefaultBuckets = []float64{.005, .01, .02, 0.04, .06, 0.08, .1, 0.15, .25, 0.4, .6, .8, 1, 1.5, 2, 3, 5}
)
//func (p *Prometheus) registerHisto(name, prefix string, buckets ...float64) {
//
//
//}
//
//func (p *Prometheus) registerCount(name, prefix string) {
// p.reqs = prometheus.NewCounterVec(
// prometheus.CounterOpts{
// Name: addPrefixIfNeeded(reqsName, prefix),
// Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path.",
// ConstLabels: prometheus.Labels{"service": name},
// },
// []string{"code", "method", "path"},
// )
//}
// NewMiddleware is
func NewMiddleware(name, prefix string, buckets ...float64) func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
var p Prometheus
p.reqs = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: addPrefixIfNeeded(reqsName, prefix),
Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path.",
ConstLabels: prometheus.Labels{"service": name},
},
[]string{"code", "method", "path"},
)
prometheus.MustRegister(p.reqs)
if len(buckets) == 0 {
buckets = dflBuckets
}
p.latency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: addPrefixIfNeeded(latencyName, prefix),
Help: "How long it took to process the request, partitioned by status code, method and HTTP path.",
ConstLabels: prometheus.Labels{"service": name},
Buckets: buckets,
},
[]string{"code", "method", "path"},
)
prometheus.MustRegister(p.latency)
return p.handler
}
func (p Prometheus) handler(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
start := time.Now()
next(ctx)
p.reqs.WithLabelValues(strconv.Itoa(ctx.Response.StatusCode()), zeroconv.B2S(ctx.Method()), zeroconv.B2S(ctx.URI().Path())).Inc()
p.latency.WithLabelValues(strconv.Itoa(ctx.Response.StatusCode()),
zeroconv.B2S(ctx.Method()), zeroconv.B2S(ctx.URI().Path())).Observe(float64(time.Since(start).Nanoseconds()) / 1000000)
}
}
// since prometheus/client_golang use net/http we need this net/http adapter for fasthttp
func PrometheusHandler() fasthttp.RequestHandler {
return fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())
}
func addPrefixIfNeeded(name, prefix string) string {
if prefix == "" {
return name
}
return prefix + "_" + name
}