Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/pkg/metrics/reporter_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package metrics
6
7
import (
8
"testing"
9
10
"github.com/google/go-cmp/cmp"
11
"github.com/prometheus/client_golang/prometheus"
12
dto "github.com/prometheus/client_model/go"
13
)
14
15
func TestHistogram(t *testing.T) {
16
reporter := NewGrpcMetricsReporter("test.io")
17
type testReq struct {
18
Name string
19
Labels map[string]string
20
Count uint64
21
Sum float64
22
Buckets []uint64
23
}
24
type expectation struct {
25
Requests []*testReq
26
Unexpected []string
27
}
28
var actual *expectation
29
reporter.addHistogram = func(name string, labels map[string]string, count uint64, sum float64, buckets []uint64) {
30
if actual == nil {
31
actual = &expectation{}
32
}
33
actual.Requests = append(actual.Requests, &testReq{name, labels, count, sum, buckets})
34
}
35
reporter.onUnexpected = func(family *dto.MetricFamily) {
36
if actual == nil {
37
actual = &expectation{}
38
}
39
actual.Unexpected = append(actual.Unexpected, family.GetName())
40
}
41
assertDiff := func(expected *expectation) string {
42
actual = nil
43
reporter.gather()
44
return cmp.Diff(expected, actual)
45
}
46
47
if diff := assertDiff(nil); diff != "" {
48
t.Errorf("unexpected output (-want +got):\n%s", diff)
49
}
50
51
histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{
52
Name: "grpc_server_handling_seconds",
53
Buckets: []float64{.005, .01, .025},
54
}, []string{"grpc_method"})
55
err := reporter.Registry.Register(histogram)
56
if err != nil {
57
t.Fatal(err)
58
}
59
if diff := assertDiff(nil); diff != "" {
60
t.Errorf("unexpected output (-want +got):\n%s", diff)
61
}
62
63
histogram.WithLabelValues("foo").Observe(.004)
64
if diff := assertDiff(&expectation{
65
Requests: []*testReq{{
66
Name: "grpc_server_handling_seconds",
67
Labels: map[string]string{"grpc_method": "foo"},
68
Count: 1,
69
Sum: 0.004,
70
Buckets: []uint64{1, 1, 1},
71
}},
72
}); diff != "" {
73
t.Errorf("unexpected output (-want +got):\n%s", diff)
74
}
75
76
histogram.WithLabelValues("foo").Observe(.001)
77
histogram.WithLabelValues("foo").Observe(.002)
78
histogram.WithLabelValues("foo").Observe(.003)
79
histogram.WithLabelValues("foo").Observe(.004)
80
histogram.WithLabelValues("foo").Observe(.005)
81
if diff := assertDiff(&expectation{
82
Requests: []*testReq{{
83
Name: "grpc_server_handling_seconds",
84
Labels: map[string]string{"grpc_method": "foo"},
85
Count: 5,
86
Sum: 0.015,
87
Buckets: []uint64{5, 5, 5},
88
}},
89
}); diff != "" {
90
t.Errorf("unexpected output (-want +got):\n%s", diff)
91
}
92
93
histogram.WithLabelValues("foo").Observe(.009)
94
if diff := assertDiff(&expectation{
95
Requests: []*testReq{{
96
Name: "grpc_server_handling_seconds",
97
Labels: map[string]string{"grpc_method": "foo"},
98
Count: 1,
99
Sum: 0.008999999999999998,
100
Buckets: []uint64{0, 1, 1},
101
}},
102
}); diff != "" {
103
t.Errorf("unexpected output (-want +got):\n%s", diff)
104
}
105
106
histogram.WithLabelValues("foo").Observe(.006)
107
histogram.WithLabelValues("foo").Observe(.007)
108
histogram.WithLabelValues("foo").Observe(.008)
109
histogram.WithLabelValues("foo").Observe(.009)
110
histogram.WithLabelValues("foo").Observe(.01)
111
if diff := assertDiff(&expectation{
112
Requests: []*testReq{{
113
Name: "grpc_server_handling_seconds",
114
Labels: map[string]string{"grpc_method": "foo"},
115
Count: 5,
116
Sum: 0.039999999999999994,
117
Buckets: []uint64{0, 5, 5},
118
}},
119
}); diff != "" {
120
t.Errorf("unexpected output (-want +got):\n%s", diff)
121
}
122
123
histogram.WithLabelValues("foo").Observe(.001)
124
histogram.WithLabelValues("foo").Observe(.002)
125
histogram.WithLabelValues("foo").Observe(.003)
126
histogram.WithLabelValues("foo").Observe(.004)
127
histogram.WithLabelValues("foo").Observe(.005)
128
histogram.WithLabelValues("foo").Observe(.006)
129
histogram.WithLabelValues("foo").Observe(.007)
130
histogram.WithLabelValues("foo").Observe(.008)
131
histogram.WithLabelValues("foo").Observe(.009)
132
histogram.WithLabelValues("foo").Observe(.01)
133
if diff := assertDiff(&expectation{
134
Requests: []*testReq{{
135
Name: "grpc_server_handling_seconds",
136
Labels: map[string]string{"grpc_method": "foo"},
137
Count: 10,
138
Sum: 0.05500000000000001,
139
Buckets: []uint64{5, 10, 10},
140
}},
141
}); diff != "" {
142
t.Errorf("unexpected output (-want +got):\n%s", diff)
143
}
144
145
histogram.WithLabelValues("foo").Observe(.02)
146
if diff := assertDiff(&expectation{
147
Requests: []*testReq{{
148
Name: "grpc_server_handling_seconds",
149
Labels: map[string]string{"grpc_method": "foo"},
150
Count: 1,
151
Sum: 0.01999999999999999,
152
Buckets: []uint64{0, 0, 1},
153
}},
154
}); diff != "" {
155
t.Errorf("unexpected output (-want +got):\n%s", diff)
156
}
157
158
histogram.WithLabelValues("foo").Observe(.021)
159
histogram.WithLabelValues("foo").Observe(.022)
160
histogram.WithLabelValues("foo").Observe(.023)
161
histogram.WithLabelValues("foo").Observe(.024)
162
histogram.WithLabelValues("foo").Observe(.025)
163
if diff := assertDiff(&expectation{
164
Requests: []*testReq{{
165
Name: "grpc_server_handling_seconds",
166
Labels: map[string]string{"grpc_method": "foo"},
167
Count: 5,
168
Sum: 0.11499999999999996,
169
Buckets: []uint64{0, 0, 5},
170
}},
171
}); diff != "" {
172
t.Errorf("unexpected output (-want +got):\n%s", diff)
173
}
174
175
histogram.WithLabelValues("foo").Observe(.001)
176
histogram.WithLabelValues("foo").Observe(.002)
177
histogram.WithLabelValues("foo").Observe(.003)
178
histogram.WithLabelValues("foo").Observe(.004)
179
histogram.WithLabelValues("foo").Observe(.005)
180
histogram.WithLabelValues("foo").Observe(.006)
181
histogram.WithLabelValues("foo").Observe(.007)
182
histogram.WithLabelValues("foo").Observe(.008)
183
histogram.WithLabelValues("foo").Observe(.009)
184
histogram.WithLabelValues("foo").Observe(.01)
185
histogram.WithLabelValues("foo").Observe(.021)
186
histogram.WithLabelValues("foo").Observe(.022)
187
histogram.WithLabelValues("foo").Observe(.023)
188
histogram.WithLabelValues("foo").Observe(.024)
189
histogram.WithLabelValues("foo").Observe(.025)
190
if diff := assertDiff(&expectation{
191
Requests: []*testReq{{
192
Name: "grpc_server_handling_seconds",
193
Labels: map[string]string{"grpc_method": "foo"},
194
Count: 15,
195
Sum: 0.17000000000000015,
196
Buckets: []uint64{5, 10, 15},
197
}},
198
}); diff != "" {
199
t.Errorf("unexpected output (-want +got):\n%s", diff)
200
}
201
202
histogram.WithLabelValues("foo").Observe(.03)
203
if diff := assertDiff(&expectation{
204
Requests: []*testReq{{
205
Name: "grpc_server_handling_seconds",
206
Labels: map[string]string{"grpc_method": "foo"},
207
Count: 1,
208
Sum: 0.02999999999999997,
209
Buckets: []uint64{0, 0, 0},
210
}},
211
}); diff != "" {
212
t.Errorf("unexpected output (-want +got):\n%s", diff)
213
}
214
215
histogram.WithLabelValues("foo").Observe(.04)
216
histogram.WithLabelValues("foo").Observe(.05)
217
histogram.WithLabelValues("foo").Observe(.06)
218
histogram.WithLabelValues("foo").Observe(.07)
219
histogram.WithLabelValues("foo").Observe(.08)
220
if diff := assertDiff(&expectation{
221
Requests: []*testReq{{
222
Name: "grpc_server_handling_seconds",
223
Labels: map[string]string{"grpc_method": "foo"},
224
Count: 5,
225
Sum: 0.30000000000000004,
226
Buckets: []uint64{0, 0, 0},
227
}},
228
}); diff != "" {
229
t.Errorf("unexpected output (-want +got):\n%s", diff)
230
}
231
232
histogram.WithLabelValues("foo").Observe(.001)
233
histogram.WithLabelValues("foo").Observe(.002)
234
histogram.WithLabelValues("foo").Observe(.003)
235
histogram.WithLabelValues("foo").Observe(.004)
236
histogram.WithLabelValues("foo").Observe(.005)
237
histogram.WithLabelValues("foo").Observe(.006)
238
histogram.WithLabelValues("foo").Observe(.007)
239
histogram.WithLabelValues("foo").Observe(.008)
240
histogram.WithLabelValues("foo").Observe(.009)
241
histogram.WithLabelValues("foo").Observe(.01)
242
histogram.WithLabelValues("foo").Observe(.021)
243
histogram.WithLabelValues("foo").Observe(.022)
244
histogram.WithLabelValues("foo").Observe(.023)
245
histogram.WithLabelValues("foo").Observe(.024)
246
histogram.WithLabelValues("foo").Observe(.025)
247
histogram.WithLabelValues("foo").Observe(.04)
248
histogram.WithLabelValues("foo").Observe(.05)
249
histogram.WithLabelValues("foo").Observe(.06)
250
histogram.WithLabelValues("foo").Observe(.07)
251
histogram.WithLabelValues("foo").Observe(.08)
252
if diff := assertDiff(&expectation{
253
Requests: []*testReq{{
254
Name: "grpc_server_handling_seconds",
255
Labels: map[string]string{"grpc_method": "foo"},
256
Count: 20,
257
Sum: 0.4700000000000003,
258
Buckets: []uint64{5, 10, 15},
259
}},
260
}); diff != "" {
261
t.Errorf("unexpected output (-want +got):\n%s", diff)
262
}
263
}
264
265
func TestCounters(t *testing.T) {
266
reporter := NewGrpcMetricsReporter("test.io")
267
type testReq struct {
268
Name string
269
Labels map[string]string
270
Value uint64
271
}
272
type expectation struct {
273
Requests []*testReq
274
Unexpected []string
275
}
276
var actual *expectation
277
reporter.addCounter = func(name string, labels map[string]string, value uint64) {
278
if actual == nil {
279
actual = &expectation{}
280
}
281
actual.Requests = append(actual.Requests, &testReq{name, labels, value})
282
}
283
reporter.onUnexpected = func(family *dto.MetricFamily) {
284
if actual == nil {
285
actual = &expectation{}
286
}
287
actual.Unexpected = append(actual.Unexpected, family.GetName())
288
}
289
assertDiff := func(expected *expectation) string {
290
actual = nil
291
reporter.gather()
292
return cmp.Diff(expected, actual)
293
}
294
295
if diff := assertDiff(nil); diff != "" {
296
t.Errorf("unexpected output (-want +got):\n%s", diff)
297
}
298
299
expectedCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
300
Name: "grpc_server_handled_total",
301
}, []string{"grpc_method"})
302
err := reporter.Registry.Register(expectedCounter)
303
if err != nil {
304
t.Fatal(err)
305
}
306
if diff := assertDiff(nil); diff != "" {
307
t.Errorf("unexpected output (-want +got):\n%s", diff)
308
}
309
310
expectedCounter.WithLabelValues("foo").Add(10)
311
if diff := assertDiff(&expectation{
312
Requests: []*testReq{{
313
Name: "grpc_server_handled_total",
314
Labels: map[string]string{"grpc_method": "foo"},
315
Value: 10,
316
}},
317
}); diff != "" {
318
t.Errorf("unexpected output (-want +got):\n%s", diff)
319
}
320
321
expectedCounter.WithLabelValues("foo").Add(30)
322
if diff := assertDiff(&expectation{
323
Requests: []*testReq{{
324
Name: "grpc_server_handled_total",
325
Labels: map[string]string{"grpc_method": "foo"},
326
Value: 30,
327
}},
328
}); diff != "" {
329
t.Errorf("unexpected output (-want +got):\n%s", diff)
330
}
331
332
expectedCounter.WithLabelValues("foo").Add(10)
333
expectedCounter.WithLabelValues("bar").Add(20)
334
if diff := assertDiff(&expectation{
335
Requests: []*testReq{{
336
Name: "grpc_server_handled_total",
337
Labels: map[string]string{"grpc_method": "bar"},
338
Value: 20,
339
}, {
340
Name: "grpc_server_handled_total",
341
Labels: map[string]string{"grpc_method": "foo"},
342
Value: 10,
343
}},
344
}); diff != "" {
345
t.Errorf("unexpected output (-want +got):\n%s", diff)
346
}
347
348
unexpectedCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
349
Name: "some_unexpected_counter_total",
350
}, []string{"grpc_method"})
351
err = reporter.Registry.Register(unexpectedCounter)
352
if err != nil {
353
t.Fatal(err)
354
}
355
if diff := assertDiff(nil); diff != "" {
356
t.Errorf("unexpected output (-want +got):\n%s", diff)
357
}
358
359
unexpectedCounter.WithLabelValues("bazz").Add(20)
360
if diff := assertDiff(&expectation{
361
Unexpected: []string{"some_unexpected_counter_total"},
362
}); diff != "" {
363
t.Errorf("unexpected output (-want +got):\n%s", diff)
364
}
365
366
unexpectedCounter.WithLabelValues("bazz").Add(30)
367
if diff := assertDiff(nil); diff != "" {
368
t.Errorf("unexpected output (-want +got):\n%s", diff)
369
}
370
}
371
372