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
@@ -16,21 +16,24 @@
//! 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.
//! Each instance of a runtime metric is mapped to a Prometheus metric on the client side.
//! The runtime metrics must be registered with the registry in the client, otherwise
//! they will not be published.
const TRACING_TARGET: &'static str = "metrics";
use parity_scale_codec::Encode;
use primitives::v1::{
RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricRegisterParams, RuntimeMetricUpdate,
metric_definitions::{CounterDefinition, CounterVecDefinition},
RuntimeMetricLabelValues, RuntimeMetricOp, 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.
@@ -49,68 +52,57 @@ trait MetricEmitter {
}
}
impl MetricEmitter for CounterVec {}
impl MetricEmitter for Counter {}
///
pub struct LabeledMetric {
name: &'static str,
label_values: RuntimeMetricLabelValues,
}
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.
impl LabeledMetric {
/// Increment the counter by `value`.
pub fn inc_by(&self, value: u64) {
let metric_update = RuntimeMetricUpdate {
metric_name: Vec::from(name),
op: RuntimeMetricOp::Register(RuntimeMetricRegisterParams::new(
Vec::from(description),
Some(labels.into()),
)),
metric_name: Vec::from(self.name),
op: RuntimeMetricOp::IncrementCounterVec(value, self.label_values.clone()),
};
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) {
pub fn inc(&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,
)),
};
impl MetricEmitter for LabeledMetric {}
impl MetricEmitter for Counter {}
Self::emit(&metric_update);
Counter { name }
impl CounterVec {
/// Create a new counter as specified by `definition`. This metric needs to be registered
/// in the client before it can be used.
pub const fn new(definition: CounterVecDefinition) -> Self {
// No register op is emitted since the metric is supposed to be registered
// on the client by the time `inc()` is called.
CounterVec { name: definition.name }
}
/// Returns a LabeledMetric instance that provides an interface for incrementing
/// the metric.
pub fn with_label_values(&self, label_values: &[&'static str]) -> LabeledMetric {
LabeledMetric { name: self.name, label_values: label_values.into() }
}
}
impl Counter {
/// Create a new counter as specified by `definition`. This metric needs to be registered
/// in the client before it can be used.
pub const fn new(definition: CounterDefinition) -> Self {
Counter { name: definition.name }
}
/// Increment counter by `value`.
pub fn inc_by(&mut self, value: u64) {
pub fn inc_by(&self, value: u64) {
let metric_update = RuntimeMetricUpdate {
metric_name: Vec::from(self.name),
op: RuntimeMetricOp::IncrementCounter(value),
@@ -120,7 +112,7 @@ impl Counter {
}
/// Increment counter.
pub fn inc(&mut self) {
pub fn inc(&self) {
self.inc_by(1);
}
}
@@ -18,6 +18,8 @@
//! provide a dummy implementation for the native runtime to avoid cluttering the runtime code
//! with `#[cfg(feature = "runtime-metrics")]`.
use primitives::v1::metric_definitions::{CounterDefinition, CounterVecDefinition};
/// A dummy Counter.
pub struct Counter;
/// A dummy CounterVec.
@@ -26,27 +28,27 @@ pub struct CounterVec;
/// Dummy implementation.
impl CounterVec {
/// Constructor.
pub fn new(_name: &'static str, _description: &'static str, _labels: &[&'static str]) -> Self {
pub const fn new(_definition: CounterVecDefinition) -> Self {
CounterVec
}
/// Sets label values, implementation is a `no op`.
pub fn with_label_values(&mut self, _label_values: &[&'static str]) -> &mut Self {
pub fn with_label_values(&self, _label_values: &[&'static str]) -> &Self {
self
}
/// Increment counter by value, implementation is a `no op`.
pub fn inc_by(&mut self, _: u64) {}
pub fn inc_by(&self, _: u64) {}
/// Increment counter, implementation is a `no op`.
pub fn inc(&mut self) {}
pub fn inc(&self) {}
}
/// Dummy implementation.
impl Counter {
/// Constructor.
pub fn new(_name: &'static str, _description: &'static str) -> Self {
pub const fn new(_definition: CounterDefinition) -> Self {
Counter
}
/// Increment counter by value, implementation is a `no op`.
pub fn inc_by(&mut self, _: u64) {}
pub fn inc_by(&self, _: u64) {}
/// Increment counter, implementation is a `no op`.
pub fn inc(&mut self) {}
pub fn inc(&self) {}
}