Update import metrics and add verification time (#6170)

* refactor import reporting and add time

* Update primitives/consensus/common/src/metrics.rs

Co-authored-by: Max Inden <mail@max-inden.de>

* remove (crate)

* fix longer lines

* swap names to avoid api breaking

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Nikolay Volf
2020-06-01 12:08:59 +03:00
committed by GitHub
parent 9cac359f44
commit 5759cee2e9
4 changed files with 115 additions and 56 deletions
@@ -16,12 +16,17 @@
//! Metering tools for consensus
use prometheus_endpoint::{register, U64, Registry, PrometheusError, Opts, CounterVec};
use prometheus_endpoint::{register, U64, Registry, PrometheusError, Opts, CounterVec, HistogramVec, HistogramOpts};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use crate::import_queue::{BlockImportResult, BlockImportError};
/// Generic Prometheus metrics for common consensus functionality.
#[derive(Clone)]
pub(crate) struct Metrics {
pub import_queue_processed: CounterVec<U64>,
pub block_verification_time: HistogramVec,
}
impl Metrics {
@@ -34,6 +39,42 @@ impl Metrics {
)?,
registry,
)?,
block_verification_time: register(
HistogramVec::new(
HistogramOpts::new(
"block_verification_time",
"Histogram of time taken to import blocks",
),
&["result"],
)?,
registry,
)?,
})
}
pub fn report_import<B: BlockT>(
&self,
result: &Result<BlockImportResult<NumberFor<B>>, BlockImportError>,
) {
let label = match result {
Ok(_) => "success",
Err(BlockImportError::IncompleteHeader(_)) => "incomplete_header",
Err(BlockImportError::VerificationFailed(_,_)) => "verification_failed",
Err(BlockImportError::BadBlock(_)) => "bad_block",
Err(BlockImportError::MissingState) => "missing_state",
Err(BlockImportError::UnknownParent) => "unknown_parent",
Err(BlockImportError::Cancelled) => "cancelled",
Err(BlockImportError::Other(_)) => "failed",
};
self.import_queue_processed.with_label_values(
&[label]
).inc();
}
pub fn report_verification(&self, success: bool, time: std::time::Duration) {
self.block_verification_time.with_label_values(
&[if success { "success" } else { "verification_failed" }]
).observe(time.as_secs_f64());
}
}