-
Notifications
You must be signed in to change notification settings - Fork 174
/
worker_test.go
587 lines (515 loc) · 13.8 KB
/
worker_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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package veneur
import (
"strings"
"sync"
"testing"
"time"
"github.com/stripe/veneur/v14/sinks"
"github.com/stripe/veneur/v14/ssf"
"github.com/stripe/veneur/v14/trace"
"github.com/stripe/veneur/v14/trace/testbackend"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stripe/veneur/v14/samplers"
"github.com/stripe/veneur/v14/samplers/metricpb"
)
func TestWorker(t *testing.T) {
w := NewWorker(1, true, false, nil, logrus.New(), nil)
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "counter",
},
Value: 1.0,
Digest: 12345,
SampleRate: 1.0,
}
w.ProcessMetric(&m)
wm := w.Flush()
assert.Len(t, wm.counters, 1, "Number of flushed metrics")
nometrics := w.Flush()
assert.Len(t, nometrics.counters, 0, "Should flush no metrics")
}
func TestWorkerLocal(t *testing.T) {
w := NewWorker(1, true, false, nil, logrus.New(), nil)
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "histogram",
},
Value: 1.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.LocalOnly,
}
w.ProcessMetric(&m)
wm := w.Flush()
assert.Len(t, wm.localHistograms, 1, "number of local histograms")
assert.Len(t, wm.histograms, 0, "number of global histograms")
}
func TestWorkerGlobal(t *testing.T) {
w := NewWorker(1, false, false, nil, logrus.New(), nil)
gc := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "counter",
},
Value: 1.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}
w.ProcessMetric(&gc)
gg := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "b.c.a",
Type: "gauge",
},
Value: 1.0,
Digest: 12346,
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}
w.ProcessMetric(&gg)
assert.Equal(t, 1, len(w.wm.globalGauges), "should have 1 global gauge")
assert.Equal(t, 0, len(w.wm.gauges), "should have no normal gauges")
assert.Equal(t, 1, len(w.wm.globalCounters), "should have 1 global counter")
assert.Equal(t, 0, len(w.wm.counters), "should have no local counters")
}
func TestWorkerStatusMetric(t *testing.T) {
w := NewWorker(1, true, false, nil, logrus.New(), nil)
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "status",
},
Value: ssf.SSFSample_CRITICAL,
Digest: 12345,
Message: "you've got mail!",
}
w.ProcessMetric(&m)
wm := w.Flush()
assert.Len(t, wm.localStatusChecks, 1, "Number of flushed metrics")
var datapoint *samplers.StatusCheck
for _, v := range wm.localStatusChecks {
datapoint = v
break
}
assert.NotNil(t, datapoint, "Expected a service check to be in the worker metrics map, but none found")
assert.Equal(t, float64(m.Value.(ssf.SSFSample_Status)), float64(datapoint.Value), "The value of the status check should be the same value as the UDPMetric input")
assert.Equal(t, m.Message, datapoint.Message, "The message of the status check should be the same message as the UDPMetric input")
assert.Equal(t, m.Name, datapoint.Name, "The name of the status check should be the same name as the UDPMetric input")
nometrics := w.Flush()
assert.Len(t, nometrics.localStatusChecks, 0, "Should flush no metrics")
}
func TestSpanWorkerTagApplication(t *testing.T) {
tags := map[string]func() map[string]string{
"foo2": func() map[string]string {
return map[string]string{
"foo": "other",
}
},
"baz": func() map[string]string {
return map[string]string{
"baz": "qux",
}
},
"both": func() map[string]string {
return map[string]string{
"baz": "qux",
}
},
}
testSpan := func(tags map[string]string) *ssf.SSFSpan {
return &ssf.SSFSpan{
TraceId: 1,
ParentId: 1,
Id: 2,
StartTimestamp: int64(time.Now().UnixNano()),
EndTimestamp: int64(time.Now().UnixNano()),
Tags: tags,
Error: false,
Service: "farts-srv",
Indicator: false,
Name: "farting farty farts",
}
}
cl, clch := newTestClient(t, 1)
quitch := make(chan struct{})
go func() {
for range clch {
}
}()
fake := &fakeSpanSink{wg: &sync.WaitGroup{}}
spanChanNone := make(chan *ssf.SSFSpan)
spanChanFoo := make(chan *ssf.SSFSpan)
logger := logrus.NewEntry(logrus.New())
go NewSpanWorker(
[]sinks.SpanSink{fake}, cl, nil, spanChanNone, logger).Work()
go NewSpanWorker(
[]sinks.SpanSink{fake}, cl, nil, spanChanFoo, logger).Work()
sendAndWait := func(spanChan chan<- *ssf.SSFSpan, span *ssf.SSFSpan) {
fake.wg.Add(1)
spanChan <- span
fake.wg.Wait()
}
// Don't allocate a map if there's no common tags and not tag map on the
// span already
sendAndWait(spanChanNone, testSpan(nil))
require.Nil(t, fake.latestSpan().Tags)
// Do not override existing tags if keys match
sendAndWait(spanChanFoo, testSpan(tags["foo2"]()))
require.Equal(t, tags["foo2"](), fake.latestSpan().Tags)
// Combine keys when no match
sendAndWait(spanChanFoo, testSpan(tags["baz"]()))
require.Equal(t, tags["both"](), fake.latestSpan().Tags)
close(quitch)
}
type fakeSpanSink struct {
wg *sync.WaitGroup
spans []*ssf.SSFSpan
}
func (s *fakeSpanSink) Start(*trace.Client) error { return nil }
func (s *fakeSpanSink) Name() string { return "fake" }
func (s *fakeSpanSink) Flush() {}
func (s *fakeSpanSink) latestSpan() *ssf.SSFSpan { return s.spans[len(s.spans)-1] }
func (s *fakeSpanSink) Ingest(span *ssf.SSFSpan) error {
s.spans = append(s.spans, span)
s.wg.Done()
return nil
}
func newTestClient(t *testing.T, num int) (*trace.Client, chan *ssf.SSFSpan) {
ch := make(chan *ssf.SSFSpan, num)
cl, err := trace.NewBackendClient(testbackend.NewBackend(ch))
require.NoError(t, err)
return cl, ch
}
type testMetricExporter interface {
GetName() string
Metric() (*metricpb.Metric, error)
}
func exportMetricAndFlush(t testing.TB, exp testMetricExporter) WorkerMetrics {
w := NewWorker(1, true, false, nil, logrus.New(), nil)
m, err := exp.Metric()
assert.NoErrorf(t, err, "exporting the metric '%s' shouldn't have failed",
exp.GetName())
assert.NoError(t, w.ImportMetric(m), "importing a metric shouldn't "+
"have failed")
return w.Flush()
}
func TestWorkerImportMetricGRPC(t *testing.T) {
t.Run("histogram", func(t *testing.T) {
t.Parallel()
h := samplers.NewHist("test.histo", nil)
h.Sample(1.0, 1.0)
assert.Len(t, exportMetricAndFlush(t, h).histograms, 1,
"The number of flushed histograms is not correct")
})
t.Run("gauge", func(t *testing.T) {
t.Parallel()
g := samplers.NewGauge("test.gauge", nil)
g.Sample(2.0, 1.0)
assert.Len(t, exportMetricAndFlush(t, g).globalGauges, 1,
"The number of flushed gauges is not correct")
})
t.Run("counter", func(t *testing.T) {
t.Parallel()
c := samplers.NewCounter("test.counter", nil)
c.Sample(2.0, 1.0)
assert.Len(t, exportMetricAndFlush(t, c).globalCounters, 1,
"The number of flushed counters is not correct")
})
t.Run("timer", func(t *testing.T) {
t.Parallel()
w := NewWorker(1, true, false, nil, logrus.New(), nil)
h := samplers.NewHist("test.timer", nil)
h.Sample(1.0, 1.0)
m, err := h.Metric()
assert.NoErrorf(t, err, "exporting the histogram shouldn't have failed")
m.Type = metricpb.Type_Timer
assert.NoError(t, w.ImportMetric(m), "importing a timer shouldn't "+
"have failed")
assert.Len(t, w.Flush().timers, 1, "The number of flushed "+
"timers is not correct")
})
t.Run("set", func(t *testing.T) {
t.Parallel()
s := samplers.NewSet("test.set", nil)
s.Sample("value")
assert.Len(t, exportMetricAndFlush(t, s).sets, 1,
"The number of flushed sets is not correct")
})
}
func TestWorkerImportMetricGRPCNilValue(t *testing.T) {
t.Parallel()
w := NewWorker(1, true, false, nil, logrus.New(), nil)
metric := &metricpb.Metric{
Name: "test",
Type: metricpb.Type_Histogram,
Value: nil,
}
assert.Error(t, w.ImportMetric(metric), "Importing a metric with "+
"a nil value should have failed")
}
// Test that (WorkerMetrics).ForwardableMetrics produces the right output with
// a variety of inputs.
func TestWorkerMetricsForwardableMetrics(t *testing.T) {
t.Parallel()
type testMetric struct {
name string
scope samplers.MetricScope
mType string
}
testCases := []struct {
name string
inputs []testMetric
expected []testMetric
}{
{
name: "no global metrics",
inputs: []testMetric{
testMetric{
name: "test.gauge",
scope: samplers.MixedScope,
mType: GaugeTypeName,
},
testMetric{
name: "test.counter",
scope: samplers.LocalOnly,
mType: CounterTypeName,
},
},
expected: []testMetric{},
},
{
name: "some global metrics",
inputs: []testMetric{
testMetric{
name: "test.gauge",
scope: samplers.MixedScope,
mType: GaugeTypeName,
},
testMetric{
name: "test.mixed.histo",
scope: samplers.MixedScope,
mType: HistogramTypeName,
},
},
expected: []testMetric{
testMetric{
name: "test.mixed.histo",
scope: samplers.MixedScope,
mType: HistogramTypeName,
},
},
},
{
name: "no metrics",
inputs: []testMetric{},
expected: []testMetric{},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
wm := NewWorkerMetrics()
// Add all of the test metrics
for _, m := range tc.inputs {
mk := samplers.MetricKey{Name: m.name, Type: m.mType}
wm.Upsert(mk, m.scope, []string{})
}
logger := logrus.NewEntry(logrus.New())
ms := wm.ForwardableMetrics(nil, logger)
// Convert all of the forwardable metrics into testMetric's
// and then compare them
actual := make([]testMetric, len(ms))
for i, m := range ms {
actual[i] = testMetric{
name: m.GetName(),
mType: strings.ToLower(m.GetType().String()),
}
}
assert.ElementsMatch(t, tc.expected, actual,
"The output of ForwardableMetrics doesn't have the right metrics")
})
}
}
func TestLocalWorkerSampleTimeseries(t *testing.T) {
w := NewWorker(1, true, true, nil, logrus.New(), nil)
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "histogram",
},
Digest: 1,
Scope: samplers.LocalOnly,
}
m2 := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "counter",
},
Digest: 2,
Scope: samplers.MixedScope,
}
w.SampleTimeseries(&m)
assert.Equal(t, uint64(1), w.uniqueMTS.Estimate())
w.SampleTimeseries(&m)
assert.Equal(t, uint64(1), w.uniqueMTS.Estimate())
w.SampleTimeseries(&m2)
assert.Equal(t, uint64(2), w.uniqueMTS.Estimate())
}
func TestLocalWorkerSampleForwardedTimeseries(t *testing.T) {
w := NewWorker(1, true, true, nil, logrus.New(), nil)
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "histogram",
},
Digest: 1,
Scope: samplers.MixedScope,
}
m2 := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "counter",
},
Digest: 2,
Scope: samplers.GlobalOnly,
}
w.SampleTimeseries(&m)
w.SampleTimeseries(&m2)
assert.Equal(t, uint64(0), w.uniqueMTS.Estimate())
}
func TestGlobalWorkerSampleTimeseries(t *testing.T) {
w := NewWorker(1, false, true, nil, logrus.New(), nil)
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "histogram",
},
Digest: 1,
Scope: samplers.LocalOnly,
}
m2 := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "counter",
},
Digest: 2,
Scope: samplers.GlobalOnly,
}
w.SampleTimeseries(&m)
w.SampleTimeseries(&m2)
assert.Equal(t, uint64(2), w.uniqueMTS.Estimate())
}
func BenchmarkWork(b *testing.B) {
w := NewWorker(1, true, false, nil, logrus.New(), nil)
const Len = 1000
input := make([]*samplers.UDPMetric, Len)
for i, _ := range input {
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "counter",
Type: CounterTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}
switch r := i % 5; r {
case 1:
m.MetricKey.Type = GaugeTypeName
case 2:
m.MetricKey.Type = HistogramTypeName
case 3:
m.MetricKey.Type = SetTypeName
m.Value = "a value here!"
case 4:
m.MetricKey.Type = TimerTypeName
default:
// do nothing
}
input[i] = &m
}
go w.Work()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.PacketChan <- *input[i%Len]
}
w.Stop()
}
func BenchmarkWorkWithCountUniqueTimeseries(b *testing.B) {
w := NewWorker(1, true, true, nil, logrus.New(), nil)
const Len = 1000
input := make([]*samplers.UDPMetric, Len)
for i, _ := range input {
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "counter",
Type: CounterTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}
switch r := i % 5; r {
case 1:
m.MetricKey.Type = GaugeTypeName
case 2:
m.MetricKey.Type = HistogramTypeName
case 3:
m.MetricKey.Type = SetTypeName
m.Value = "a value here!"
case 4:
m.MetricKey.Type = TimerTypeName
default:
// do nothing
}
input[i] = &m
}
go w.Work()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.PacketChan <- *input[i%Len]
}
w.Stop()
}
func BenchmarkSampleTimeseries(b *testing.B) {
w := NewWorker(1, true, true, nil, logrus.New(), nil)
const Len = 1000
input := make([]*samplers.UDPMetric, Len)
for i, _ := range input {
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "counter",
Type: CounterTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}
switch r := i % 5; r {
case 1:
m.MetricKey.Type = GaugeTypeName
case 2:
m.MetricKey.Type = HistogramTypeName
case 3:
m.MetricKey.Type = SetTypeName
m.Value = "a value here!"
case 4:
m.MetricKey.Type = TimerTypeName
default:
// do nothing
}
input[i] = &m
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.SampleTimeseries(input[i%Len])
}
}