This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware_test.go
214 lines (197 loc) · 9.96 KB
/
middleware_test.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
package middleware_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
prommiddleware "github.com/slok/go-prometheus-middleware"
)
func getFakeHandler(statusCode int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
})
}
func TestMiddlewareHandler(t *testing.T) {
tests := []struct {
name string
config prommiddleware.Config
requests func(h http.Handler)
handlerID string
statusCode int
expMetrics []string
}{
{
name: "default configuration without handlerID should measure without prefix with the default buckets and the URL as handler",
config: prommiddleware.Config{},
handlerID: "",
statusCode: 403,
requests: func(h http.Handler) {
r := httptest.NewRequest("GET", "/test", nil)
r2 := httptest.NewRequest("POST", "/test2", nil)
h.ServeHTTP(httptest.NewRecorder(), r)
h.ServeHTTP(httptest.NewRecorder(), r2)
},
expMetrics: []string{
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.005"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.01"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.025"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.05"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.1"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.25"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="0.5"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="1"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="2.5"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="5"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="10"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test",method="GET",le="+Inf"} 1`,
`http_request_duration_seconds_count{code="403",handler="/test",method="GET"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.005"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.01"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.025"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.05"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.1"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.25"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="0.5"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="1"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="2.5"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="5"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="10"} 1`,
`http_request_duration_seconds_bucket{code="403",handler="/test2",method="POST",le="+Inf"} 1`,
`http_request_duration_seconds_count{code="403",handler="/test2",method="POST"} 1`,
},
},
{
name: "custom configuration with handlerID should measure with prefix, with the default buckets and the the same handlerID",
config: prommiddleware.Config{
Prefix: "batman",
Buckets: []float64{.5, 1, 2.5, 5, 10, 20, 40, 80, 160, 320},
},
handlerID: "bruceWayne",
statusCode: 201,
requests: func(h http.Handler) {
r := httptest.NewRequest("GET", "/test", nil)
r2 := httptest.NewRequest("POST", "/test2", nil)
h.ServeHTTP(httptest.NewRecorder(), r)
h.ServeHTTP(httptest.NewRecorder(), r2)
},
expMetrics: []string{
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="0.5"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="1"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="2.5"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="5"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="10"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="20"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="40"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="80"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="160"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="320"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="GET",le="+Inf"} 1`,
`batman_http_request_duration_seconds_count{code="201",handler="bruceWayne",method="GET"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="0.5"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="1"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="2.5"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="5"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="10"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="20"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="40"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="80"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="160"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="320"} 1`,
`batman_http_request_duration_seconds_bucket{code="201",handler="bruceWayne",method="POST",le="+Inf"} 1`,
`batman_http_request_duration_seconds_count{code="201",handler="bruceWayne",method="POST"} 1`,
},
},
{
name: "default configuration with grouped status code should group the code label.",
config: prommiddleware.Config{
GroupedStatus: true,
},
handlerID: "",
statusCode: 301,
requests: func(h http.Handler) {
r := httptest.NewRequest("GET", "/test", nil)
h.ServeHTTP(httptest.NewRecorder(), r)
},
expMetrics: []string{
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.005"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.01"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.025"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.05"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.1"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.25"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="0.5"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="1"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="2.5"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="5"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="10"} 1`,
`http_request_duration_seconds_bucket{code="3xx",handler="/test",method="GET",le="+Inf"} 1`,
`http_request_duration_seconds_count{code="3xx",handler="/test",method="GET"} 1`,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
reg := prometheus.NewRegistry()
m := prommiddleware.New(test.config, reg)
h := m.Handler(test.handlerID, getFakeHandler(test.statusCode))
// Make the calls to our handler.
test.requests(h)
// Get the metrics handler and serve.
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(rec, req)
resp := rec.Result()
// Check all metrics are present.
if assert.Equal(http.StatusOK, resp.StatusCode) {
body, _ := ioutil.ReadAll(resp.Body)
for _, expMetric := range test.expMetrics {
assert.Contains(string(body), expMetric, "metric not present on the result")
}
}
})
}
}
func BenchmarkMiddlewareHandler(b *testing.B) {
b.StopTimer()
benchs := []struct {
name string
handlerID string
cfg prommiddleware.Config
}{
{
name: "benchmark with default settings.",
handlerID: "",
cfg: prommiddleware.Config{},
},
{
name: "benchmark with grouped status code.",
cfg: prommiddleware.Config{
GroupedStatus: true,
},
},
{
name: "benchmark with predefined handler ID",
handlerID: "benchmark1",
cfg: prommiddleware.Config{},
},
}
for _, bench := range benchs {
b.Run(bench.name, func(b *testing.B) {
// Prepare.
reg := prometheus.NewRegistry()
m := prommiddleware.New(bench.cfg, reg)
h := m.Handler(bench.handlerID, getFakeHandler(200))
r := httptest.NewRequest("GET", "/test", nil)
// Make the requests.
for n := 0; n < b.N; n++ {
b.StartTimer()
h.ServeHTTP(httptest.NewRecorder(), r)
b.StopTimer()
}
})
}
}