referrerpolicy=no-referrer-when-downgrade

polkadot_primitives/v9/
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//! Runtime metric primitives.
18
19use alloc::vec::Vec;
20use codec::{Decode, Encode};
21
22/// Runtime metric operations.
23#[derive(Encode, Decode)]
24#[cfg_attr(feature = "std", derive(Debug))]
25pub enum RuntimeMetricOp {
26	/// Increment a counter metric with labels by value.
27	IncrementCounterVec(u64, RuntimeMetricLabelValues),
28	/// Increment a counter metric by value.
29	IncrementCounter(u64),
30	/// Observe histogram value
31	ObserveHistogram(u128),
32}
33
34/// Runtime metric update event.
35#[derive(Encode, Decode)]
36#[cfg_attr(feature = "std", derive(Debug))]
37pub struct RuntimeMetricUpdate {
38	/// The name of the metric.
39	pub metric_name: Vec<u8>,
40	/// The operation applied to the metric.
41	pub op: RuntimeMetricOp,
42}
43
44fn vec_to_str<'a>(v: &'a Vec<u8>, default: &'static str) -> &'a str {
45	return alloc::str::from_utf8(v).unwrap_or(default)
46}
47
48impl RuntimeMetricLabels {
49	/// Returns a labels as `Vec<&str>`.
50	pub fn as_str_vec(&self) -> Vec<&str> {
51		self.0
52			.iter()
53			.map(|label_vec| vec_to_str(&label_vec.0, "invalid_label"))
54			.collect()
55	}
56
57	/// Return the inner values as vec.
58	pub fn clear(&mut self) {
59		self.0.clear();
60	}
61}
62
63impl From<&[&'static str]> for RuntimeMetricLabels {
64	fn from(v: &[&'static str]) -> RuntimeMetricLabels {
65		RuntimeMetricLabels(
66			v.iter().map(|label| RuntimeMetricLabel(label.as_bytes().to_vec())).collect(),
67		)
68	}
69}
70
71impl RuntimeMetricUpdate {
72	/// Returns the metric name.
73	pub fn metric_name(&self) -> &str {
74		vec_to_str(&self.metric_name, "invalid_metric_name")
75	}
76}
77
78/// A set of metric labels.
79#[derive(Clone, Default, Encode, Decode)]
80#[cfg_attr(feature = "std", derive(Debug))]
81pub struct RuntimeMetricLabels(Vec<RuntimeMetricLabel>);
82
83/// A metric label.
84#[derive(Clone, Default, Encode, Decode)]
85#[cfg_attr(feature = "std", derive(Debug))]
86pub struct RuntimeMetricLabel(Vec<u8>);
87
88/// A metric label value.
89pub type RuntimeMetricLabelValue = RuntimeMetricLabel;
90
91/// A set of metric label values.
92pub type RuntimeMetricLabelValues = RuntimeMetricLabels;
93
94impl From<&'static str> for RuntimeMetricLabel {
95	fn from(s: &'static str) -> Self {
96		Self(s.as_bytes().to_vec())
97	}
98}
99
100/// Contains all runtime metrics defined as constants.
101pub mod metric_definitions {
102	/// `Counter` metric definition.
103	pub struct CounterDefinition {
104		/// The name of the metric.
105		pub name: &'static str,
106		/// The description of the metric.
107		pub description: &'static str,
108	}
109
110	/// `CounterVec` metric definition.
111	pub struct CounterVecDefinition<'a> {
112		/// The name of the metric.
113		pub name: &'static str,
114		/// The description of the metric.
115		pub description: &'static str,
116		/// The label names of the metric.
117		pub labels: &'a [&'static str],
118	}
119
120	/// `Histogram` metric definition
121	pub struct HistogramDefinition<'a> {
122		/// The name of the metric.
123		pub name: &'static str,
124		/// The description of the metric.
125		pub description: &'static str,
126		/// The buckets for the histogram
127		pub buckets: &'a [f64],
128	}
129
130	/// Counts parachain inherent data weights. Use `before` and `after` labels to differentiate
131	/// between the weight before and after filtering.
132	pub const PARACHAIN_INHERENT_DATA_WEIGHT: CounterVecDefinition = CounterVecDefinition {
133		name: "polkadot_parachain_inherent_data_weight",
134		description: "Inherent data weight before and after filtering",
135		labels: &["when"],
136	};
137
138	/// Counts the number of bitfields processed in `process_inherent_data`.
139	pub const PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED: CounterDefinition = CounterDefinition {
140		name: "polkadot_parachain_inherent_data_bitfields_processed",
141		description: "Counts the number of bitfields processed in `process_inherent_data`.",
142	};
143
144	/// Counts the `total`, `sanitized` and `included` number of parachain block candidates
145	/// in `process_inherent_data`.
146	pub const PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED: CounterVecDefinition =
147		CounterVecDefinition {
148			name: "polkadot_parachain_inherent_data_candidates_processed",
149			description:
150				"Counts the number of parachain block candidates processed in `process_inherent_data`.",
151			labels: &["category"],
152		};
153
154	/// Counts the number of `imported`, `current` and `concluded_invalid` dispute statements sets
155	/// processed in `process_inherent_data`. The `current` label refers to the disputes statement
156	/// sets of the current session.
157	pub const PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED: CounterVecDefinition =
158		CounterVecDefinition {
159			name: "polkadot_parachain_inherent_data_dispute_sets_processed",
160			description:
161				"Counts the number of dispute statements sets processed in `process_inherent_data`.",
162			labels: &["category"],
163		};
164
165	/// Counts the number of `valid` and `invalid` bitfields signature checked in
166	/// `process_inherent_data`.
167	pub const PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS: CounterVecDefinition =
168		CounterVecDefinition {
169			name: "polkadot_parachain_create_inherent_bitfields_signature_checks",
170			description:
171				"Counts the number of bitfields signature checked in `process_inherent_data`.",
172			labels: &["validity"],
173		};
174
175	/// Measures how much time does it take to verify a single validator signature of a dispute
176	/// statement
177	pub const PARACHAIN_VERIFY_DISPUTE_SIGNATURE: HistogramDefinition =
178		HistogramDefinition {
179			name: "polkadot_parachain_verify_dispute_signature",
180			description: "How much time does it take to verify a single validator signature of a dispute statement, in seconds",
181			buckets: &[0.0, 0.00005, 0.00006, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.3, 0.5, 1.0],
182	};
183}