Crates.io | arc-metrics |
lib.rs | arc-metrics |
version | |
source | src |
created_at | 2025-05-07 17:05:01.050766+00 |
updated_at | 2025-05-07 17:05:01.050766+00 |
description | Composable metrics, application manually registers them |
homepage | |
repository | https://github.com/Developed-Methods/arc-metrics |
max_upload_size | |
id | 1664121 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A lot of data structures & algorithms I write greatly benefit from having counters and gauges for monitoring and testing assumption in production. Sometimes there's multiple instances and things get harder when libraries register metrics to a global state. This library tries to make it easy to create a default Metrics structure which application can register for monitoring however they want.
#[derive(Default)]
pub struct MapWrapper<K: Hash + Eq, V> {
map: HashMap<K, V>,
metrics: Arc<MapWrapperMetrics>,
}
impl<K: Hash + Eq, V> MapWrapper<K, V> {
pub fn get(&self, key: &K) -> Option<&V> {
match self.map.get(key) {
None => {
self.metrics.missing_count.inc();
None
}
Some(v) => {
self.metrics.get_count.inc();
Some(v)
}
}
}
pub fn insert(&mut self, key: K, value: V) {
if self.map.insert(key, value).is_some() {
self.metrics.replace_count.owned_inc();
} else {
self.metrics.insert_count.owned_inc();
}
}
}
#[derive(Default)]
pub struct MapWrapperMetrics {
pub insert_count: IntCounter,
pub replace_count: IntCounter,
pub get_count: IntCounter,
pub missing_count: IntCounter,
}
impl RegisterableMetric for MapWrapperMetrics {
fn register(&'static self, register: &mut RegisterAction) {
register.count("insert", &self.insert_count)
.attr("result", "new");
register.count("insert", &self.replace_count)
.attr("result", "replace");
register.count("get", &self.get_count)
.attr("result", "exists");
register.count("get", &self.missing_count)
.attr("result", "missing");
}
}