referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_metrics/
with_runtime_metrics.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! This module provides an implementation for the runtime metrics types: `Counter`,
18//! `CounterVec` and `Histogram`. These types expose a Prometheus like interface and
19//! same functionality. Each instance of a runtime metric is mapped to a Prometheus
20//! metric on the client side. The runtime metrics must be registered with the registry
21//! in the client, otherwise they will not be published.
22
23const TRACING_TARGET: &'static str = "metrics";
24
25use alloc::vec::Vec;
26use codec::Encode;
27use polkadot_primitives::{
28	metric_definitions::{CounterDefinition, CounterVecDefinition, HistogramDefinition},
29	RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricUpdate,
30};
31
32/// Holds a set of counters that have different values for their labels,
33/// like Prometheus `CounterVec`.
34pub struct CounterVec {
35	name: &'static str,
36}
37
38/// A counter metric.
39pub struct Counter {
40	name: &'static str,
41}
42
43pub struct Histogram {
44	name: &'static str,
45}
46
47/// Convenience trait implemented for all metric types.
48trait MetricEmitter {
49	fn emit(metric_op: &RuntimeMetricUpdate) {
50		sp_tracing::event!(
51			target: TRACING_TARGET,
52			sp_tracing::Level::TRACE,
53			update_op = bs58::encode(&metric_op.encode()).into_string().as_str()
54		);
55	}
56}
57
58///
59pub struct LabeledMetric {
60	name: &'static str,
61	label_values: RuntimeMetricLabelValues,
62}
63
64impl LabeledMetric {
65	/// Increment the counter by `value`.
66	pub fn inc_by(&self, value: u64) {
67		let metric_update = RuntimeMetricUpdate {
68			metric_name: Vec::from(self.name),
69			op: RuntimeMetricOp::IncrementCounterVec(value, self.label_values.clone()),
70		};
71
72		Self::emit(&metric_update);
73	}
74
75	/// Increment the counter value.
76	pub fn inc(&self) {
77		self.inc_by(1);
78	}
79}
80
81impl MetricEmitter for LabeledMetric {}
82impl MetricEmitter for Counter {}
83impl MetricEmitter for Histogram {}
84
85impl CounterVec {
86	/// Create a new counter as specified by `definition`. This metric needs to be registered
87	/// in the client before it can be used.
88	pub const fn new(definition: CounterVecDefinition) -> Self {
89		// No register op is emitted since the metric is supposed to be registered
90		// on the client by the time `inc()` is called.
91		CounterVec { name: definition.name }
92	}
93
94	/// Returns a `LabeledMetric` instance that provides an interface for incrementing
95	/// the metric.
96	pub fn with_label_values(&self, label_values: &[&'static str]) -> LabeledMetric {
97		LabeledMetric { name: self.name, label_values: label_values.into() }
98	}
99}
100
101impl Counter {
102	/// Create a new counter as specified by `definition`. This metric needs to be registered
103	/// in the client before it can be used.
104	pub const fn new(definition: CounterDefinition) -> Self {
105		Counter { name: definition.name }
106	}
107
108	/// Increment counter by `value`.
109	pub fn inc_by(&self, value: u64) {
110		let metric_update = RuntimeMetricUpdate {
111			metric_name: Vec::from(self.name),
112			op: RuntimeMetricOp::IncrementCounter(value),
113		};
114
115		Self::emit(&metric_update);
116	}
117
118	/// Increment counter.
119	pub fn inc(&self) {
120		self.inc_by(1);
121	}
122}
123
124impl Histogram {
125	/// Create a new histogram as specified by `definition`. This metric needs to be registered
126	/// in the client before it can be used.
127	pub const fn new(definition: HistogramDefinition) -> Self {
128		// No register op is emitted since the metric is supposed to be registered
129		// on the client by the time `inc()` is called.
130		Histogram { name: definition.name }
131	}
132
133	// Observe a value in the histogram
134	pub fn observe(&self, value: u128) {
135		let metric_update = RuntimeMetricUpdate {
136			metric_name: Vec::from(self.name),
137			op: RuntimeMetricOp::ObserveHistogram(value),
138		};
139		Self::emit(&metric_update);
140	}
141}
142
143/// Returns current time in ns
144pub fn get_current_time() -> u128 {
145	frame_benchmarking::current_time()
146}