Parachain runtime metrics followup (#4602)

* initial changes

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fmt

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* remove file

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Remove pallet

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix copyright year

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Remove metric registration op

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Register runtime metrics in client

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fmt

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix build without `runtime-metrics`

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* reduce visibility

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* remove metric prefix logic, use hardcoded

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* review feedback

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Split CounterVec api so it doesn't need mutability

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Const beautify

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Fix

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix docs

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Merge web ui feedback.

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
sandreim
2022-01-05 15:33:02 +02:00
committed by GitHub
parent 8a6ca36ad0
commit b93c6e68ab
10 changed files with 314 additions and 172 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ pub mod runtime;
#[cfg(feature = "runtime-metrics")]
pub use self::runtime::logger_hook;
/// Export a dummy logger hook when `wasm tracing` is not enabled.
/// Export a dummy logger hook when the `runtime-metrics` feature is not enabled.
#[cfg(not(feature = "runtime-metrics"))]
pub fn logger_hook() -> impl FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration) -> () {
|_logger_builder, _config| {}
@@ -20,11 +20,13 @@
//! tracing support. This requires that the custom profiler (`TraceHandler`) to be
//! registered in substrate via a `logger_hook()`. Events emitted from runtime are
//! then captured/processed by the `TraceHandler` implementation.
#![cfg(feature = "runtime-metrics")]
use codec::Decode;
use primitives::v1::{
RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricRegisterParams, RuntimeMetricUpdate,
metric_definitions::{CounterDefinition, CounterVecDefinition},
RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricUpdate,
};
use std::{
collections::hash_map::HashMap,
@@ -33,9 +35,9 @@ use std::{
use substrate_prometheus_endpoint::{
register, Counter, CounterVec, Opts, PrometheusError, Registry, U64,
};
mod parachain;
const LOG_TARGET: &'static str = "metrics::runtime";
const METRIC_PREFIX: &'static str = "polkadot";
/// Holds the registered Prometheus metric collections.
#[derive(Clone, Default)]
@@ -55,12 +57,12 @@ impl RuntimeMetricsProvider {
}
/// Register a counter vec metric.
pub fn register_countervec(&self, metric_name: &str, params: &RuntimeMetricRegisterParams) {
pub fn register_countervec(&self, countervec: CounterVecDefinition) {
self.with_counter_vecs_lock_held(|mut hashmap| {
hashmap.entry(metric_name.to_owned()).or_insert(register(
hashmap.entry(countervec.name.to_owned()).or_insert(register(
CounterVec::new(
Opts::new(metric_name, params.description()),
&params.labels().unwrap_or_default(),
Opts::new(countervec.name, countervec.description),
countervec.labels,
)?,
&self.0,
)?);
@@ -69,11 +71,11 @@ impl RuntimeMetricsProvider {
}
/// Register a counter metric.
pub fn register_counter(&self, metric_name: &str, params: &RuntimeMetricRegisterParams) {
pub fn register_counter(&self, counter: CounterDefinition) {
self.with_counters_lock_held(|mut hashmap| {
hashmap
.entry(metric_name.to_owned())
.or_insert(register(Counter::new(metric_name, params.description())?, &self.0)?);
.entry(counter.name.to_owned())
.or_insert(register(Counter::new(counter.name, counter.description)?, &self.0)?);
return Ok(())
})
}
@@ -162,18 +164,11 @@ impl sc_tracing::TraceHandler for RuntimeMetricsProvider {
impl RuntimeMetricsProvider {
// Parse end execute the update operation.
fn parse_metric_update(&self, update: RuntimeMetricUpdate) {
let metric_name = &format!("{}_{}", METRIC_PREFIX, update.metric_name());
match update.op {
RuntimeMetricOp::Register(ref params) =>
if params.labels.is_none() {
self.register_counter(metric_name, &params);
} else {
self.register_countervec(metric_name, &params);
},
RuntimeMetricOp::IncrementCounterVec(value, ref labels) =>
self.inc_counter_vec_by(metric_name, value, labels),
RuntimeMetricOp::IncrementCounter(value) => self.inc_counter_by(metric_name, value),
self.inc_counter_vec_by(update.metric_name(), value, labels),
RuntimeMetricOp::IncrementCounter(value) =>
self.inc_counter_by(update.metric_name(), value),
}
}
@@ -205,6 +200,7 @@ pub fn logger_hook() -> impl FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Con
}
let registry = config.prometheus_registry().cloned().unwrap();
let metrics_provider = RuntimeMetricsProvider::new(registry);
parachain::register_metrics(&metrics_provider);
logger_builder.with_custom_profiling(Box::new(metrics_provider));
}
}
@@ -0,0 +1,38 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Client side declaration and registration of the parachain Prometheus metrics.
//! All of the metrics have a correspondent runtime metric definition.
use crate::runtime::RuntimeMetricsProvider;
use primitives::v1::metric_definitions::{
PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS,
PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED, PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED,
PARACHAIN_INHERENT_DATA_DISPUTE_SETS_INCLUDED, PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED,
PARACHAIN_INHERENT_DATA_WEIGHT,
};
/// Register the parachain runtime metrics.
pub fn register_metrics(runtime_metrics_provider: &RuntimeMetricsProvider) {
runtime_metrics_provider.register_counter(PARACHAIN_INHERENT_DATA_DISPUTE_SETS_INCLUDED);
runtime_metrics_provider.register_counter(PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED);
runtime_metrics_provider.register_countervec(PARACHAIN_INHERENT_DATA_WEIGHT);
runtime_metrics_provider.register_countervec(PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED);
runtime_metrics_provider.register_countervec(PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED);
runtime_metrics_provider
.register_countervec(PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS);
}