use std::{
sync::{Arc, RwLock},
time::Instant,
};
use super::{MyRegistry, SubmoduleState};
use prometheus::{IntCounterVec, Registry};
struct Metrics {
registry: Registry,
ops: IntCounterVec,
time: IntCounterVec,
}
pub struct MetaInstrumentationState {
metrics: Metrics,
}
impl MetaInstrumentationState {
pub fn meta_instrument<T, F>(&self, operation: &str, f: F) -> T
where
F: FnOnce() -> T,
{
let start = Instant::now();
let ret = f();
self.metrics.ops.with_label_values(&[operation]).inc();
self.metrics
.time
.with_label_values(&[operation])
.inc_by(start.elapsed().as_micros() as u64);
ret
}
}
impl SubmoduleState for MetaInstrumentationState {
fn new(_module: &super::PrometheusModule) -> Result<Arc<RwLock<Self>>, String>
where
Self: Sized,
{
let registry = Registry::new();
let state = Arc::new(RwLock::new(MetaInstrumentationState {
metrics: Metrics {
ops: registry.register_int_counter_vec(
"rs_malloc_tracker_instrumentation_ops_total",
"Amount of instrumentation operations performed",
&["operation"],
)?,
time: registry.register_int_counter_vec(
"rs_malloc_tracker_instrumentation_time_us_total",
"Amount of time spent processing operations (microseconds)",
&["operation"],
)?,
registry,
},
}));
Ok(state)
}
fn get_registry(&self) -> &Registry {
&self.metrics.registry
}
}