use polkadot_node_subsystem_util::metrics::{self, prometheus};
#[derive(Clone)]
struct MetricsInner {
pre_check_judgement: prometheus::Histogram,
votes_total: prometheus::Counter<prometheus::U64>,
votes_started: prometheus::Counter<prometheus::U64>,
votes_duplicate: prometheus::Counter<prometheus::U64>,
pvfs_observed: prometheus::Counter<prometheus::U64>,
pvfs_left: prometheus::Counter<prometheus::U64>,
}
#[derive(Default, Clone)]
pub struct Metrics(Option<MetricsInner>);
impl Metrics {
pub(crate) fn time_pre_check_judgement(
&self,
) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
self.0.as_ref().map(|metrics| metrics.pre_check_judgement.start_timer())
}
pub(crate) fn on_vote_submitted(&self) {
if let Some(metrics) = &self.0 {
metrics.votes_total.inc();
}
}
pub(crate) fn on_vote_submission_started(&self) {
if let Some(metrics) = &self.0 {
metrics.votes_started.inc();
}
}
pub(crate) fn on_vote_duplicate(&self) {
if let Some(metrics) = &self.0 {
metrics.votes_duplicate.inc();
}
}
pub(crate) fn on_pvf_observed(&self, num: usize) {
if let Some(metrics) = &self.0 {
metrics.pvfs_observed.inc_by(num as u64);
}
}
pub(crate) fn on_pvf_left(&self, num: usize) {
if let Some(metrics) = &self.0 {
metrics.pvfs_left.inc_by(num as u64);
}
}
}
impl metrics::Metrics for Metrics {
fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
let metrics = MetricsInner {
pre_check_judgement: prometheus::register(
prometheus::Histogram::with_opts(
prometheus::HistogramOpts::new(
"polkadot_pvf_precheck_judgement",
"Time between sending the pre-check request to receiving the response.",
)
.buckets(vec![0.1, 0.5, 1.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0]),
)?,
registry,
)?,
votes_total: prometheus::register(
prometheus::Counter::new(
"polkadot_pvf_precheck_votes_total",
"The total number of votes submitted.",
)?,
registry,
)?,
votes_started: prometheus::register(
prometheus::Counter::new(
"polkadot_pvf_precheck_votes_started",
"The number of votes that are pending submission",
)?,
registry,
)?,
votes_duplicate: prometheus::register(
prometheus::Counter::new(
"polkadot_pvf_precheck_votes_duplicate",
"The number of votes that are submitted more than once for the same code within\
the same session.",
)?,
registry,
)?,
pvfs_observed: prometheus::register(
prometheus::Counter::new(
"polkadot_pvf_precheck_pvfs_observed",
"The number of new PVFs observed.",
)?,
registry,
)?,
pvfs_left: prometheus::register(
prometheus::Counter::new(
"polkadot_pvf_precheck_pvfs_left",
"The number of PVFs removed from the view.",
)?,
registry,
)?,
};
Ok(Self(Some(metrics)))
}
}