use polkadot_node_subsystem_util::{
metrics,
metrics::{
prometheus,
prometheus::{Counter, CounterVec, Opts, PrometheusError, Registry, U64},
},
};
pub const SUCCEEDED: &'static str = "succeeded";
pub const FAILED: &'static str = "failed";
#[derive(Clone, Default)]
pub struct Metrics(Option<MetricsInner>);
#[derive(Clone)]
struct MetricsInner {
sent_requests: CounterVec<U64>,
received_requests: Counter<U64>,
imported_requests: CounterVec<U64>,
time_dispute_request: prometheus::Histogram,
}
impl Metrics {
pub fn new_dummy() -> Self {
Metrics(None)
}
pub fn on_sent_request(&self, label: &'static str) {
if let Some(metrics) = &self.0 {
metrics.sent_requests.with_label_values(&[label]).inc()
}
}
pub fn on_received_request(&self) {
if let Some(metrics) = &self.0 {
metrics.received_requests.inc()
}
}
pub fn on_imported(&self, label: &'static str, num_requests: usize) {
if let Some(metrics) = &self.0 {
metrics
.imported_requests
.with_label_values(&[label])
.inc_by(num_requests as u64)
}
}
pub fn time_dispute_request(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
self.0.as_ref().map(|metrics| metrics.time_dispute_request.start_timer())
}
}
impl metrics::Metrics for Metrics {
fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
let metrics = MetricsInner {
sent_requests: prometheus::register(
CounterVec::new(
Opts::new(
"polkadot_parachain_dispute_distribution_sent_requests",
"Total number of sent requests.",
),
&["success"],
)?,
registry,
)?,
received_requests: prometheus::register(
Counter::new(
"polkadot_parachain_dispute_distribution_received_requests",
"Total number of received dispute requests.",
)?,
registry,
)?,
imported_requests: prometheus::register(
CounterVec::new(
Opts::new(
"polkadot_parachain_dispute_distribution_imported_requests",
"Total number of imported requests.",
),
&["success"],
)?,
registry,
)?,
time_dispute_request: prometheus::register(
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
"polkadot_parachain_dispute_distribution_time_dispute_request",
"Time needed for dispute votes to get confirmed/fail getting transmitted.",
))?,
registry,
)?,
};
Ok(Metrics(Some(metrics)))
}
}