use crate::{
ids::SystemMetricKind,
ops::{prelude::*, runtime::metrics::system::SystemMetrics},
};
use std::{cell::RefCell, collections::HashMap};
thread_local! {
static ICC_METRICS: RefCell<HashMap<IccMetricKey, u64>> =
RefCell::new(HashMap::new());
}
#[derive(Clone)]
pub struct IccMetricsSnapshot {
pub entries: Vec<(IccMetricKey, u64)>,
}
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct IccMetricKey {
pub target: Principal,
pub method: String,
}
pub struct IccMetrics;
impl IccMetrics {
fn increment(target: Principal, method: &str) {
ICC_METRICS.with_borrow_mut(|counts| {
let key = IccMetricKey {
target,
method: method.to_string(),
};
let entry = counts.entry(key).or_insert(0);
*entry = entry.saturating_add(1);
});
}
pub fn record_call(target: impl Into<Principal>, method: &str) {
let target: Principal = target.into();
SystemMetrics::increment(SystemMetricKind::CanisterCall);
Self::increment(target, method);
}
#[must_use]
pub fn snapshot() -> IccMetricsSnapshot {
let entries = ICC_METRICS
.with_borrow(std::clone::Clone::clone)
.into_iter()
.collect();
IccMetricsSnapshot { entries }
}
#[cfg(test)]
pub fn reset() {
ICC_METRICS.with_borrow_mut(HashMap::clear);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn snapshot_map() -> HashMap<IccMetricKey, u64> {
IccMetrics::snapshot().entries.into_iter().collect()
}
#[test]
fn icc_metrics_track_target_and_method() {
IccMetrics::reset();
let t1 = Principal::from_slice(&[1; 29]);
let t2 = Principal::from_slice(&[2; 29]);
IccMetrics::increment(t1, "foo");
IccMetrics::increment(t1, "foo");
IccMetrics::increment(t1, "bar");
IccMetrics::increment(t2, "foo");
let map = snapshot_map();
assert_eq!(
map.get(&IccMetricKey {
target: t1,
method: "foo".to_string()
}),
Some(&2)
);
assert_eq!(
map.get(&IccMetricKey {
target: t1,
method: "bar".to_string()
}),
Some(&1)
);
assert_eq!(
map.get(&IccMetricKey {
target: t2,
method: "foo".to_string()
}),
Some(&1)
);
assert_eq!(map.len(), 3);
}
}