use prometheus::{Gauge, IntGauge, Registry};
use crate::{
types::{FinalizedBlock, Timestamp},
unregister_metric,
};
#[derive(Debug)]
pub(super) struct Metrics {
finalization_time: Gauge,
finalized_block_count: IntGauge,
time_of_last_proposed_block: IntGauge,
time_of_last_finalized_block: IntGauge,
pub(super) current_era: IntGauge,
registry: Registry,
}
impl Metrics {
pub(super) fn new(registry: &Registry) -> Result<Self, prometheus::Error> {
let finalization_time = Gauge::new(
"finalization_time",
"the amount of time, in milliseconds, between proposal and finalization of the latest finalized block",
)?;
let finalized_block_count =
IntGauge::new("amount_of_blocks", "the number of blocks finalized so far")?;
let time_of_last_proposed_block = IntGauge::new(
"time_of_last_block_payload",
"timestamp of the most recently accepted block payload",
)?;
let time_of_last_finalized_block = IntGauge::new(
"time_of_last_finalized_block",
"timestamp of the most recently finalized block",
)?;
let current_era = IntGauge::new("current_era", "the current era")?;
registry.register(Box::new(finalization_time.clone()))?;
registry.register(Box::new(finalized_block_count.clone()))?;
registry.register(Box::new(current_era.clone()))?;
registry.register(Box::new(time_of_last_proposed_block.clone()))?;
registry.register(Box::new(time_of_last_finalized_block.clone()))?;
Ok(Metrics {
finalization_time,
finalized_block_count,
time_of_last_proposed_block,
time_of_last_finalized_block,
current_era,
registry: registry.clone(),
})
}
pub(super) fn finalized_block(&mut self, finalized_block: &FinalizedBlock) {
let time_since_block_payload = finalized_block.timestamp().elapsed().millis() as f64;
self.finalization_time.set(time_since_block_payload);
self.time_of_last_finalized_block
.set(finalized_block.timestamp().millis() as i64);
self.finalized_block_count
.set(finalized_block.height() as i64);
}
pub(super) fn proposed_block(&mut self) {
self.time_of_last_proposed_block
.set(Timestamp::now().millis() as i64);
}
}
impl Drop for Metrics {
fn drop(&mut self) {
unregister_metric!(self.registry, self.finalization_time);
unregister_metric!(self.registry, self.finalized_block_count);
unregister_metric!(self.registry, self.current_era);
unregister_metric!(self.registry, self.time_of_last_finalized_block);
unregister_metric!(self.registry, self.time_of_last_proposed_block);
}
}