File: reporter_test.go

package info (click to toggle)
golang-github-uber-go-tally 4.1.16-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,228 kB
  • sloc: makefile: 89; sh: 13
file content (402 lines) | stat: -rw-r--r-- 11,413 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2021 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 multi

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	tally "github.com/uber-go/tally/v4"
)

func TestMultiReporter(t *testing.T) {
	a, b, c :=
		newCapturingStatsReporter(),
		newCapturingStatsReporter(),
		newCapturingStatsReporter()
	all := []*capturingStatsReporter{a, b, c}

	r := NewMultiReporter(a, b, c)

	tags := []map[string]string{
		{"foo": "bar"},
		{"foo": "baz"},
		{"foo": "qux"},
		{"foo": "bzz"},
		{"foo": "buz"},
	}

	valueBuckets := tally.MustMakeLinearValueBuckets(0, 2, 5)
	durationBuckets := tally.MustMakeLinearDurationBuckets(0, 2*time.Second, 5)

	r.ReportCounter("foo", tags[0], 42)
	r.ReportCounter("foo", tags[0], 84)
	r.ReportGauge("baz", tags[1], 42.0)
	r.ReportTimer("qux", tags[2], 126*time.Millisecond)
	r.ReportHistogramValueSamples("bzz", tags[3], valueBuckets,
		2.0, 4.0, 3)
	r.ReportHistogramDurationSamples("buz", tags[4], durationBuckets,
		2*time.Second, 4*time.Second, 3)
	for _, r := range all {
		require.Equal(t, 2, len(r.counts))

		assert.Equal(t, "foo", r.counts[0].name)
		assert.Equal(t, tags[0], r.counts[0].tags)
		assert.Equal(t, int64(42), r.counts[0].value)

		assert.Equal(t, "foo", r.counts[1].name)
		assert.Equal(t, tags[0], r.counts[1].tags)
		assert.Equal(t, int64(84), r.counts[1].value)

		assert.Equal(t, "baz", r.gauges[0].name)
		assert.Equal(t, tags[1], r.gauges[0].tags)
		assert.Equal(t, float64(42.0), r.gauges[0].value)

		assert.Equal(t, "qux", r.timers[0].name)
		assert.Equal(t, tags[2], r.timers[0].tags)
		assert.Equal(t, 126*time.Millisecond, r.timers[0].value)

		assert.Equal(t, "bzz", r.histogramValueSamples[0].name)
		assert.Equal(t, tags[3], r.histogramValueSamples[0].tags)
		assert.Equal(t, 2.0, r.histogramValueSamples[0].bucketLowerBound)
		assert.Equal(t, 4.0, r.histogramValueSamples[0].bucketUpperBound)
		assert.Equal(t, int64(3), r.histogramValueSamples[0].samples)

		assert.Equal(t, "buz", r.histogramDurationSamples[0].name)
		assert.Equal(t, tags[4], r.histogramDurationSamples[0].tags)
		assert.Equal(t, 2*time.Second, r.histogramDurationSamples[0].bucketLowerBound)
		assert.Equal(t, 4*time.Second, r.histogramDurationSamples[0].bucketUpperBound)
		assert.Equal(t, int64(3), r.histogramDurationSamples[0].samples)
	}

	assert.NotNil(t, r.Capabilities())

	r.Flush()
	for _, r := range all {
		assert.Equal(t, 1, r.flush)
	}
}

func TestMultiCachedReporter(t *testing.T) {
	a, b, c :=
		newCapturingStatsReporter(),
		newCapturingStatsReporter(),
		newCapturingStatsReporter()
	all := []*capturingStatsReporter{a, b, c}

	r := NewMultiCachedReporter(a, b, c)

	tags := []map[string]string{
		{"foo": "bar"},
		{"foo": "baz"},
		{"foo": "qux"},
		{"foo": "bzz"},
		{"foo": "buz"},
	}

	valueBuckets := tally.MustMakeLinearValueBuckets(0, 2, 5)
	durationBuckets := tally.MustMakeLinearDurationBuckets(0, 2*time.Second, 5)

	ctr := r.AllocateCounter("foo", tags[0])
	ctr.ReportCount(42)
	ctr.ReportCount(84)

	gauge := r.AllocateGauge("baz", tags[1])
	gauge.ReportGauge(42.0)

	tmr := r.AllocateTimer("qux", tags[2])
	tmr.ReportTimer(126 * time.Millisecond)

	vhist := r.AllocateHistogram("bzz", tags[3], valueBuckets)
	vhist.ValueBucket(2.0, 4.0).ReportSamples(3)

	dhist := r.AllocateHistogram("buz", tags[4], durationBuckets)
	dhist.DurationBucket(2*time.Second, 4*time.Second).ReportSamples(3)

	for _, r := range all {
		require.Equal(t, 2, len(r.counts))

		assert.Equal(t, "foo", r.counts[0].name)
		assert.Equal(t, tags[0], r.counts[0].tags)
		assert.Equal(t, int64(42), r.counts[0].value)

		assert.Equal(t, "foo", r.counts[1].name)
		assert.Equal(t, tags[0], r.counts[1].tags)
		assert.Equal(t, int64(84), r.counts[1].value)

		assert.Equal(t, "baz", r.gauges[0].name)
		assert.Equal(t, tags[1], r.gauges[0].tags)
		assert.Equal(t, float64(42.0), r.gauges[0].value)

		assert.Equal(t, "qux", r.timers[0].name)
		assert.Equal(t, tags[2], r.timers[0].tags)
		assert.Equal(t, 126*time.Millisecond, r.timers[0].value)

		assert.Equal(t, "bzz", r.histogramValueSamples[0].name)
		assert.Equal(t, tags[3], r.histogramValueSamples[0].tags)
		assert.Equal(t, 2.0, r.histogramValueSamples[0].bucketLowerBound)
		assert.Equal(t, 4.0, r.histogramValueSamples[0].bucketUpperBound)
		assert.Equal(t, int64(3), r.histogramValueSamples[0].samples)

		assert.Equal(t, "buz", r.histogramDurationSamples[0].name)
		assert.Equal(t, tags[4], r.histogramDurationSamples[0].tags)
		assert.Equal(t, 2*time.Second, r.histogramDurationSamples[0].bucketLowerBound)
		assert.Equal(t, 4*time.Second, r.histogramDurationSamples[0].bucketUpperBound)
		assert.Equal(t, int64(3), r.histogramDurationSamples[0].samples)
	}

	assert.NotNil(t, r.Capabilities())

	r.Flush()
	for _, r := range all {
		assert.Equal(t, 1, r.flush)
	}
}

type capturingStatsReporter struct {
	counts                   []capturedCount
	gauges                   []capturedGauge
	timers                   []capturedTimer
	histogramValueSamples    []capturedHistogramValueSamples
	histogramDurationSamples []capturedHistogramDurationSamples
	capabilities             int
	flush                    int
}

type capturedCount struct {
	name  string
	tags  map[string]string
	value int64
}

type capturedGauge struct {
	name  string
	tags  map[string]string
	value float64
}

type capturedTimer struct {
	name  string
	tags  map[string]string
	value time.Duration
}

type capturedHistogramValueSamples struct {
	name             string
	tags             map[string]string
	bucketLowerBound float64
	bucketUpperBound float64
	samples          int64
}

type capturedHistogramDurationSamples struct {
	name             string
	tags             map[string]string
	bucketLowerBound time.Duration
	bucketUpperBound time.Duration
	samples          int64
}

func newCapturingStatsReporter() *capturingStatsReporter {
	return &capturingStatsReporter{}
}

func (r *capturingStatsReporter) ReportCounter(
	name string,
	tags map[string]string,
	value int64,
) {
	r.counts = append(r.counts, capturedCount{name, tags, value})
}

func (r *capturingStatsReporter) ReportGauge(
	name string,
	tags map[string]string,
	value float64,
) {
	r.gauges = append(r.gauges, capturedGauge{name, tags, value})
}

func (r *capturingStatsReporter) ReportTimer(
	name string,
	tags map[string]string,
	value time.Duration,
) {
	r.timers = append(r.timers, capturedTimer{name, tags, value})
}

func (r *capturingStatsReporter) ReportHistogramValueSamples(
	name string,
	tags map[string]string,
	buckets tally.Buckets,
	bucketLowerBound,
	bucketUpperBound float64,
	samples int64,
) {
	elem := capturedHistogramValueSamples{
		name, tags,
		bucketLowerBound, bucketUpperBound, samples,
	}
	r.histogramValueSamples = append(r.histogramValueSamples, elem)
}

func (r *capturingStatsReporter) ReportHistogramDurationSamples(
	name string,
	tags map[string]string,
	buckets tally.Buckets,
	bucketLowerBound,
	bucketUpperBound time.Duration,
	samples int64,
) {
	elem := capturedHistogramDurationSamples{
		name, tags,
		bucketLowerBound, bucketUpperBound, samples,
	}
	r.histogramDurationSamples = append(r.histogramDurationSamples, elem)
}

func (r *capturingStatsReporter) AllocateCounter(
	name string,
	tags map[string]string,
) tally.CachedCount {
	return cachedCount{fn: func(value int64) {
		r.counts = append(r.counts, capturedCount{name, tags, value})
	}}
}

func (r *capturingStatsReporter) AllocateGauge(
	name string,
	tags map[string]string,
) tally.CachedGauge {
	return cachedGauge{fn: func(value float64) {
		r.gauges = append(r.gauges, capturedGauge{name, tags, value})
	}}
}

func (r *capturingStatsReporter) AllocateTimer(
	name string,
	tags map[string]string,
) tally.CachedTimer {
	return cachedTimer{fn: func(value time.Duration) {
		r.timers = append(r.timers, capturedTimer{name, tags, value})
	}}
}

func (r *capturingStatsReporter) AllocateHistogram(
	name string,
	tags map[string]string,
	buckets tally.Buckets,
) tally.CachedHistogram {
	return cachedHistogram{
		valueFn: func(bucketLowerBound, bucketUpperBound float64, samples int64) {
			elem := capturedHistogramValueSamples{
				name, tags, bucketLowerBound, bucketUpperBound, samples,
			}
			r.histogramValueSamples = append(r.histogramValueSamples, elem)
		},
		durationFn: func(bucketLowerBound, bucketUpperBound time.Duration, samples int64) {
			elem := capturedHistogramDurationSamples{
				name, tags, bucketLowerBound, bucketUpperBound, samples,
			}
			r.histogramDurationSamples = append(r.histogramDurationSamples, elem)
		},
	}
}

func (r *capturingStatsReporter) Capabilities() tally.Capabilities {
	r.capabilities++
	return r
}

func (r *capturingStatsReporter) Reporting() bool {
	return true
}

func (r *capturingStatsReporter) Tagging() bool {
	return true
}

func (r *capturingStatsReporter) Flush() {
	r.flush++
}

type cachedCount struct {
	fn func(value int64)
}

func (c cachedCount) ReportCount(value int64) {
	c.fn(value)
}

type cachedGauge struct {
	fn func(value float64)
}

func (c cachedGauge) ReportGauge(value float64) {
	c.fn(value)
}

type cachedTimer struct {
	fn func(value time.Duration)
}

func (c cachedTimer) ReportTimer(value time.Duration) {
	c.fn(value)
}

type cachedHistogram struct {
	valueFn    func(bucketLowerBound, bucketUpperBound float64, samples int64)
	durationFn func(bucketLowerBound, bucketUpperBound time.Duration, samples int64)
}

func (h cachedHistogram) ValueBucket(
	bucketLowerBound, bucketUpperBound float64,
) tally.CachedHistogramBucket {
	return cachedHistogramValueBucket{&h, bucketLowerBound, bucketUpperBound}
}

func (h cachedHistogram) DurationBucket(
	bucketLowerBound, bucketUpperBound time.Duration,
) tally.CachedHistogramBucket {
	return cachedHistogramDurationBucket{&h, bucketLowerBound, bucketUpperBound}
}

type cachedHistogramValueBucket struct {
	histogram        *cachedHistogram
	bucketLowerBound float64
	bucketUpperBound float64
}

func (b cachedHistogramValueBucket) ReportSamples(v int64) {
	b.histogram.valueFn(b.bucketLowerBound, b.bucketUpperBound, v)
}

type cachedHistogramDurationBucket struct {
	histogram        *cachedHistogram
	bucketLowerBound time.Duration
	bucketUpperBound time.Duration
}

func (b cachedHistogramDurationBucket) ReportSamples(v int64) {
	b.histogram.durationFn(b.bucketLowerBound, b.bucketUpperBound, v)
}