Add support for wasm runtime metrics try #2 (#4483)

* Add runtime metrics provider

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

* Runner changes

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

* Some sample metrics in paras_inherent

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

* update cargo toml

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

* fmt

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

* bug

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

* more fmt after merge

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

* Refactor metric prefix override

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

* fmt

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

* remove bug comment

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

* Add runtime metric primitives

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

* Impl trace event parsing

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

* Update metrics

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

* cargo lock

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

* fmt

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

* Fix target check

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

* Runtime metrics primitives

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

* Review feedback

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

* Runtime metrics crate

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

* Node side runtime metric changes

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

* use runtime CounterVec instead of macro

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

* fmt nice

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

* remove dead code

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

* base58 decoding

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

* base58 encoding

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

* fix warn

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

* typo

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

* Review feedback

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

* Finish label support

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

* fmt

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

* please compile

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

* add feature gate

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

* fmt

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

* Comment cargo toml

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

* Fix cargo toml description

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

* Update doc.

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

* switch to `runtime-metrics` feature

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

* fmt

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

* cargo toml

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

* fix tests

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

* fixes

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

* better ux

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

* from_utf8_unchecked is safe

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

* fmt

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

* Add Counter and refactor

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

* Fixes

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

* review fixes

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

* more fixes

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

* add integration test

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

* dev deps

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

* gitlab script update

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

* review fixes

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

* fix merge damage

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

* Run tests twice

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

* small fix

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

* typo

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

* cargo lock

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

* tests

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

* spellcheck happy ?

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

* more fixes

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

* Guard tracing init

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

* missing copyright

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

* update lockfile for substrate

Co-authored-by: parity-processbot <>
This commit is contained in:
sandreim
2021-12-16 13:56:25 +02:00
committed by GitHub
parent 6f2f9fe381
commit e75ad88ae8
23 changed files with 1014 additions and 185 deletions
+33
View File
@@ -0,0 +1,33 @@
// 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/>.
//! Runtime metric interface similar to native Prometheus metrics.
//!
//! This is intended to be used only for testing and debugging and **must never
//! be used in production**. It requires the Substrate wasm tracing support
//! and command line configuration: `--tracing-targets wasm_tracing=trace`.
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-metrics")]
mod with_runtime_metrics;
#[cfg(feature = "runtime-metrics")]
pub use crate::with_runtime_metrics::*;
#[cfg(not(feature = "runtime-metrics"))]
mod without_runtime_metrics;
#[cfg(not(feature = "runtime-metrics"))]
pub use crate::without_runtime_metrics::*;
@@ -0,0 +1,126 @@
// 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/>.
//! This module provides an implementation for the runtime metrics types: `Counter`
//! and `CounterVec`. These types expose a Prometheus like interface and same functionality.
//! Each instance of a runtime metric is mapped to a Prometheus metric on the node side.
const TRACING_TARGET: &'static str = "metrics";
use parity_scale_codec::Encode;
use primitives::v1::{
RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricRegisterParams, RuntimeMetricUpdate,
};
use sp_std::prelude::*;
/// Holds a set of counters that have different values for their labels,
/// like Prometheus CounterVec.
pub struct CounterVec {
name: &'static str,
label_values: Option<RuntimeMetricLabelValues>,
}
/// A counter metric.
pub struct Counter {
name: &'static str,
}
/// Convenience trait implemented for all metric types.
trait MetricEmitter {
fn emit(metric_op: &RuntimeMetricUpdate) {
sp_tracing::event!(
target: TRACING_TARGET,
sp_tracing::Level::TRACE,
update_op = bs58::encode(&metric_op.encode()).into_string().as_str()
);
}
}
impl MetricEmitter for CounterVec {}
impl MetricEmitter for Counter {}
impl CounterVec {
/// Create a new metric with specified `name`, `description` and `labels`.
pub fn new(name: &'static str, description: &'static str, labels: &[&'static str]) -> Self {
// Send a register metric operation to node side.
let metric_update = RuntimeMetricUpdate {
metric_name: Vec::from(name),
op: RuntimeMetricOp::Register(RuntimeMetricRegisterParams::new(
Vec::from(description),
Some(labels.into()),
)),
};
Self::emit(&metric_update);
CounterVec { name, label_values: None }
}
/// Set the label values. Must be called before each increment operation.
pub fn with_label_values(&mut self, label_values: &[&'static str]) -> &mut Self {
self.label_values = Some(label_values.into());
self
}
/// Increment the counter by `value`.
pub fn inc_by(&mut self, value: u64) {
self.label_values.take().map(|label_values| {
let metric_update = RuntimeMetricUpdate {
metric_name: Vec::from(self.name),
op: RuntimeMetricOp::IncrementCounterVec(value, label_values),
};
Self::emit(&metric_update);
});
}
/// Increment the counter value.
pub fn inc(&mut self) {
self.inc_by(1);
}
}
impl Counter {
/// Create a new counter metric with specified `name`, `description`.
pub fn new(name: &'static str, description: &'static str) -> Self {
// Send a register metric operation to node side.
let metric_update = RuntimeMetricUpdate {
metric_name: Vec::from(name),
op: RuntimeMetricOp::Register(RuntimeMetricRegisterParams::new(
Vec::from(description),
None,
)),
};
Self::emit(&metric_update);
Counter { name }
}
/// Increment counter by `value`.
pub fn inc_by(&mut self, value: u64) {
let metric_update = RuntimeMetricUpdate {
metric_name: Vec::from(self.name),
op: RuntimeMetricOp::IncrementCounter(value),
};
Self::emit(&metric_update);
}
/// Increment counter.
pub fn inc(&mut self) {
self.inc_by(1);
}
}
@@ -0,0 +1,52 @@
// 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/>.
//! Runtime metrics are usable from the wasm runtime only. The purpose of this module is to
//! provide a dummy implementation for the native runtime to avoid cluttering the runtime code
//! with `#[cfg(feature = "runtime-metrics")]`.
/// A dummy Counter.
pub struct Counter;
/// A dummy CounterVec.
pub struct CounterVec;
/// Dummy implementation.
impl CounterVec {
/// Constructor.
pub fn new(_name: &'static str, _description: &'static str, _labels: &[&'static str]) -> Self {
CounterVec
}
/// Sets label values, implementation is a `no op`.
pub fn with_label_values(&mut self, _label_values: &[&'static str]) -> &mut Self {
self
}
/// Increment counter by value, implementation is a `no op`.
pub fn inc_by(&mut self, _: u64) {}
/// Increment counter, implementation is a `no op`.
pub fn inc(&mut self) {}
}
/// Dummy implementation.
impl Counter {
/// Constructor.
pub fn new(_name: &'static str, _description: &'static str) -> Self {
Counter
}
/// Increment counter by value, implementation is a `no op`.
pub fn inc_by(&mut self, _: u64) {}
/// Increment counter, implementation is a `no op`.
pub fn inc(&mut self) {}
}