1use {
7 serde::Serialize,
8 std::{cell::RefCell, collections::BTreeMap},
9};
10
11#[derive(Debug, Default, Serialize)]
13pub struct CustomMetrics {
14 pub sample_u64: BTreeMap<String, Vec<u64>>,
16 pub run_u64: BTreeMap<String, u64>,
18}
19
20thread_local! {
21 static CUSTOM_METRICS: RefCell<CustomMetrics> = RefCell::new(CustomMetrics::default());
22}
23
24pub fn record_sample_u64(name: impl Into<String>, value: u64) {
26 CUSTOM_METRICS.with(|metrics| {
27 metrics
28 .borrow_mut()
29 .sample_u64
30 .entry(name.into())
31 .or_default()
32 .push(value);
33 });
34}
35
36pub fn record_run_u64(name: impl Into<String>, value: u64) {
38 CUSTOM_METRICS.with(|metrics| {
39 metrics.borrow_mut().run_u64.insert(name.into(), value);
40 });
41}
42
43#[cfg(feature = "registry")]
44pub(crate) fn clear() {
45 CUSTOM_METRICS.with(|metrics| *metrics.borrow_mut() = CustomMetrics::default());
46}
47
48#[cfg(feature = "registry")]
49pub(crate) fn take() -> CustomMetrics {
50 CUSTOM_METRICS.with(|metrics| std::mem::take(&mut *metrics.borrow_mut()))
51}
52
53impl CustomMetrics {
54 #[cfg(feature = "registry")]
55 pub(crate) fn is_empty(&self) -> bool {
56 self.sample_u64.is_empty() && self.run_u64.is_empty()
57 }
58}