use std::sync::Arc;
use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
#[derive(Clone, Default)]
pub struct MetricsLink(Arc<Option<Metrics>>);
impl MetricsLink {
pub fn new(registry: Option<&Registry>) -> Self {
Self(Arc::new(registry.and_then(|registry| {
Metrics::register(registry)
.map_err(|err| {
log::warn!("Failed to register prometheus metrics: {}", err);
})
.ok()
})))
}
pub fn report(&self, do_this: impl FnOnce(&Metrics)) {
if let Some(metrics) = self.0.as_ref() {
do_this(metrics);
}
}
}
pub struct Metrics {
pub submitted_statements: Counter<U64>,
pub validations_invalid: Counter<U64>,
pub statements_pruned: Counter<U64>,
}
impl Metrics {
pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
submitted_statements: register(
Counter::new(
"substrate_sub_statement_store_submitted_statements",
"Total number of statements submitted",
)?,
registry,
)?,
validations_invalid: register(
Counter::new(
"substrate_sub_statement_store_validations_invalid",
"Total number of statements that were removed from the pool as invalid",
)?,
registry,
)?,
statements_pruned: register(
Counter::new(
"substrate_sub_statement_store_block_statements",
"Total number of statements that was requested to be pruned by block events",
)?,
registry,
)?,
})
}
}