forked from MarijnKoesen/prometheus-aggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector_test.go
355 lines (303 loc) · 9.09 KB
/
collector_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
package main
import (
"runtime"
"sort"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
a "github.com/stretchr/testify/assert"
)
const defaultExpiryTime = 24 * time.Hour
func Test_Collector_New(t *testing.T) {
c := newCollector(defaultExpiryTime)
a.IsType(t, &collector{}, c)
a.Equal(t, ingressQueueSize, cap(c.ingressCh))
a.NotNil(t, c.counters)
a.NotNil(t, c.gauges)
a.NotNil(t, c.histograms)
a.Equal(t, c.expiryTime, defaultExpiryTime)
}
var tfCollectorSamples = []*sample{
{
name: "name_of_1_metric_total", kind: sampleCounter,
labels: map[string]string{"service": "srvA1", "host": "hostA", "phpVersion": "5.6", "labelA": "labelValueA", "label2": "labelValue2"},
value: 1.1,
},
{
name: "name_of_2_metric_total", kind: sampleCounter,
labels: map[string]string{"service": "srvA1", "host": "hostA", "phpVersion": "5.6"},
value: 100.01,
},
{
name: "name_of_1_metric_total", kind: sampleCounter,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
value: 1000.001,
},
{
name: "name_of_2_metric_total", kind: sampleCounter,
labels: map[string]string{},
value: 10000.0001,
},
{
name: "name_of_3_metric", kind: sampleGauge,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
value: 7.3,
},
{
name: "name_of_3_metric", kind: sampleGauge,
labels: map[string]string{},
value: 17.3,
},
}
func Test_Collector_Write_Success(t *testing.T) {
c := newCollector(defaultExpiryTime)
for _, s := range tfCollectorSamples {
c.Write(s)
}
if !a.Len(t, c.ingressCh, len(tfCollectorSamples)) {
t.FailNow()
}
for i := 0; i < len(tfCollectorSamples); i++ {
a.Equal(t, tfCollectorSamples[i], <-c.ingressCh)
}
}
func Test_Collector_Write_ChannelFull(t *testing.T) {
c := &collector{}
// size of buffer is smaller than number of samples to store
bufLen := 2
c.ingressCh = make(chan *sample, bufLen)
errGot := make(chan error, len(tfCollectorSamples))
for _, s := range tfCollectorSamples {
errGot <- c.Write(s)
}
if !a.Len(t, c.ingressCh, bufLen) {
t.FailNow()
}
// check on calls which should add samples to buffer
for i := 0; i < bufLen; i++ {
a.Equal(t, tfCollectorSamples[i], <-c.ingressCh)
a.Nil(t, <-errGot)
}
// check on calls which should result in errors
for i := bufLen; i < len(tfCollectorSamples); i++ {
a.Equal(t, ErrIngressQueueFull, <-errGot)
}
}
func thInitSampleHasher(h sampleHasherFunc) func() {
sampleHasherOld := sampleHasher
sampleHasher = h
return func() {
sampleHasher = sampleHasherOld
}
}
func thCollectorProcessPopulate(c *collector, samples []*sample) {
for _, s := range samples {
c.ingressCh <- s
}
}
func thCollectorProcessSynchronise(t *testing.T, c *collector) {
c.shutdownTimeout = time.Millisecond * 100
sampleProcessingDoneCh := make(chan struct{})
// failInTestHook is used to pass failures from "process" goroutine
failInTestHook := make(chan struct{}, 1)
c.testHookProcessSampleDone = func() {
select {
case sampleProcessingDoneCh <- struct{}{}:
case <-time.After(time.Millisecond * 10):
failInTestHook <- struct{}{}
}
runtime.Gosched()
}
go c.process()
inProcessing:
for {
select {
case <-sampleProcessingDoneCh:
if len(c.ingressCh) == 0 {
break inProcessing
}
case <-failInTestHook:
t.Fatal("timeout in testHookProcessSampleDone")
case <-time.After(time.Millisecond * 10):
t.Fatal("timeout in synchronise")
}
runtime.Gosched()
}
if err := c.stop(); err != nil {
t.Fatal("timeout in shutdown")
}
}
func Test_Collector_Process_Success_NewHashes(t *testing.T) {
tests := map[string]struct {
h sampleHasherFunc
}{
"md5": {hashMD5},
"prom": {hashProm},
}
sampleHasherOld := sampleHasher
defer func() {
sampleHasher = sampleHasherOld
}()
for sym, tc := range tests {
sampleHasher = tc.h
c := newCollector(defaultExpiryTime)
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessSynchronise(t, c)
// check if the samples are converted to metrics
var hashesGot []string
for h := range c.counters {
hashesGot = append(hashesGot, h)
}
for h := range c.gauges {
hashesGot = append(hashesGot, h)
}
sort.Strings(hashesGot)
var hashesExp []string
for _, s := range tfCollectorSamples {
hashesExp = append(hashesExp, string(s.hash()))
}
sort.Strings(hashesExp)
a.Equal(t, hashesExp, hashesGot, sym)
}
}
func Test_Collector_Process_Success_Existing(t *testing.T) {
defer thInitSampleHasher(hashMD5)()
c := newCollector(defaultExpiryTime)
// duplicate to simulate adding existing samples
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessSynchronise(t, c)
// check if the samples are converted to metrics
var hashesGot []string
for h := range c.counters {
hashesGot = append(hashesGot, h)
}
for h := range c.gauges {
hashesGot = append(hashesGot, h)
}
sort.Strings(hashesGot)
var hashesExp []string
for _, s := range tfCollectorSamples {
hashesExp = append(hashesExp, string(s.hash()))
}
sort.Strings(hashesExp)
a.Equal(t, hashesExp, hashesGot)
}
func Test_Collector_Process_Success_Values(t *testing.T) {
defer thInitSampleHasher(hashMD5)()
c := newCollector(defaultExpiryTime)
// duplicate to simulate adding existing samples
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessSynchronise(t, c)
for _, s := range tfCollectorSamples {
var mm dto.Metric
switch s.kind {
case sampleCounter:
m := c.counters[string(s.hash())]
m.Counter.Write(&mm)
// samples were added 3 times
a.Equal(t, s.value*3, mm.Counter.GetValue())
case sampleGauge:
m := c.gauges[string(s.hash())]
m.Gauge.Write(&mm)
a.Equal(t, s.value, mm.Gauge.GetValue())
}
}
}
func Test_Collector_Process_Success_HistogramLinear(t *testing.T) {
s1 := sample{
name: "name_of_1_metric_seconds", kind: sampleHistogramLinear,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
histogramDef: []string{"8.0", "2.0", "10"},
}
s2 := *&s1
s1.value = 10
s2.value = 20
defer thInitSampleHasher(hashMD5)()
c := newCollector(defaultExpiryTime)
c.ingressCh <- &s1
c.ingressCh <- &s2
thCollectorProcessSynchronise(t, c)
var mm dto.Metric
m := c.histograms[string(s1.hash())]
m.Histogram.Write(&mm)
a.Equal(t, uint64(2), mm.Histogram.GetSampleCount())
a.Equal(t, float64(30), mm.Histogram.GetSampleSum())
if !a.Len(t, mm.Histogram.GetBucket(), 10) {
t.FailNow()
}
// inspect one of the buckets
b := mm.Histogram.GetBucket()[3]
a.Equal(t, uint64(1), b.GetCumulativeCount())
a.Equal(t, float64(14), b.GetUpperBound())
}
func Test_Collector_Collect_NoMetric(t *testing.T) {
c := newCollector(defaultExpiryTime)
metricCh := make(chan prometheus.Metric, 2048)
c.Collect(metricCh)
if !a.Len(t, metricCh, 3) {
t.FailNow()
}
mA := <-metricCh
a.Equal(t, c.metricAppStart.Desc(), mA.Desc())
mB := <-metricCh
a.Equal(t, c.metricAppDuration.Desc(), mB.Desc())
}
func Test_Collector_Collect_MetricFromSamples(t *testing.T) {
c := newCollector(defaultExpiryTime)
// set-up
c.counters["c1"] = NewUpdatingCounter(prometheus.NewCounter(prometheus.CounterOpts{Name: "counter_A", Help: "auto"}))
c.counters["c2"] = NewUpdatingCounter(prometheus.NewCounter(prometheus.CounterOpts{Name: "counter_B", Help: "auto"}))
c.gauges["g1"] = NewUpdatingGauge(prometheus.NewGauge(prometheus.GaugeOpts{Name: "gauge_A", Help: "auto"}))
c.gauges["g2"] = NewUpdatingGauge(prometheus.NewGauge(prometheus.GaugeOpts{Name: "gauge_B", Help: "auto"}))
c.histograms["hl1"] = NewUpdatingHistogram(prometheus.NewHistogram(prometheus.HistogramOpts{Name: "histLinear_A", Help: "auto"}))
expDescMap := make(map[string]prometheus.Desc)
descHash := func(d *prometheus.Desc) []byte {
return []byte(d.String())
}
addDesc := func(m map[string]prometheus.Desc, me prometheus.Metric) {
d := me.Desc()
m[string(descHash(d))] = *d
}
addDesc(expDescMap, c.counters["c1"].Counter)
addDesc(expDescMap, c.counters["c2"].Counter)
addDesc(expDescMap, c.gauges["g1"].Gauge)
addDesc(expDescMap, c.gauges["g2"].Gauge)
addDesc(expDescMap, c.histograms["hl1"].Histogram)
addDesc(expDescMap, c.metricAppStart)
addDesc(expDescMap, c.metricAppDuration)
addDesc(expDescMap, c.metricQueueLength)
metricCh := make(chan prometheus.Metric, 2048)
// call
c.Collect(metricCh)
// check
if !a.Len(t, metricCh, len(expDescMap)) {
t.FailNow()
}
gotDescMap := make(map[string]prometheus.Desc)
totMetric := len(metricCh)
for i := 0; i < totMetric; i++ {
addDesc(gotDescMap, <-metricCh)
}
a.Equal(t, expDescMap, gotDescMap)
}
func Test_Collector_Expire(t *testing.T) {
defer thInitSampleHasher(hashMD5)()
c := newCollector(defaultExpiryTime)
thCollectorProcessPopulate(c, tfCollectorSamples)
thCollectorProcessSynchronise(t, c)
l := len(c.counters)
var k string
var m *UpdatingCounter
for k, m = range c.counters {
break
}
a.NotNil(t, c.counters[k])
m.UpdatedAt = time.Now().Add(-48 * time.Hour)
c.expire()
a.Nil(t, c.counters[k])
a.Equal(t, l-1, len(c.counters))
}