use polkadot_node_subsystem::prometheus::HistogramVec;
use polkadot_node_subsystem_util::metrics::{
self,
prometheus::{
self, prometheus::HistogramTimer, Counter, CounterVec, Histogram, Opts, PrometheusError,
Registry, U64,
},
};
#[derive(Clone, Default)]
pub struct Metrics(Option<MetricsInner>);
#[derive(Clone)]
struct MetricsInner {
chunk_requests_issued: CounterVec<U64>,
recovered_bytes_total: Counter<U64>,
chunk_requests_finished: CounterVec<U64>,
chunk_request_protocols: CounterVec<U64>,
full_data_requests_issued: Counter<U64>,
full_data_requests_finished: CounterVec<U64>,
time_chunk_request: HistogramVec,
time_erasure_recovery: HistogramVec,
time_erasure_reconstruct: HistogramVec,
time_reencode_chunks: Histogram,
time_full_recovery: Histogram,
full_recoveries_finished: CounterVec<U64>,
full_recoveries_started: Counter<U64>,
}
impl Metrics {
pub fn new_dummy() -> Self {
Metrics(None)
}
pub fn on_chunk_request_issued(&self, chunk_type: &str) {
if let Some(metrics) = &self.0 {
metrics.chunk_requests_issued.with_label_values(&[chunk_type]).inc()
}
}
pub fn on_full_request_issued(&self) {
if let Some(metrics) = &self.0 {
metrics.full_data_requests_issued.inc()
}
}
pub fn on_chunk_request_timeout(&self, chunk_type: &str) {
if let Some(metrics) = &self.0 {
metrics
.chunk_requests_finished
.with_label_values(&[chunk_type, "timeout"])
.inc()
}
}
pub fn on_full_request_timeout(&self) {
if let Some(metrics) = &self.0 {
metrics.full_data_requests_finished.with_label_values(&["timeout"]).inc()
}
}
pub fn on_chunk_request_no_such_chunk(&self, chunk_type: &str) {
if let Some(metrics) = &self.0 {
metrics
.chunk_requests_finished
.with_label_values(&[chunk_type, "no_such_chunk"])
.inc()
}
}
pub fn on_full_request_no_such_data(&self) {
if let Some(metrics) = &self.0 {
metrics.full_data_requests_finished.with_label_values(&["no_such_data"]).inc()
}
}
pub fn on_chunk_request_error(&self, chunk_type: &str) {
if let Some(metrics) = &self.0 {
metrics.chunk_requests_finished.with_label_values(&[chunk_type, "error"]).inc()
}
}
pub fn on_full_request_error(&self) {
if let Some(metrics) = &self.0 {
metrics.full_data_requests_finished.with_label_values(&["error"]).inc()
}
}
pub fn on_chunk_request_invalid(&self, chunk_type: &str) {
if let Some(metrics) = &self.0 {
metrics
.chunk_requests_finished
.with_label_values(&[chunk_type, "invalid"])
.inc()
}
}
pub fn on_full_request_invalid(&self) {
if let Some(metrics) = &self.0 {
metrics.full_data_requests_finished.with_label_values(&["invalid"]).inc()
}
}
pub fn on_chunk_request_succeeded(&self, chunk_type: &str) {
if let Some(metrics) = &self.0 {
metrics
.chunk_requests_finished
.with_label_values(&[chunk_type, "success"])
.inc()
}
}
pub fn on_chunk_response_v1(&self) {
if let Some(metrics) = &self.0 {
metrics.chunk_request_protocols.with_label_values(&["v1"]).inc()
}
}
pub fn on_chunk_response_v2(&self) {
if let Some(metrics) = &self.0 {
metrics.chunk_request_protocols.with_label_values(&["v2"]).inc()
}
}
pub fn on_full_request_succeeded(&self) {
if let Some(metrics) = &self.0 {
metrics.full_data_requests_finished.with_label_values(&["success"]).inc()
}
}
pub fn time_chunk_request(&self, chunk_type: &str) -> Option<HistogramTimer> {
self.0.as_ref().map(|metrics| {
metrics.time_chunk_request.with_label_values(&[chunk_type]).start_timer()
})
}
pub fn time_erasure_recovery(&self, chunk_type: &str) -> Option<HistogramTimer> {
self.0.as_ref().map(|metrics| {
metrics.time_erasure_recovery.with_label_values(&[chunk_type]).start_timer()
})
}
pub fn time_erasure_reconstruct(&self, chunk_type: &str) -> Option<HistogramTimer> {
self.0.as_ref().map(|metrics| {
metrics.time_erasure_reconstruct.with_label_values(&[chunk_type]).start_timer()
})
}
pub fn time_reencode_chunks(&self) -> Option<HistogramTimer> {
self.0.as_ref().map(|metrics| metrics.time_reencode_chunks.start_timer())
}
pub fn time_full_recovery(&self) -> Option<HistogramTimer> {
self.0.as_ref().map(|metrics| metrics.time_full_recovery.start_timer())
}
pub fn on_recovery_succeeded(&self, strategy_type: &str, bytes: usize) {
if let Some(metrics) = &self.0 {
metrics
.full_recoveries_finished
.with_label_values(&["success", strategy_type])
.inc();
metrics.recovered_bytes_total.inc_by(bytes as u64)
}
}
pub fn on_recovery_failed(&self, strategy_type: &str) {
if let Some(metrics) = &self.0 {
metrics
.full_recoveries_finished
.with_label_values(&["failure", strategy_type])
.inc()
}
}
pub fn on_recovery_invalid(&self, strategy_type: &str) {
if let Some(metrics) = &self.0 {
metrics
.full_recoveries_finished
.with_label_values(&["invalid", strategy_type])
.inc()
}
}
pub fn on_recovery_started(&self) {
if let Some(metrics) = &self.0 {
metrics.full_recoveries_started.inc()
}
}
}
impl metrics::Metrics for Metrics {
fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
let metrics = MetricsInner {
chunk_requests_issued: prometheus::register(
CounterVec::new(
Opts::new("polkadot_parachain_availability_recovery_chunk_requests_issued",
"Total number of issued chunk requests."),
&["type"]
)?,
registry,
)?,
full_data_requests_issued: prometheus::register(
Counter::new(
"polkadot_parachain_availability_recovery_full_data_requests_issued",
"Total number of issued full data requests.",
)?,
registry,
)?,
recovered_bytes_total: prometheus::register(
Counter::new(
"polkadot_parachain_availability_recovery_bytes_total",
"Total number of bytes recovered",
)?,
registry,
)?,
chunk_requests_finished: prometheus::register(
CounterVec::new(
Opts::new(
"polkadot_parachain_availability_recovery_chunk_requests_finished",
"Total number of chunk requests finished.",
),
&["result", "type"],
)?,
registry,
)?,
chunk_request_protocols: prometheus::register(
CounterVec::new(
Opts::new(
"polkadot_parachain_availability_recovery_chunk_request_protocols",
"Total number of successful chunk requests, mapped by the protocol version (v1 or v2).",
),
&["protocol"],
)?,
registry,
)?,
full_data_requests_finished: prometheus::register(
CounterVec::new(
Opts::new(
"polkadot_parachain_availability_recovery_full_data_requests_finished",
"Total number of full data requests finished.",
),
&["result"],
)?,
registry,
)?,
time_chunk_request: prometheus::register(
prometheus::HistogramVec::new(prometheus::HistogramOpts::new(
"polkadot_parachain_availability_recovery_time_chunk_request",
"Time spent waiting for a response to a chunk request",
), &["type"])?,
registry,
)?,
time_erasure_recovery: prometheus::register(
prometheus::HistogramVec::new(prometheus::HistogramOpts::new(
"polkadot_parachain_availability_recovery_time_erasure_recovery",
"Time spent to recover the erasure code and verify the merkle root by re-encoding as erasure chunks",
), &["type"])?,
registry,
)?,
time_erasure_reconstruct: prometheus::register(
prometheus::HistogramVec::new(prometheus::HistogramOpts::new(
"polkadot_parachain_availability_recovery_time_erasure_reconstruct",
"Time spent to reconstruct the data from chunks",
), &["type"])?,
registry,
)?,
time_reencode_chunks: prometheus::register(
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
"polkadot_parachain_availability_reencode_chunks",
"Time spent re-encoding the data as erasure chunks",
))?,
registry,
)?,
time_full_recovery: prometheus::register(
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
"polkadot_parachain_availability_recovery_time_total",
"Time a full recovery process took, either until failure or successful erasure decoding.",
))?,
registry,
)?,
full_recoveries_finished: prometheus::register(
CounterVec::new(
Opts::new(
"polkadot_parachain_availability_recovery_recoveries_finished",
"Total number of recoveries that finished.",
),
&["result", "strategy_type"],
)?,
registry,
)?,
full_recoveries_started: prometheus::register(
Counter::new(
"polkadot_parachain_availability_recovery_recoveries_started",
"Total number of started recoveries.",
)?,
registry,
)?,
};
Ok(Metrics(Some(metrics)))
}
}