1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
34// 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.
89// 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.
1314// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1617//! 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.
2223const TRACING_TARGET: &'static str = "metrics";
2425use alloc::vec::Vec;
26use codec::Encode;
27use polkadot_primitives::{
28 metric_definitions::{CounterDefinition, CounterVecDefinition, HistogramDefinition},
29 RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricUpdate,
30};
3132/// Holds a set of counters that have different values for their labels,
33/// like Prometheus `CounterVec`.
34pub struct CounterVec {
35 name: &'static str,
36}
3738/// A counter metric.
39pub struct Counter {
40 name: &'static str,
41}
4243pub struct Histogram {
44 name: &'static str,
45}
4647/// Convenience trait implemented for all metric types.
48trait MetricEmitter {
49fn emit(metric_op: &RuntimeMetricUpdate) {
50sp_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}
5758///
59pub struct LabeledMetric {
60 name: &'static str,
61 label_values: RuntimeMetricLabelValues,
62}
6364impl LabeledMetric {
65/// Increment the counter by `value`.
66pub fn inc_by(&self, value: u64) {
67let metric_update = RuntimeMetricUpdate {
68 metric_name: Vec::from(self.name),
69 op: RuntimeMetricOp::IncrementCounterVec(value, self.label_values.clone()),
70 };
7172Self::emit(&metric_update);
73 }
7475/// Increment the counter value.
76pub fn inc(&self) {
77self.inc_by(1);
78 }
79}
8081impl MetricEmitter for LabeledMetric {}
82impl MetricEmitter for Counter {}
83impl MetricEmitter for Histogram {}
8485impl 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.
88pub 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.
91CounterVec { name: definition.name }
92 }
9394/// Returns a `LabeledMetric` instance that provides an interface for incrementing
95 /// the metric.
96pub fn with_label_values(&self, label_values: &[&'static str]) -> LabeledMetric {
97 LabeledMetric { name: self.name, label_values: label_values.into() }
98 }
99}
100101impl 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.
104pub const fn new(definition: CounterDefinition) -> Self {
105 Counter { name: definition.name }
106 }
107108/// Increment counter by `value`.
109pub fn inc_by(&self, value: u64) {
110let metric_update = RuntimeMetricUpdate {
111 metric_name: Vec::from(self.name),
112 op: RuntimeMetricOp::IncrementCounter(value),
113 };
114115Self::emit(&metric_update);
116 }
117118/// Increment counter.
119pub fn inc(&self) {
120self.inc_by(1);
121 }
122}
123124impl 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.
127pub 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.
130Histogram { name: definition.name }
131 }
132133// Observe a value in the histogram
134pub fn observe(&self, value: u128) {
135let metric_update = RuntimeMetricUpdate {
136 metric_name: Vec::from(self.name),
137 op: RuntimeMetricOp::ObserveHistogram(value),
138 };
139Self::emit(&metric_update);
140 }
141}
142143/// Returns current time in ns
144pub fn get_current_time() -> u128 {
145 frame_benchmarking::current_time()
146}