1use {
8 serde::Serialize,
9 std::{cell::RefCell, collections::BTreeMap},
10};
11
12#[derive(Debug, Default, Serialize)]
14pub struct CustomMetrics {
15 pub sample_u64: BTreeMap<String, Vec<u64>>,
17 pub run_u64: BTreeMap<String, u64>,
19}
20
21thread_local! {
22 static CUSTOM_METRICS: RefCell<CustomMetrics> =
23 RefCell::new(CustomMetrics::default());
24}
25
26pub fn record_sample_u64(name: impl Into<String>, value: u64) {
32 CUSTOM_METRICS.with(|metrics| {
33 metrics
34 .borrow_mut()
35 .sample_u64
36 .entry(name.into())
37 .or_default()
38 .push(value);
39 });
40}
41
42pub fn record_run_u64(name: impl Into<String>, value: u64) {
46 CUSTOM_METRICS.with(|metrics| {
47 metrics.borrow_mut().run_u64.insert(name.into(), value);
48 });
49}
50
51#[cfg(feature = "registry")]
52pub(crate) fn clear() {
53 CUSTOM_METRICS.with(|metrics| {
54 *metrics.borrow_mut() = CustomMetrics::default();
55 });
56}
57
58#[cfg(feature = "registry")]
59pub(crate) fn take() -> CustomMetrics {
60 CUSTOM_METRICS.with(|metrics| std::mem::take(&mut *metrics.borrow_mut()))
61}
62
63impl CustomMetrics {
64 #[cfg(feature = "registry")]
65 pub(crate) fn is_empty(&self) -> bool {
66 self.sample_u64.is_empty() && self.run_u64.is_empty()
67 }
68}