@@ -58,15 +58,15 @@ func TestGoMetricsAdapter(t *testing.T) {
58
58
// register some metrics and check they're satisfying the go-metrics interface
59
59
// no matter if owned by monitoring or go-metrics
60
60
for name := range counters {
61
- cnt , ok := reg .GetOrRegister (name , func () interface {} {
61
+ cnt , ok := reg .GetOrRegister (name , func () any {
62
62
return metrics .NewCounter ()
63
63
}).(metrics.Counter )
64
64
require .True (t , ok )
65
65
cnt .Clear ()
66
66
}
67
67
68
68
for name := range meters {
69
- meter , ok := reg .GetOrRegister (name , func () interface {} {
69
+ meter , ok := reg .GetOrRegister (name , func () any {
70
70
return metrics .NewMeter ()
71
71
}).(metrics.Meter )
72
72
require .True (t , ok )
@@ -100,14 +100,117 @@ func TestGoMetricsAdapter(t *testing.T) {
100
100
}
101
101
102
102
// check Each only returns metrics not registered with monitoring.Registry
103
- reg .Each (func (name string , v interface {} ) {
103
+ reg .Each (func (name string , v any ) {
104
104
if strings .HasPrefix (name , "mon" ) {
105
105
t .Errorf ("metric %v should not have been reported by each" , name )
106
106
}
107
107
})
108
- monReg .Do (monitoring .Full , func (name string , v interface {} ) {
108
+ monReg .Do (monitoring .Full , func (name string , v any ) {
109
109
if ! strings .HasPrefix (name , "test.mon" ) {
110
110
t .Errorf ("metric %v should not have been reported by each" , name )
111
111
}
112
112
})
113
113
}
114
+
115
+ func TestGoMetricsHistogramClearOnVisit (t * testing.T ) {
116
+ monReg := monitoring .NewRegistry ()
117
+ histogramSample := metrics .NewUniformSample (10 )
118
+ clearedHistogramSample := metrics .NewUniformSample (10 )
119
+ _ = NewGoMetrics (monReg , "original" , Accept ).Register ("histogram" , metrics .NewHistogram (histogramSample ))
120
+ _ = NewGoMetrics (monReg , "cleared" , Accept ).Register ("histogram" , NewClearOnVisitHistogram (clearedHistogramSample ))
121
+ dataPoints := [... ]int {2 , 4 , 8 , 4 , 2 }
122
+ dataPointsMedian := 4.0
123
+ for _ , i := range dataPoints {
124
+ histogramSample .Update (int64 (i ))
125
+ clearedHistogramSample .Update (int64 (i ))
126
+ }
127
+
128
+ preSnapshot := []struct {
129
+ expected any
130
+ actual any
131
+ msg string
132
+ }{
133
+ {
134
+ actual : histogramSample .Count (),
135
+ expected : int64 (len (dataPoints )),
136
+ msg : "histogram sample count incorrect" ,
137
+ },
138
+ {
139
+ actual : clearedHistogramSample .Count (),
140
+ expected : int64 (len (dataPoints )),
141
+ msg : "cleared histogram sample count incorrect" ,
142
+ },
143
+ {
144
+ actual : histogramSample .Percentiles ([]float64 {0.5 }),
145
+ expected : []float64 {dataPointsMedian },
146
+ msg : "histogram median incorrect" ,
147
+ },
148
+ {
149
+ actual : clearedHistogramSample .Percentiles ([]float64 {0.5 }),
150
+ expected : []float64 {dataPointsMedian },
151
+ msg : "cleared histogram median incorrect" ,
152
+ },
153
+ }
154
+
155
+ for _ , tc := range preSnapshot {
156
+ require .Equal (t , tc .expected , tc .actual , tc .msg )
157
+ }
158
+
159
+ // collecting the snapshot triggers the visit of each histogram
160
+ flatSnapshot := monitoring .CollectFlatSnapshot (monReg , monitoring .Full , false )
161
+
162
+ // Check to make sure after the snapshot the samples in
163
+ // clearedHistogramSample have been reset to zero, but the
164
+ // snapshot reports the values before the clear
165
+
166
+ postSnapshot := []struct {
167
+ expected any
168
+ actual any
169
+ msg string
170
+ }{
171
+ {
172
+ actual : histogramSample .Count (),
173
+ expected : int64 (len (dataPoints )),
174
+ msg : "histogram sample count incorrect" ,
175
+ },
176
+ {
177
+ actual : clearedHistogramSample .Count (),
178
+ expected : int64 (0 ),
179
+ msg : "cleared histogram sample count incorrect" ,
180
+ },
181
+ {
182
+ actual : histogramSample .Percentiles ([]float64 {0.5 }),
183
+ expected : []float64 {dataPointsMedian },
184
+ msg : "histogram median incorrect" ,
185
+ },
186
+ {
187
+ actual : clearedHistogramSample .Percentiles ([]float64 {0.5 }),
188
+ expected : []float64 {0 },
189
+ msg : "cleared histogram median incorrect" ,
190
+ },
191
+ {
192
+ actual : flatSnapshot .Ints ["original.histogram.count" ],
193
+ expected : int64 (len (dataPoints )),
194
+ msg : "visited histogram count is wrong" ,
195
+ },
196
+ {
197
+ actual : flatSnapshot .Ints ["cleared.histogram.count" ],
198
+ expected : int64 (len (dataPoints )),
199
+ msg : "visited cleared histogram count is wrong" ,
200
+ },
201
+ {
202
+ actual : flatSnapshot .Floats ["original.histogram.median" ],
203
+ expected : float64 (dataPointsMedian ),
204
+ msg : "visited histogram median is wrong" ,
205
+ },
206
+ {
207
+ actual : flatSnapshot .Floats ["cleared.histogram.median" ],
208
+ expected : float64 (dataPointsMedian ),
209
+ msg : "visited cleared histogram median is wrong" ,
210
+ },
211
+ }
212
+
213
+ for _ , tc := range postSnapshot {
214
+ require .Equal (t , tc .expected , tc .actual , tc .msg )
215
+ }
216
+ }
0 commit comments