-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathtest_reporter.go
288 lines (248 loc) · 6.87 KB
/
test_reporter.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
// Copyright (c) 2016 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 xmetrics
import (
"sync"
"time"
"github.com/uber-go/tally"
)
// TestStatsReporter is a test reporter that collects metrics and makes
// them accessible for testing purposes
type TestStatsReporter interface {
tally.StatsReporter
Counters() map[string]int64
Gauges() map[string]float64
Timers() map[string][]time.Duration
Events() []TestStatsReporterEvent
}
// TestStatsReporterEvent is an event that was reported to the test reporter
type TestStatsReporterEvent interface {
Name() string
Tags() map[string]string
IsCounter() bool
IsGauge() bool
IsTimer() bool
Value() int64
TimerValue() time.Duration
}
// testStatsReporter should probably be moved to the tally project for better testing
type testStatsReporter struct {
sync.RWMutex
counters map[string]int64
gauges map[string]float64
timers map[string][]time.Duration
events []*event
timersDisabled bool
captureEvents bool
}
// NewTestStatsReporter returns a new TestStatsReporter
func NewTestStatsReporter(opts TestStatsReporterOptions) TestStatsReporter {
return &testStatsReporter{
counters: make(map[string]int64),
gauges: make(map[string]float64),
timers: make(map[string][]time.Duration),
timersDisabled: opts.TimersDisabled(),
captureEvents: opts.CaptureEvents(),
}
}
func (r *testStatsReporter) Flush() {}
func (r *testStatsReporter) ReportCounter(name string, tags map[string]string, value int64) {
r.Lock()
r.counters[name] += value
if r.captureEvents {
r.events = append(r.events, &event{
eventType: eventTypeCounter,
name: name,
tags: tags,
value: value,
})
}
r.Unlock()
}
func (r *testStatsReporter) ReportGauge(name string, tags map[string]string, value float64) {
r.Lock()
r.gauges[name] = value
if r.captureEvents {
r.events = append(r.events, &event{
eventType: eventTypeGauge,
name: name,
tags: tags,
value: int64(value),
})
}
r.Unlock()
}
func (r *testStatsReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) {
r.Lock()
if r.captureEvents {
r.events = append(r.events, &event{
eventType: eventTypeTimer,
name: name,
tags: tags,
timerValue: interval,
})
}
if r.timersDisabled {
r.Unlock()
return
}
if _, ok := r.timers[name]; !ok {
r.timers[name] = make([]time.Duration, 0, 1)
}
r.timers[name] = append(r.timers[name], interval)
r.Unlock()
}
func (r *testStatsReporter) ReportHistogramValueSamples(
name string,
tags map[string]string,
buckets tally.Buckets,
bucketLowerBound,
bucketUpperBound float64,
samples int64,
) {
// TODO: implement
}
func (r *testStatsReporter) ReportHistogramDurationSamples(
name string,
tags map[string]string,
buckets tally.Buckets,
bucketLowerBound,
bucketUpperBound time.Duration,
samples int64,
) {
// TODO: implement
}
func (r *testStatsReporter) Capabilities() tally.Capabilities {
return r
}
func (r *testStatsReporter) Reporting() bool {
return true
}
func (r *testStatsReporter) Tagging() bool {
return true
}
func (r *testStatsReporter) Counters() map[string]int64 {
r.RLock()
result := make(map[string]int64, len(r.counters))
for k, v := range r.counters {
result[k] = v
}
r.RUnlock()
return result
}
func (r *testStatsReporter) Gauges() map[string]float64 {
r.RLock()
result := make(map[string]float64, len(r.gauges))
for k, v := range r.gauges {
result[k] = v
}
r.RUnlock()
return result
}
func (r *testStatsReporter) Timers() map[string][]time.Duration {
r.RLock()
result := make(map[string][]time.Duration, len(r.timers))
for k, v := range r.timers {
result[k] = v
}
r.RUnlock()
return result
}
func (r *testStatsReporter) Events() []TestStatsReporterEvent {
r.RLock()
events := make([]TestStatsReporterEvent, len(r.events))
for i := range r.events {
events[i] = r.events[i]
}
r.RUnlock()
return events
}
type event struct {
eventType eventType
name string
tags map[string]string
value int64
timerValue time.Duration
}
type eventType int
const (
eventTypeCounter eventType = iota
eventTypeGauge
eventTypeTimer
)
func (e *event) Name() string {
return e.name
}
func (e *event) Tags() map[string]string {
return e.tags
}
func (e *event) IsCounter() bool {
return e.eventType == eventTypeCounter
}
func (e *event) IsGauge() bool {
return e.eventType == eventTypeGauge
}
func (e *event) IsTimer() bool {
return e.eventType == eventTypeTimer
}
func (e *event) Value() int64 {
return e.value
}
func (e *event) TimerValue() time.Duration {
return e.timerValue
}
// TestStatsReporterOptions is a set of options for a test stats reporter
type TestStatsReporterOptions interface {
// SetTimersDisable sets whether to disable timers
SetTimersDisable(value bool) TestStatsReporterOptions
// SetTimersDisable returns whether to disable timers
TimersDisabled() bool
// SetCaptureEvents sets whether to capture each event
SetCaptureEvents(value bool) TestStatsReporterOptions
// SetCaptureEvents returns whether to capture each event
CaptureEvents() bool
}
type testStatsReporterOptions struct {
timersDisabled bool
captureEvents bool
}
// NewTestStatsReporterOptions creates a new set of options for a test stats reporter
func NewTestStatsReporterOptions() TestStatsReporterOptions {
return &testStatsReporterOptions{
timersDisabled: true,
captureEvents: false,
}
}
func (o *testStatsReporterOptions) SetTimersDisable(value bool) TestStatsReporterOptions {
opts := *o
opts.timersDisabled = value
return &opts
}
func (o *testStatsReporterOptions) TimersDisabled() bool {
return o.timersDisabled
}
func (o *testStatsReporterOptions) SetCaptureEvents(value bool) TestStatsReporterOptions {
opts := *o
opts.captureEvents = value
return &opts
}
func (o *testStatsReporterOptions) CaptureEvents() bool {
return o.captureEvents
}