|
21 | 21 | package metrics |
22 | 22 |
|
23 | 23 | import ( |
24 | | - "io" |
25 | 24 | "testing" |
26 | 25 | "time" |
27 | 26 |
|
28 | 27 | "github.com/stretchr/testify/require" |
29 | 28 | "github.com/uber-go/tally" |
30 | 29 | "sync" |
| 30 | + "io" |
31 | 31 | ) |
32 | 32 |
|
33 | 33 | func Test_Counter(t *testing.T) { |
34 | 34 | isReplay := true |
35 | | - scope, closer, reporter := newMetricsScope(&isReplay) |
| 35 | + scope, closer, reporter := NewMetricsScope(&isReplay) |
36 | 36 | scope.Counter("test-name").Inc(1) |
37 | 37 | closer.Close() |
38 | | - require.Equal(t, 0, len(reporter.counts)) |
| 38 | + require.Equal(t, 0, len(reporter.Counts())) |
39 | 39 |
|
40 | 40 | isReplay = false |
41 | | - scope, closer, reporter = newMetricsScope(&isReplay) |
| 41 | + scope, closer, reporter = NewMetricsScope(&isReplay) |
42 | 42 | scope.Counter("test-name").Inc(3) |
43 | 43 | closer.Close() |
44 | | - require.Equal(t, 1, len(reporter.counts)) |
45 | | - require.Equal(t, int64(3), reporter.counts[0].value) |
| 44 | + require.Equal(t, 1, len(reporter.Counts())) |
| 45 | + require.Equal(t, int64(3), reporter.Counts()[0].Value()) |
46 | 46 | } |
47 | 47 |
|
48 | 48 | func Test_Gauge(t *testing.T) { |
49 | 49 | isReplay := true |
50 | | - scope, closer, reporter := newMetricsScope(&isReplay) |
| 50 | + scope, closer, reporter := NewMetricsScope(&isReplay) |
51 | 51 | scope.Gauge("test-name").Update(1) |
52 | 52 | closer.Close() |
53 | | - require.Equal(t, 0, len(reporter.gauges)) |
| 53 | + require.Equal(t, 0, len(reporter.Gauges())) |
54 | 54 |
|
55 | 55 | isReplay = false |
56 | | - scope, closer, reporter = newMetricsScope(&isReplay) |
| 56 | + scope, closer, reporter = NewMetricsScope(&isReplay) |
57 | 57 | scope.Gauge("test-name").Update(3) |
58 | 58 | closer.Close() |
59 | | - require.Equal(t, 1, len(reporter.gauges)) |
60 | | - require.Equal(t, float64(3), reporter.gauges[0].value) |
| 59 | + require.Equal(t, 1, len(reporter.Gauges())) |
| 60 | + require.Equal(t, float64(3), reporter.Gauges()[0].Value()) |
61 | 61 | } |
62 | 62 |
|
63 | 63 | func Test_Timer(t *testing.T) { |
64 | 64 | isReplay := true |
65 | | - scope, closer, reporter := newMetricsScope(&isReplay) |
| 65 | + scope, closer, reporter := NewMetricsScope(&isReplay) |
66 | 66 | scope.Timer("test-name").Record(time.Second) |
67 | 67 | sw := scope.Timer("test-stopwatch").Start() |
68 | 68 | sw.Stop() |
69 | 69 | closer.Close() |
70 | | - require.Equal(t, 0, len(reporter.timers)) |
| 70 | + require.Equal(t, 0, len(reporter.Timers())) |
71 | 71 |
|
72 | 72 | isReplay = false |
73 | | - scope, closer, reporter = newMetricsScope(&isReplay) |
| 73 | + scope, closer, reporter = NewMetricsScope(&isReplay) |
74 | 74 | scope.Timer("test-name").Record(time.Second) |
75 | 75 | sw = scope.Timer("test-stopwatch").Start() |
76 | 76 | sw.Stop() |
77 | 77 | closer.Close() |
78 | | - require.Equal(t, 2, len(reporter.timers)) |
79 | | - require.Equal(t, time.Second, reporter.timers[0].value) |
| 78 | + require.Equal(t, 2, len(reporter.Timers())) |
| 79 | + require.Equal(t, time.Second, reporter.Timers()[0].Value()) |
80 | 80 | } |
81 | 81 |
|
82 | 82 | func Test_Histogram(t *testing.T) { |
83 | 83 | isReplay := true |
84 | | - scope, closer, reporter := newMetricsScope(&isReplay) |
| 84 | + scope, closer, reporter := NewMetricsScope(&isReplay) |
85 | 85 | valueBuckets := tally.MustMakeLinearValueBuckets(0, 10, 10) |
86 | 86 | scope.Histogram("test-hist-1", valueBuckets).RecordValue(5) |
87 | 87 | scope.Histogram("test-hist-2", valueBuckets).RecordValue(15) |
88 | 88 | closer.Close() |
89 | | - require.Equal(t, 0, len(reporter.histogramValueSamples)) |
90 | | - scope, closer, reporter = newMetricsScope(&isReplay) |
| 89 | + require.Equal(t, 0, len(reporter.HistogramValueSamples())) |
| 90 | + scope, closer, reporter = NewMetricsScope(&isReplay) |
91 | 91 | durationBuckets := tally.MustMakeLinearDurationBuckets(0, time.Hour, 10) |
92 | 92 | scope.Histogram("test-hist-1", durationBuckets).RecordDuration(time.Minute) |
93 | 93 | scope.Histogram("test-hist-2", durationBuckets).RecordDuration(time.Minute * 61) |
94 | 94 | sw := scope.Histogram("test-hist-3", durationBuckets).Start() |
95 | 95 | sw.Stop() |
96 | 96 | closer.Close() |
97 | | - require.Equal(t, 0, len(reporter.histogramDurationSamples)) |
| 97 | + require.Equal(t, 0, len(reporter.HistogramDurationSamples())) |
98 | 98 |
|
99 | 99 | isReplay = false |
100 | | - scope, closer, reporter = newMetricsScope(&isReplay) |
| 100 | + scope, closer, reporter = NewMetricsScope(&isReplay) |
101 | 101 | valueBuckets = tally.MustMakeLinearValueBuckets(0, 10, 10) |
102 | 102 | scope.Histogram("test-hist-1", valueBuckets).RecordValue(5) |
103 | 103 | scope.Histogram("test-hist-2", valueBuckets).RecordValue(15) |
104 | 104 | closer.Close() |
105 | | - require.Equal(t, 2, len(reporter.histogramValueSamples)) |
| 105 | + require.Equal(t, 2, len(reporter.HistogramValueSamples())) |
106 | 106 |
|
107 | | - scope, closer, reporter = newMetricsScope(&isReplay) |
| 107 | + scope, closer, reporter = NewMetricsScope(&isReplay) |
108 | 108 | durationBuckets = tally.MustMakeLinearDurationBuckets(0, time.Hour, 10) |
109 | 109 | scope.Histogram("test-hist-1", durationBuckets).RecordDuration(time.Minute) |
110 | 110 | scope.Histogram("test-hist-2", durationBuckets).RecordDuration(time.Minute * 61) |
111 | 111 | sw = scope.Histogram("test-hist-3", durationBuckets).Start() |
112 | 112 | sw.Stop() |
113 | 113 | closer.Close() |
114 | | - require.Equal(t, 3, len(reporter.histogramDurationSamples)) |
| 114 | + require.Equal(t, 3, len(reporter.HistogramDurationSamples())) |
115 | 115 | } |
116 | 116 |
|
117 | 117 | func Test_ScopeCoverage(t *testing.T) { |
118 | 118 | isReplay := false |
119 | | - scope, closer, reporter := newMetricsScope(&isReplay) |
| 119 | + scope, closer, reporter := NewMetricsScope(&isReplay) |
120 | 120 | caps := scope.Capabilities() |
121 | 121 | require.Equal(t, true, caps.Reporting()) |
122 | 122 | require.Equal(t, true, caps.Tagging()) |
123 | 123 | subScope := scope.SubScope("test") |
124 | 124 | taggedScope := subScope.Tagged(make(map[string]string)) |
125 | 125 | taggedScope.Counter("test-counter").Inc(1) |
126 | 126 | closer.Close() |
127 | | - require.Equal(t, 1, len(reporter.counts)) |
| 127 | + require.Equal(t, 1, len(reporter.Counts())) |
128 | 128 | } |
129 | 129 |
|
130 | 130 | func Test_TaggedScope(t *testing.T) { |
131 | | - taggedScope, closer, reporter := newTaggedMetricsScope() |
| 131 | + taggedScope, closer, reporter := NewTaggedMetricsScope() |
132 | 132 | scope := taggedScope.GetTaggedScope("tag1", "val1") |
133 | 133 | scope.Counter("test-name").Inc(3) |
134 | 134 | closer.Close() |
135 | | - require.Equal(t, 1, len(reporter.counts)) |
136 | | - require.Equal(t, int64(3), reporter.counts[0].value) |
| 135 | + require.Equal(t, 1, len(reporter.Counts())) |
| 136 | + require.Equal(t, int64(3), reporter.Counts()[0].Value()) |
137 | 137 |
|
138 | 138 | m := &sync.Map{} |
139 | | - taggedScope, closer, reporter = newTaggedMetricsScope() |
| 139 | + taggedScope, closer, reporter = NewTaggedMetricsScope() |
140 | 140 | taggedScope.Map = m |
141 | 141 | scope = taggedScope.GetTaggedScope("tag2", "val1") |
142 | 142 | scope.Counter("test-name").Inc(2) |
143 | | - taggedScope, closer2, reporter2 := newTaggedMetricsScope() |
| 143 | + taggedScope, closer2, reporter2 := NewTaggedMetricsScope() |
144 | 144 | taggedScope.Map = m |
145 | 145 | scope = taggedScope.GetTaggedScope("tag2", "val1") |
146 | 146 | scope.Counter("test-name").Inc(1) |
147 | 147 | closer2.Close() |
148 | | - require.Equal(t, 0, len(reporter2.counts)) |
| 148 | + require.Equal(t, 0, len(reporter2.Counts())) |
149 | 149 | closer.Close() |
150 | | - require.Equal(t, 1, len(reporter.counts)) |
151 | | - require.Equal(t, int64(3), reporter.counts[0].value) |
| 150 | + require.Equal(t, 1, len(reporter.Counts())) |
| 151 | + require.Equal(t, int64(3), reporter.Counts()[0].Value()) |
152 | 152 | } |
153 | 153 |
|
154 | 154 | func Test_TaggedScope_WithMultiTags(t *testing.T) { |
|
0 commit comments