Skip to content

Commit 8148c04

Browse files
authored
refactor(util): "consume" counters and gauges on snapshot from DebuggingRecorder (#588)
1 parent b821f88 commit 8148c04

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

metrics-util/src/debugging.rs

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ impl Snapshotter {
106106
for (ck, _) in seen.into_iter() {
107107
let value = match ck.kind() {
108108
MetricKind::Counter => {
109-
counters.get(ck.key()).map(|c| DebugValue::Counter(c.load(Ordering::SeqCst)))
109+
counters.get(ck.key()).map(|c| DebugValue::Counter(c.swap(0, Ordering::SeqCst)))
110110
}
111111
MetricKind::Gauge => gauges.get(ck.key()).map(|g| {
112-
let value = f64::from_bits(g.load(Ordering::SeqCst));
112+
let value = f64::from_bits(g.swap(0, Ordering::SeqCst));
113113
DebugValue::Gauge(value.into())
114114
}),
115115
MetricKind::Histogram => histograms.get(ck.key()).map(|h| {
@@ -219,3 +219,88 @@ impl Default for DebuggingRecorder {
219219
DebuggingRecorder::new()
220220
}
221221
}
222+
223+
#[cfg(test)]
224+
mod tests {
225+
use super::*;
226+
use metrics::Label;
227+
228+
#[test]
229+
fn test_debugging_recorder() {
230+
let recorder = DebuggingRecorder::default();
231+
let snapshotter = recorder.snapshotter();
232+
metrics::with_local_recorder(&recorder, move || {
233+
// We run this multiple times to validate the counters, gauges and histograms are
234+
// "consumed" after being snapshotted. Consumption refers to resetting the value of a
235+
// counter and gauge to 0 and clearing observed values from a histogram.
236+
for _ in 1..3 {
237+
let counter = metrics::counter!("test.counter", "counter.key" => "counter.value");
238+
let gauge_incr = metrics::gauge!("test.gauge.incr", "gauge.key" => "gauge.value");
239+
let gauge_set = metrics::gauge!("test.gauge.set", "gauge.key" => "gauge.value");
240+
let histogram =
241+
metrics::histogram!("test.histogram", "histogram.key" => "histogram.value");
242+
243+
counter.increment(123);
244+
gauge_incr.increment(456);
245+
gauge_set.set(654);
246+
histogram.record(789);
247+
248+
let metrics = snapshotter.snapshot().into_vec();
249+
250+
assert_eq!(
251+
metrics,
252+
vec![
253+
(
254+
CompositeKey::new(
255+
MetricKind::Counter,
256+
Key::from_parts(
257+
"test.counter",
258+
vec![Label::new("counter.key", "counter.value")]
259+
)
260+
),
261+
None,
262+
None,
263+
DebugValue::Counter(123)
264+
),
265+
(
266+
CompositeKey::new(
267+
MetricKind::Gauge,
268+
Key::from_parts(
269+
"test.gauge.incr",
270+
vec![Label::new("gauge.key", "gauge.value")]
271+
)
272+
),
273+
None,
274+
None,
275+
DebugValue::Gauge(OrderedFloat(456.0 as f64))
276+
),
277+
(
278+
CompositeKey::new(
279+
MetricKind::Gauge,
280+
Key::from_parts(
281+
"test.gauge.set",
282+
vec![Label::new("gauge.key", "gauge.value")]
283+
)
284+
),
285+
None,
286+
None,
287+
DebugValue::Gauge(OrderedFloat(654.0 as f64))
288+
),
289+
(
290+
CompositeKey::new(
291+
MetricKind::Histogram,
292+
Key::from_parts(
293+
"test.histogram",
294+
vec![Label::new("histogram.key", "histogram.value")]
295+
)
296+
),
297+
None,
298+
None,
299+
DebugValue::Histogram(vec![OrderedFloat(789.0)])
300+
)
301+
]
302+
);
303+
}
304+
});
305+
}
306+
}

0 commit comments

Comments
 (0)