-
Notifications
You must be signed in to change notification settings - Fork 8
/
middleware.go
149 lines (117 loc) · 3.45 KB
/
middleware.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
package fasthttpprometheus
import (
"strconv"
"sync"
"time"
"github.com/fasthttp/router"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
var (
defaultMetricPath = "/metrics"
requestHandlerPool sync.Pool
)
type FasthttpHandlerFunc func(*fasthttp.RequestCtx)
type Prometheus struct {
reqCnt *prometheus.CounterVec
reqDur *prometheus.HistogramVec
reqSize, respSize prometheus.Summary
MetricsPath string
}
func NewPrometheus(subsystem string) *Prometheus {
p := &Prometheus{
MetricsPath: defaultMetricPath,
}
p.registerMetrics(subsystem)
return p
}
func prometheusHandler() fasthttp.RequestHandler {
return fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())
}
func (p *Prometheus) WrapHandler(r *router.Router) fasthttp.RequestHandler {
// Setting prometheus metrics handler
r.GET(p.MetricsPath, prometheusHandler())
return func(ctx *fasthttp.RequestCtx) {
if string(ctx.Request.URI().Path()) == defaultMetricPath {
r.Handler(ctx)
return
}
reqSize := make(chan int)
frc := acquireRequestFromPool()
ctx.Request.CopyTo(frc)
go computeApproximateRequestSize(frc, reqSize)
start := time.Now()
r.Handler(ctx)
status := strconv.Itoa(ctx.Response.StatusCode())
elapsed := float64(time.Since(start)) / float64(time.Second)
respSize := float64(len(ctx.Response.Body()))
p.reqDur.WithLabelValues(status).Observe(elapsed)
p.reqCnt.WithLabelValues(status, string(ctx.Method())).Inc()
p.reqSize.Observe(float64(<-reqSize))
p.respSize.Observe(respSize)
}
}
// Idea is from https://github.com/DanielHeckrath/gin-prometheus/blob/master/gin_prometheus.go and https://github.com/zsais/go-gin-prometheus/blob/master/middleware.go
func computeApproximateRequestSize(ctx *fasthttp.Request, out chan int) {
s := 0
if ctx.URI() != nil {
s += len(ctx.URI().Path())
s += len(ctx.URI().Host())
}
s += len(ctx.Header.Method())
s += len("HTTP/1.1")
ctx.Header.VisitAll(func(key, value []byte) {
if string(key) != "Host" {
s += len(key) + len(value)
}
})
if ctx.Header.ContentLength() != -1 {
s += ctx.Header.ContentLength()
}
out <- s
}
func (p *Prometheus) registerMetrics(subsystem string) {
RequestDurationBucket := []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 15, 20, 30, 40, 50, 60}
p.reqCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: subsystem,
Name: "requests_total",
Help: "The HTTP request counts processed.",
},
[]string{"code", "method"},
)
p.reqDur = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "The HTTP request duration in seconds.",
Buckets: RequestDurationBucket,
},
[]string{"code"},
)
p.reqSize = prometheus.NewSummary(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "request_size_bytes",
Help: "The HTTP request sizes in bytes.",
},
)
p.respSize = prometheus.NewSummary(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "response_size_bytes",
Help: "The HTTP response sizes in bytes.",
},
)
prometheus.MustRegister(p.reqCnt, p.reqDur, p.reqSize, p.respSize)
}
func acquireRequestFromPool() *fasthttp.Request {
rp := requestHandlerPool.Get()
if rp == nil {
return new(fasthttp.Request)
}
frc := rp.(*fasthttp.Request)
return frc
}