-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics_test.go
378 lines (302 loc) · 10.5 KB
/
metrics_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package gotoprom_test
import (
"math"
"strings"
"testing"
"time"
"github.com/cabify/gotoprom"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
func Test_InitHappyCase(t *testing.T) {
type labels struct {
Region string `label:"region"`
Status string `label:"status"`
DuckType string `label:"duck_type"`
}
var metrics struct {
HTTPRequestTime func(labels) prometheus.Histogram `name:"http_request_count" help:"Time taken to serve a HTTP request" buckets:"0.001,0.005,0.01,0.05,0.1,0.5,1,5,10"`
DuvelsEmptied func(labels) prometheus.Counter `name:"duvels_emptied" help:"Delirium floor sweep count"`
RubberDuckInTherapy func(labels) prometheus.Gauge `name:"rubber_ducks_in_therapy" help:"Number of rubber ducks who need help after some intense coding"`
BrokenDeploysAccomplished func(labels) prometheus.Summary `name:"broken_deploys_accomplished" help:"Number of deploys that broke production" objectives:"0.5,0.9,0.99"`
NoLabels func() prometheus.Counter `name:"no_labels" help:"Metric without labels"`
}
gotoprom.MustInit(&metrics, "delirium")
// Elsewhere in the code
theseLabels := labels{
Region: "bruxelles",
Status: "recovering",
DuckType: "definitely_rubber",
}
metrics.HTTPRequestTime(theseLabels).Observe(time.Second.Seconds())
metrics.DuvelsEmptied(theseLabels).Add(4)
metrics.RubberDuckInTherapy(theseLabels).Set(12)
metrics.BrokenDeploysAccomplished(theseLabels).Observe(20)
metrics.NoLabels().Add(288.88)
}
func Test_NestedMetrics(t *testing.T) {
type testLabels struct {
TestLabel string `label:"test_label"`
}
var metrics struct {
Server struct {
Hits func(testLabels) prometheus.Counter `name:"hits_total" help:"bla bla"`
} `namespace:"server"`
Client struct {
Requests func(testLabels) prometheus.Counter `name:"requests_total" help:"cuac cuac"`
} `namespace:"client"`
MemoryConsumption func(testLabels) prometheus.Gauge `name:"memory_consumption_bytes" help:"wololo"`
}
gotoprom.MustInit(&metrics, "testservice")
metrics.Server.Hits(testLabels{}).Add(1.0)
metrics.Client.Requests(testLabels{}).Add(2.0)
metrics.MemoryConsumption(testLabels{}).Set(3.0)
mfs, err := prometheus.DefaultGatherer.Gather()
assert.Nil(t, err)
reportedMetrics := map[string]struct{}{}
for _, m := range mfs {
for _, mm := range m.Metric {
for _, l := range mm.Label {
if *l.Name == "test_label" {
reportedMetrics[*m.Name] = struct{}{}
}
}
}
}
assert.Equal(t, map[string]struct{}{
"testservice_server_hits_total": {},
"testservice_client_requests_total": {},
"testservice_memory_consumption_bytes": {},
}, reportedMetrics)
}
func Test_EmbeddedLabels(t *testing.T) {
type commonLabels struct {
CommonValue string `label:"common_value"`
}
type specificLabels struct {
commonLabels
SpecificValue string `label:"specific_value"`
}
var metrics struct {
WithLabels func(specificLabels) prometheus.Counter `name:"with_labels" help:"Some metric with labels"`
}
gotoprom.MustInit(&metrics, "namespace")
metrics.WithLabels(specificLabels{
commonLabels: commonLabels{CommonValue: "common"},
SpecificValue: "specific",
}).Add(42.0)
reportedLabels := retrieveReportedLabels(t, "namespace_with_labels")
assert.Equal(t, map[string]string{
"common_value": "common",
"specific_value": "specific",
}, reportedLabels)
}
func Test_LabelsWithBooleans(t *testing.T) {
type labelsWithBools struct {
StringValue string `label:"string_value"`
BooleanValue bool `label:"bool_value"`
}
var metrics struct {
WithLabels func(labelsWithBools) prometheus.Histogram `name:"with_booleans" help:"Parse booleans as strings" buckets:""`
}
gotoprom.MustInit(&metrics, "testbooleans")
metrics.WithLabels(labelsWithBools{
StringValue: "string",
BooleanValue: false,
}).Observe(288.0)
reportedLabels := retrieveReportedLabels(t, "testbooleans_with_booleans")
assert.Equal(t, map[string]string{
"string_value": "string",
"bool_value": "false",
}, reportedLabels)
}
func Test_LabelsWithInts(t *testing.T) {
type labelsWithInts struct {
StringValue string `label:"string_value"`
IntValue int `label:"int_value"`
IntValue8 int8 `label:"int8_value"`
IntValue16 int16 `label:"int16_value"`
IntValue32 int32 `label:"int32_value"`
IntValue64 int64 `label:"int64_value"`
}
var metrics struct {
WithLabels func(labelsWithInts) prometheus.Histogram `name:"with_ints" help:"Parse ints as strings" buckets:""`
}
gotoprom.MustInit(&metrics, "testints")
metrics.WithLabels(labelsWithInts{
StringValue: "string",
IntValue: 10,
IntValue8: 20,
IntValue16: 30,
IntValue32: 40,
IntValue64: 50,
}).Observe(288.0)
reportedLabels := retrieveReportedLabels(t, "testints_with_ints")
assert.Equal(t, map[string]string{
"string_value": "string",
"int_value": "10",
"int8_value": "20",
"int16_value": "30",
"int32_value": "40",
"int64_value": "50",
}, reportedLabels)
}
func Test_LabelsWithUints(t *testing.T) {
type labelsWithUints struct {
StringValue string `label:"string_value"`
UintValue uint `label:"uint_value"`
UintValue8 uint8 `label:"uint8_value"`
UintValue16 uint16 `label:"uint16_value"`
UintValue32 uint32 `label:"uint32_value"`
UintValue64 uint64 `label:"uint64_value"`
}
var metrics struct {
WithLabels func(labelsWithUints) prometheus.Histogram `name:"with_uints" help:"Parse uints as strings" buckets:""`
}
gotoprom.MustInit(&metrics, "testuints")
metrics.WithLabels(labelsWithUints{
StringValue: "string",
UintValue: 10,
UintValue8: 20,
UintValue16: 30,
UintValue32: 40,
UintValue64: 50,
}).Observe(288.0)
reportedLabels := retrieveReportedLabels(t, "testuints_with_uints")
assert.Equal(t, map[string]string{
"string_value": "string",
"uint_value": "10",
"uint8_value": "20",
"uint16_value": "30",
"uint32_value": "40",
"uint64_value": "50",
}, reportedLabels)
}
func Test_DefaultLabelValues(t *testing.T) {
type labelsWithEmptyValues struct {
StringWithEmpty string `label:"string_with_default" default:"none"`
StringWithoutEmpty string `label:"string_without_default"`
}
var metrics struct {
WithLabels func(labelsWithEmptyValues) prometheus.Histogram `name:"with_labels" help:"Assign default values" buckets:"10,20,30"`
}
gotoprom.MustInit(&metrics, "testdefault")
metrics.WithLabels(labelsWithEmptyValues{}).Observe(288.0)
reportedLabels := retrieveReportedLabels(t, "testdefault_with_labels")
assert.Equal(t, map[string]string{
"string_with_default": "none",
"string_without_default": "",
}, reportedLabels)
}
func Test_HistogramWithUnsupportedBuckets(t *testing.T) {
var metrics struct {
Histogram func() prometheus.Histogram `name:"with_broken_buckets" help:"Wrong buckets" buckets:"0.005, whatever"`
}
err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
}
func Test_WrongLabels(t *testing.T) {
t.Run("unsupported fields", func(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
FloatValue float64 `label:"float_value"`
}
var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Can't parse float labels"`
}
err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})
t.Run("no label tag", func(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
StringWithoutTag string
}
var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Tag is missing"`
}
err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})
t.Run("same label registered twice", func(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
SameStringValue string `label:"string_value"`
}
var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Same string value"`
}
err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})
t.Run("embedded struct is wrong", func(t *testing.T) {
type embeddedStructLabels struct {
SameStringValue string `label:"string_value"`
}
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
embeddedStructLabels
}
var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Same string value in the embedded struct"`
}
err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})
t.Run("labels are not a struct", func(t *testing.T) {
type labels string
var metrics struct {
WithLabels func(labels) prometheus.Counter `name:"with_unsupported_fields" help:"Labels are not a struct"`
}
err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})
}
func Test_SummaryWithSpecifiedMaxAge(t *testing.T) {
summaryHelp := "Uses default value for max age"
var metrics struct {
Summary func() prometheus.Summary `name:"without_max_age" help:"Uses default value for max age" max_age:"1s" objectives:".5,.9,.99"`
}
err := gotoprom.Init(&metrics, "test")
assert.NoError(t, err)
metrics.Summary().Observe(1.0)
metrics.Summary().Observe(2.0)
metrics.Summary().Observe(3.0)
nl := "\n"
expectedUnexpired := "" +
`# HELP test_without_max_age ` + summaryHelp + nl +
`# TYPE test_without_max_age summary` + nl +
`test_without_max_age{quantile="0.5"} 2` + nl +
`test_without_max_age{quantile="0.9"} 3` + nl +
`test_without_max_age{quantile="0.99"} 3` + nl +
`test_without_max_age_sum{} 6` + nl +
`test_without_max_age_count{} 3` + nl
err = testutil.GatherAndCompare(prometheus.DefaultGatherer, strings.NewReader(expectedUnexpired), "test_without_max_age")
assert.NoError(t, err)
time.Sleep(time.Second * 2)
mfs, err := prometheus.DefaultGatherer.Gather()
assert.NoError(t, err)
for _, m := range mfs {
if *m.Name == "test_without_max_age" {
for _, mm := range m.Metric {
assert.True(t, math.IsNaN(*mm.GetSummary().Quantile[1].Value))
}
}
}
}
func retrieveReportedLabels(t *testing.T, metric string) map[string]string {
mfs, err := prometheus.DefaultGatherer.Gather()
assert.Nil(t, err)
reportedLabels := map[string]string{}
for _, m := range mfs {
if *m.Name == metric {
for _, mm := range m.Metric {
for _, l := range mm.Label {
reportedLabels[*l.Name] = *l.Value
}
}
}
}
return reportedLabels
}