Repository navigation
Expand file tree
/
Copy pathnative_histogram_test.go
More file actions
593 lines (476 loc) · 17.5 KB
/
Copy pathnative_histogram_test.go
File metadata and controls
593 lines (476 loc) · 17.5 KB
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
588
589
590
591
592
593
// Copyright (c) 2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tally
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testNativeHistogramData is a NativeHistogramData that records what it is
// given so tests can assert on the payload and on drain semantics.
type testNativeHistogramData struct {
values []float64
marshalErr error
marshals int
clears int
}
func newTestNativeHistogramData() *testNativeHistogramData {
return &testNativeHistogramData{}
}
func (d *testNativeHistogramData) Update(value float64) {
d.values = append(d.values, value)
}
func (d *testNativeHistogramData) Count() uint64 {
return uint64(len(d.values))
}
func (d *testNativeHistogramData) Clear() {
d.values = nil
d.clears++
}
func (d *testNativeHistogramData) MarshalBinary() ([]byte, error) {
d.marshals++
if d.marshalErr != nil {
return nil, d.marshalErr
}
return []byte(fmt.Sprint(d.values)), nil
}
type testCachedNativeHistogram struct {
payloads [][]byte
samples []uint64
}
func (c *testCachedNativeHistogram) ReportNativeHistogram(payload []byte, samples uint64) {
c.payloads = append(c.payloads, payload)
c.samples = append(c.samples, samples)
}
// The two variants are views over one accumulator, exactly as the scope
// builds them.
func newTestValueHistogram(
data NativeHistogramData,
cached CachedNativeHistogram,
) nativeValueHistogram {
return nativeValueHistogram{newNativeHistogram(data, cached)}
}
func newTestDurationHistogram(
data NativeHistogramData,
cached CachedNativeHistogram,
) nativeDurationHistogram {
return nativeDurationHistogram{newNativeHistogram(data, cached)}
}
func TestNativeHistogramRecordValue(t *testing.T) {
data := newTestNativeHistogramData()
h := newTestValueHistogram(data, nil)
h.RecordValue(1.5)
h.RecordValue(2.5)
assert.Equal(t, []float64{1.5, 2.5}, data.values)
assert.Equal(t, uint64(2), h.snapshot())
}
// RecordDuration must convert to milliseconds. Getting this wrong silently
// misaligns every percentile by 1000x, and nothing downstream can correct it.
func TestNativeHistogramRecordDurationRecordsMilliseconds(t *testing.T) {
tests := []struct {
duration time.Duration
expected float64
}{
{duration: 0, expected: 0},
{duration: time.Nanosecond, expected: 1e-6},
{duration: time.Microsecond, expected: 0.001},
{duration: time.Millisecond, expected: 1},
{duration: 250 * time.Millisecond, expected: 250},
{duration: time.Second, expected: 1000},
{duration: 90 * time.Second, expected: 90000},
{duration: -time.Second, expected: -1000},
}
for _, tt := range tests {
t.Run(tt.duration.String(), func(t *testing.T) {
data := newTestNativeHistogramData()
h := newTestDurationHistogram(data, nil)
h.RecordDuration(tt.duration)
require.Equal(t, 1, len(data.values))
assert.Equal(t, tt.expected, data.values[0])
})
}
}
// Native duration histograms are milliseconds where DurationBuckets is
// seconds. The divergence is deliberate; pinned here so it cannot be quietly
// "corrected" back into alignment.
func TestNativeHistogramDurationUnitsDivergeFromDurationBuckets(t *testing.T) {
buckets := DurationBuckets{
10 * time.Millisecond,
250 * time.Millisecond,
3 * time.Second,
}
data := newTestNativeHistogramData()
h := newTestDurationHistogram(data, nil)
for _, d := range buckets {
h.RecordDuration(d)
}
assert.Equal(t, []float64{10, 250, 3000}, data.values)
seconds := buckets.AsValues()
require.Equal(t, len(seconds), len(data.values))
for i := range seconds {
assert.Equal(t, seconds[i]*1000, data.values[i],
"milliseconds, exactly 1000x the bucketed equivalent")
}
}
func TestNativeHistogramStopwatch(t *testing.T) {
now := time.Now()
restore := globalNow
globalNow = func() time.Time { return now }
defer func() { globalNow = restore }()
data := newTestNativeHistogramData()
h := newTestDurationHistogram(data, nil)
sw := h.Start()
now = now.Add(1500 * time.Millisecond)
sw.Stop()
require.Equal(t, 1, len(data.values))
assert.Equal(t, float64(1500), data.values[0])
}
func TestNativeHistogramReport(t *testing.T) {
data := newTestNativeHistogramData()
h := newTestValueHistogram(data, nil)
r := newStatsTestReporter()
h.RecordValue(1)
h.RecordValue(2)
h.report("nh", nil, r)
assert.Equal(t, []byte("[1 2]"), r.nativeHistogramPayload)
assert.Equal(t, uint64(2), r.nativeHistogramSamples)
}
func TestNativeHistogramReportNothingWithoutSamples(t *testing.T) {
data := newTestNativeHistogramData()
h := newTestValueHistogram(data, nil)
r := newStatsTestReporter()
h.report("nh", nil, r)
assert.Nil(t, r.nativeHistogramPayload)
assert.Equal(t, uint64(0), r.nativeHistogramSamples)
assert.Equal(t, 0, data.marshals, "should not marshal an empty accumulator")
}
// Each report covers only the interval since the previous one.
func TestNativeHistogramReportIsDelta(t *testing.T) {
data := newTestNativeHistogramData()
h := newTestValueHistogram(data, nil)
r := newStatsTestReporter()
h.RecordValue(1)
h.report("nh", nil, r)
assert.Equal(t, []byte("[1]"), r.nativeHistogramPayload)
assert.Equal(t, uint64(1), r.nativeHistogramSamples)
h.RecordValue(2)
h.report("nh", nil, r)
assert.Equal(t, []byte("[2]"), r.nativeHistogramPayload)
assert.Equal(t, uint64(1), r.nativeHistogramSamples)
assert.Equal(t, 2, data.clears)
}
// A failed marshal costs that interval's samples. Marshalling is
// deterministic, so keeping them would fail identically next cycle and leave
// the histogram reporting nothing for good.
func TestNativeHistogramReportDiscardsSamplesOnMarshalError(t *testing.T) {
data := newTestNativeHistogramData()
data.marshalErr = errors.New("boom")
h := newTestValueHistogram(data, nil)
r := newStatsTestReporter()
h.RecordValue(1)
h.report("nh", nil, r)
assert.Nil(t, r.nativeHistogramPayload, "nothing should be reported")
assert.Equal(t, 1, data.clears, "accumulator must be cleared anyway")
assert.Equal(t, uint64(0), h.snapshot())
// The next interval reports on its own, carrying none of the lost samples.
data.marshalErr = nil
h.RecordValue(2)
h.report("nh", nil, r)
assert.Equal(t, []byte("[2]"), r.nativeHistogramPayload)
assert.Equal(t, uint64(1), r.nativeHistogramSamples)
assert.Equal(t, uint64(0), h.snapshot())
}
// The reason the type is split in two. RecordValue states no unit, so a
// histogram that accepts both APIs lets two call sites feed one distribution
// 1000x apart -- RecordValue(2) for two seconds against the 2000 that
// RecordDuration(2*time.Second) records -- with nothing to catch it.
func TestNativeHistogramVariantsWithholdEachOthersAPIs(t *testing.T) {
acc := newNativeHistogram(newTestNativeHistogramData(), nil)
var value interface{} = nativeValueHistogram{acc}
var duration interface{} = nativeDurationHistogram{acc}
_, ok := value.(interface{ RecordDuration(time.Duration) })
assert.False(t, ok, "a value histogram must not accept durations")
_, ok = value.(interface{ Start() Stopwatch })
assert.False(t, ok, "a value histogram must not hand out a stopwatch")
_, ok = duration.(interface{ RecordValue(float64) })
assert.False(t, ok, "a duration histogram must not accept raw values")
}
func TestNativeHistogramCachedReport(t *testing.T) {
data := newTestNativeHistogramData()
cached := &testCachedNativeHistogram{}
h := newTestValueHistogram(data, cached)
h.RecordValue(1)
h.RecordValue(2)
h.cachedReport()
require.Equal(t, 1, len(cached.payloads))
assert.Equal(t, []byte("[1 2]"), cached.payloads[0])
assert.Equal(t, uint64(2), cached.samples[0])
// No samples, no report.
h.cachedReport()
assert.Equal(t, 1, len(cached.payloads))
}
func TestNativeHistogramSnapshot(t *testing.T) {
data := newTestNativeHistogramData()
h := newTestValueHistogram(data, nil)
assert.Equal(t, uint64(0), h.snapshot())
h.RecordValue(1)
h.RecordValue(2)
h.RecordValue(3)
assert.Equal(t, uint64(3), h.snapshot())
// Snapshot does not drain.
assert.Equal(t, uint64(3), h.snapshot())
}
func TestDefaultNativeHistogramData(t *testing.T) {
data := defaultNativeHistogramFactory(DefaultNativeHistogramMaxBuckets)
assert.Equal(t, uint64(0), data.Count())
data.Update(1)
data.Update(2)
assert.Equal(t, uint64(2), data.Count())
payload, err := data.MarshalBinary()
assert.Nil(t, payload)
assert.ErrorIs(t, err, ErrNativeHistogramBackendNotConfigured)
data.Clear()
assert.Equal(t, uint64(0), data.Count())
}
// Without a NativeHistogramFactory a scope's native histograms must accumulate
// harmlessly and never report, rather than panicking or emitting nonsense.
func TestDefaultNativeHistogramDataIsInert(t *testing.T) {
acc := newNativeHistogram(
defaultNativeHistogramFactory(DefaultNativeHistogramMaxBuckets),
nil,
)
r := newStatsTestReporter()
nativeValueHistogram{acc}.RecordValue(1)
nativeDurationHistogram{acc}.RecordDuration(time.Second)
assert.Equal(t, uint64(2), acc.snapshot(), "values are still counted")
acc.report("nh", nil, r)
assert.Nil(t, r.nativeHistogramPayload, "but nothing is reported")
assert.Equal(t, uint64(0), acc.snapshot(), "and the failed marshal clears")
}
func TestDefaultNativeHistogramMaxBucketsValue(t *testing.T) {
assert.Equal(t, 160, DefaultNativeHistogramMaxBuckets)
}
// testNativeHistogramFactory records the bucket budget it is handed for each
// native histogram a scope creates, and hands back accumulators the test can
// inspect.
type testNativeHistogramFactory struct {
maxBuckets []int
data []*testNativeHistogramData
}
func (f *testNativeHistogramFactory) New(maxBuckets int) NativeHistogramData {
d := newTestNativeHistogramData()
f.maxBuckets = append(f.maxBuckets, maxBuckets)
f.data = append(f.data, d)
return d
}
// getNativeHistograms mirrors testStatsReporter's other accessors, keying by
// bare metric name.
func (r *testStatsReporter) getNativeHistograms() map[string]*testNativeHistogramValue {
r.mtx.Lock()
defer r.mtx.Unlock()
dst := make(map[string]*testNativeHistogramValue, len(r.nativeHistograms))
for k, v := range r.nativeHistograms {
var (
parts = strings.Split(k, "+")
name string
)
if len(parts) > 0 {
name = parts[0]
}
dst[name] = v
}
return dst
}
func TestScopeNativeHistogram(t *testing.T) {
r := newTestStatsReporter()
f := &testNativeHistogramFactory{}
root, closer := NewRootScope(ScopeOptions{
Reporter: r,
NativeHistogramFactory: f.New,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
s := root.(*scope)
r.nhg.Add(2)
s.NativeValueHistogram("payload_bytes").RecordValue(1)
s.NativeDurationHistogram("latency").RecordDuration(2 * time.Second)
s.report(r)
r.WaitAll()
bytes := r.getNativeHistograms()["payload_bytes"]
require.NotNil(t, bytes)
assert.Equal(t, []byte("[1]"), bytes.payload)
assert.Equal(t, uint64(1), bytes.samples)
latency := r.getNativeHistograms()["latency"]
require.NotNil(t, latency)
assert.Equal(t, []byte("[2000]"), latency.payload,
"durations report as milliseconds")
assert.Equal(t, uint64(1), latency.samples)
}
func TestScopeNativeHistogramCachedReporter(t *testing.T) {
r := newTestStatsReporter()
f := &testNativeHistogramFactory{}
root, closer := NewRootScope(ScopeOptions{
CachedReporter: r,
NativeHistogramFactory: f.New,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
s := root.(*scope)
r.nhg.Add(1)
s.NativeValueHistogram("latency").RecordValue(42)
s.cachedReport()
r.WaitAll()
nh := r.getNativeHistograms()["latency"]
require.NotNil(t, nh)
assert.Equal(t, []byte("[42]"), nh.payload)
assert.Equal(t, uint64(1), nh.samples)
}
func TestScopeNativeHistogramMaxBuckets(t *testing.T) {
tests := []struct {
name string
scopeMax int
expectedTo int
}{
{
name: "an unset scope default falls back to the package default",
expectedTo: DefaultNativeHistogramMaxBuckets,
},
{
name: "a configured scope default is honoured",
scopeMax: 64,
expectedTo: 64,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &testNativeHistogramFactory{}
root, closer := NewRootScope(ScopeOptions{
Reporter: NullStatsReporter,
NativeHistogramFactory: f.New,
DefaultNativeHistogramMaxBuckets: tt.scopeMax,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
root.NativeValueHistogram("payload_bytes")
root.NativeDurationHistogram("latency")
require.Equal(t, 2, len(f.maxBuckets))
assert.Equal(t, tt.expectedTo, f.maxBuckets[0])
assert.Equal(t, tt.expectedTo, f.maxBuckets[1],
"both variants honour the same budget")
})
}
}
// A scope with no NativeHistogramFactory must stay usable: values are counted
// but nothing is ever reported.
func TestScopeNativeHistogramWithoutFactory(t *testing.T) {
r := newTestStatsReporter()
root, closer := NewRootScope(ScopeOptions{
Reporter: r,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
s := root.(*scope)
values := s.NativeValueHistogram("payload_bytes").(nativeValueHistogram)
durations := s.NativeDurationHistogram("latency").(nativeDurationHistogram)
values.RecordValue(1)
durations.RecordDuration(time.Second)
require.Equal(t, uint64(1), values.snapshot(), "values are counted")
require.Equal(t, uint64(1), durations.snapshot())
s.report(r)
r.WaitAll()
assert.Empty(t, r.getNativeHistograms(), "but nothing is reported")
assert.Equal(t, uint64(0), values.snapshot(), "and the failed marshal clears")
assert.Equal(t, uint64(0), durations.snapshot())
}
func TestScopeNativeHistogramSubscopesInheritFactory(t *testing.T) {
r := newTestStatsReporter()
f := &testNativeHistogramFactory{}
root, closer := NewRootScope(ScopeOptions{
Reporter: r,
NativeHistogramFactory: f.New,
DefaultNativeHistogramMaxBuckets: 64,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
sub := root.SubScope("requests").Tagged(map[string]string{"env": "test"})
r.nhg.Add(1)
sub.NativeValueHistogram("latency").RecordValue(7)
root.(*scope).registry.Report(r)
r.WaitAll()
require.Equal(t, 1, len(f.maxBuckets))
assert.Equal(t, 64, f.maxBuckets[0], "subscope inherits the scope default")
nh := r.getNativeHistograms()["requests.latency"]
require.NotNil(t, nh)
assert.Equal(t, []byte("[7]"), nh.payload)
assert.Equal(t, uint64(1), nh.samples)
assert.Equal(t, map[string]string{"env": "test"}, nh.tags)
}
func TestScopeNativeHistogramReturnsSameInstance(t *testing.T) {
f := &testNativeHistogramFactory{}
root, closer := NewRootScope(ScopeOptions{
Reporter: NullStatsReporter,
NativeHistogramFactory: f.New,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
first := root.NativeValueHistogram("latency")
second := root.NativeValueHistogram("latency")
assert.Equal(t, first, second)
assert.Equal(t, 1, len(f.maxBuckets), "the accumulator is only built once")
}
// Values and durations are separate metrics, so one name in both yields two
// accumulators -- and two payloads reported under that name, the same way
// Counter("x") and Gauge("x") already both report as x.
func TestScopeNativeHistogramVariantsAreSeparateMetrics(t *testing.T) {
f := &testNativeHistogramFactory{}
root, closer := NewRootScope(ScopeOptions{
Reporter: NullStatsReporter,
NativeHistogramFactory: f.New,
OmitCardinalityMetrics: true,
}, 0)
defer closer.Close()
root.NativeValueHistogram("latency").RecordValue(1)
root.NativeDurationHistogram("latency").RecordDuration(time.Second)
require.Equal(t, 2, len(f.maxBuckets), "one accumulator per variant")
require.Equal(t, 2, len(f.data))
assert.Equal(t, []float64{1}, f.data[0].values)
assert.Equal(t, []float64{1000}, f.data[1].values,
"a second of duration in milliseconds, not the raw value")
}
func TestScopeNativeHistogramSnapshot(t *testing.T) {
s := NewTestScope("prefix", map[string]string{"env": "test"})
s.NativeDurationHistogram("latency").RecordDuration(time.Second)
s.NativeDurationHistogram("latency").RecordDuration(2 * time.Second)
s.NativeValueHistogram("other").RecordValue(3)
snap := s.Snapshot().NativeHistograms()
require.Equal(t, 2, len(snap), "both variants land in the one map")
latency := snap[KeyForPrefixedStringMap("prefix.latency", map[string]string{"env": "test"})]
require.NotNil(t, latency)
assert.Equal(t, "prefix.latency", latency.Name())
assert.Equal(t, map[string]string{"env": "test"}, latency.Tags())
assert.Equal(t, uint64(2), latency.Samples())
other := snap[KeyForPrefixedStringMap("prefix.other", map[string]string{"env": "test"})]
require.NotNil(t, other)
assert.Equal(t, uint64(1), other.Samples())
}