use prometheus::{IntCounter, IntGauge, Registry};
use crate::unregister_metric;
#[derive(Debug)]
pub(super) struct Metrics {
pub(super) items_received: IntCounter,
pub(super) times_gossiped: IntCounter,
pub(super) times_ran_out_of_peers: IntCounter,
pub(super) table_items_current: IntGauge,
pub(super) table_items_finished: IntGauge,
registry: Registry,
}
impl Metrics {
pub fn new(name: &str, registry: &Registry) -> Result<Self, prometheus::Error> {
let items_received = IntCounter::new(
format!("{}_items_received", name),
format!("number of items received by the {}", name),
)?;
let times_gossiped = IntCounter::new(
format!("{}_times_gossiped", name),
format!("number of times the {} sent gossip requests to peers", name),
)?;
let times_ran_out_of_peers = IntCounter::new(
format!("{}_times_ran_out_of_peers", name),
format!(
"number of times the {} ran out of peers and had to pause",
name
),
)?;
let table_items_current = IntGauge::new(
format!("{}_table_items_current", name),
format!(
"number of items in the gossip table of {} in state current",
name
),
)?;
let table_items_finished = IntGauge::new(
format!("{}_table_items_finished", name),
format!(
"number of items in the gossip table of {} in state finished",
name
),
)?;
registry.register(Box::new(items_received.clone()))?;
registry.register(Box::new(times_gossiped.clone()))?;
registry.register(Box::new(times_ran_out_of_peers.clone()))?;
registry.register(Box::new(table_items_current.clone()))?;
registry.register(Box::new(table_items_finished.clone()))?;
Ok(Metrics {
items_received,
times_gossiped,
times_ran_out_of_peers,
table_items_current,
table_items_finished,
registry: registry.clone(),
})
}
}
impl Drop for Metrics {
fn drop(&mut self) {
unregister_metric!(self.registry, self.items_received);
unregister_metric!(self.registry, self.times_gossiped);
unregister_metric!(self.registry, self.times_ran_out_of_peers);
unregister_metric!(self.registry, self.table_items_current);
unregister_metric!(self.registry, self.table_items_finished);
}
}