-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrictest_test.go
339 lines (260 loc) · 7.8 KB
/
metrictest_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
package metrictest
import (
"fmt"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
counter = prometheus.NewCounter(prometheus.CounterOpts{Name: "test_counter"})
counterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "test_counter_vec",
}, []string{"hero", "villain"})
gauge = prometheus.NewGauge(prometheus.GaugeOpts{Name: "test_gauge"})
gaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "test_gauge_vec",
}, []string{"hero", "villain"})
histogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "test_histogram",
})
histogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "test_histogram",
}, []string{"hero", "villain"})
summary = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "test_summary",
})
summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "test_summary",
}, []string{"hero", "villain"})
)
func reset() {
counterVec.Reset()
}
type recordingT []string
func (t *recordingT) Errorf(format string, args ...interface{}) {
*t = append(*t, fmt.Sprintf(format, args...))
}
func TestAssertCounter(t *testing.T) {
reset()
recT := recordingT{}
counter.Add(10)
AssertCounter(&recT, 10, counter)
require.Len(t, recT, 0)
AssertCounter(&recT, 1, counter)
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Not equal:
expected: 1
actual : 10
`)
}
func TestAssertCounterVec(t *testing.T) {
reset()
counterVec.WithLabelValues("batman", "joker").Add(2)
counterVec.WithLabelValues("superman", "lex luthor").Add(100)
recT := recordingT{}
AssertCounterVec(&recT, 2, counterVec, "batman", "joker")
AssertCounterVec(&recT, 100, counterVec, "superman", "lex luthor")
require.Len(t, recT, 0)
AssertCounterVec(&recT, 1, counterVec, "batman", "joker")
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Not equal:
expected: 1
actual : 2
metric:
# HELP test_counter_vec
# TYPE test_counter_vec counter
test_counter_vec{hero="batman",villain="joker"} 2
test_counter_vec{hero="superman",villain="lex luthor"} 100
`)
}
func TestAssertCounterVecZero(t *testing.T) {
reset()
counterVec.WithLabelValues("batman", "joker").Add(2)
recT := recordingT{}
AssertCounterVec(&recT, 0, counterVec, "foo", "bar")
require.Len(t, recT, 0)
}
func TestAssertGauge(t *testing.T) {
reset()
recT := recordingT{}
gauge.Set(10)
AssertGauge(&recT, 10, gauge)
require.Len(t, recT, 0)
AssertGauge(&recT, 1, gauge)
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Not equal:
expected: 1
actual : 10
`)
}
func TestAssertGaugeVec(t *testing.T) {
reset()
gaugeVec.WithLabelValues("batman", "joker").Add(2)
gaugeVec.WithLabelValues("superman", "lex luthor").Add(100)
recT := recordingT{}
AssertGaugeVec(&recT, 2, gaugeVec, "batman", "joker")
AssertGaugeVec(&recT, 100, gaugeVec, "superman", "lex luthor")
require.Len(t, recT, 0)
AssertGaugeVec(&recT, 1, gaugeVec, "batman", "joker")
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Not equal:
expected: 1
actual : 2
metric:
# HELP test_gauge_vec
# TYPE test_gauge_vec gauge
test_gauge_vec{hero="batman",villain="joker"} 2
test_gauge_vec{hero="superman",villain="lex luthor"} 100
`)
}
func TestAssertGaugeVecZero(t *testing.T) {
reset()
gaugeVec.WithLabelValues("batman", "joker").Add(2)
recT := recordingT{}
AssertGaugeVec(&recT, 0, gaugeVec, "foo", "bar")
require.Len(t, recT, 0)
}
func TestAssertHistogramSamples(t *testing.T) {
reset()
histogram.Observe(10)
histogram.Observe(20)
recT := recordingT{}
AssertHistogramSamples(&recT, 2, 30, histogram)
require.Len(t, recT, 0)
AssertHistogramSamples(&recT, 1, 30, histogram)
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Sample count not equal:
expected: 1
actual : 2
`)
recT = recordingT{}
AssertHistogramSamples(&recT, 2, 42, histogram)
requireStringEqual(t, recT[0], `
Sample sum not equal:
expected: 42
actual : 30
`)
}
func TestAssertHistogramVecSamples(t *testing.T) {
reset()
histogramVec.WithLabelValues("batman", "joker").Observe(10)
histogramVec.WithLabelValues("batman", "joker").Observe(20)
histogramVec.WithLabelValues("superman", "lex luthor").Observe(10)
recT := recordingT{}
AssertHistogramVecSamples(&recT, 2, 30, histogramVec, "batman", "joker")
require.Len(t, recT, 0)
AssertHistogramVecSamples(&recT, 1, 30, histogramVec, "batman", "joker")
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Sample count not equal:
expected: 1
actual : 2
metric:
test_histogram_sum{hero="batman",villain="joker"} 30
test_histogram_count{hero="batman",villain="joker"} 2
test_histogram_sum{hero="superman",villain="lex luthor"} 10
test_histogram_count{hero="superman",villain="lex luthor"} 1`)
recT = recordingT{}
AssertHistogramVecSamples(&recT, 2, 42, histogramVec, "batman", "joker")
requireStringEqual(t, recT[0], `
Sample sum not equal:
expected: 42
actual : 30
metric:
test_histogram_sum{hero="batman",villain="joker"} 30
test_histogram_count{hero="batman",villain="joker"} 2
test_histogram_sum{hero="superman",villain="lex luthor"} 10
test_histogram_count{hero="superman",villain="lex luthor"} 1
`)
}
func TestAssertHistogramVecSamplesZero(t *testing.T) {
reset()
histogramVec.WithLabelValues("batman", "joker").Observe(10)
recT := recordingT{}
AssertHistogramVecSamples(&recT, 0, 0, histogramVec, "foo", "bar")
require.Len(t, recT, 0)
}
func TestAssertSummarySamples(t *testing.T) {
reset()
summary.Observe(10)
summary.Observe(20)
recT := recordingT{}
AssertSummarySamples(&recT, 2, 30, summary)
require.Len(t, recT, 0)
AssertSummarySamples(&recT, 1, 30, summary)
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Sample count not equal:
expected: 1
actual : 2
`)
recT = recordingT{}
AssertHistogramSamples(&recT, 2, 42, histogram)
requireStringEqual(t, recT[0], `
Sample sum not equal:
expected: 42
actual : 30
`)
}
func TestAssertSummaryVecSamples(t *testing.T) {
reset()
summaryVec.WithLabelValues("batman", "joker").Observe(10)
summaryVec.WithLabelValues("batman", "joker").Observe(20)
summaryVec.WithLabelValues("superman", "lex luthor").Observe(10)
recT := recordingT{}
AssertSummaryVecSamples(&recT, 2, 30, summaryVec, "batman", "joker")
require.Len(t, recT, 0)
AssertSummaryVecSamples(&recT, 1, 30, summaryVec, "batman", "joker")
require.Len(t, recT, 1)
requireStringEqual(t, recT[0], `
Sample count not equal:
expected: 1
actual : 2
metric:
test_summary_sum{hero="batman",villain="joker"} 30
test_summary_count{hero="batman",villain="joker"} 2
test_summary_sum{hero="superman",villain="lex luthor"} 10
test_summary_count{hero="superman",villain="lex luthor"} 1
`)
recT = recordingT{}
AssertSummaryVecSamples(&recT, 2, 42, summaryVec, "batman", "joker")
requireStringEqual(t, recT[0], `
Sample sum not equal:
expected: 42
actual : 30
metric:
test_summary_sum{hero="batman",villain="joker"} 30
test_summary_count{hero="batman",villain="joker"} 2
test_summary_sum{hero="superman",villain="lex luthor"} 10
test_summary_count{hero="superman",villain="lex luthor"} 1
`)
}
func TestAssertSummaryVecSamplesZero(t *testing.T) {
reset()
summaryVec.WithLabelValues("batman", "joker").Observe(10)
recT := recordingT{}
AssertSummaryVecSamples(&recT, 0, 0, summaryVec, "foo", "bar")
require.Len(t, recT, 0)
}
func TestToExpVar(t *testing.T) {
reset()
counterVec.WithLabelValues("batman", "joker").Add(2)
counterVec.WithLabelValues("superman", "lex luthor").Add(100)
out, err := ToExpVar(counterVec)
require.NoError(t, err)
assert.Equal(t, `# HELP test_counter_vec
# TYPE test_counter_vec counter
test_counter_vec{hero="batman",villain="joker"} 2
test_counter_vec{hero="superman",villain="lex luthor"} 100
`, out)
}
func requireStringEqual(t *testing.T, subject, contains string) {
require.Equal(t, strings.TrimSpace(subject), strings.TrimSpace(contains))
}