polkadot_primitives/v9/
metrics.rs1use alloc::vec::Vec;
20use codec::{Decode, Encode};
21
22#[derive(Encode, Decode)]
24#[cfg_attr(feature = "std", derive(Debug))]
25pub enum RuntimeMetricOp {
26 IncrementCounterVec(u64, RuntimeMetricLabelValues),
28 IncrementCounter(u64),
30 ObserveHistogram(u128),
32}
33
34#[derive(Encode, Decode)]
36#[cfg_attr(feature = "std", derive(Debug))]
37pub struct RuntimeMetricUpdate {
38 pub metric_name: Vec<u8>,
40 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 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 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 pub fn metric_name(&self) -> &str {
74 vec_to_str(&self.metric_name, "invalid_metric_name")
75 }
76}
77
78#[derive(Clone, Default, Encode, Decode)]
80#[cfg_attr(feature = "std", derive(Debug))]
81pub struct RuntimeMetricLabels(Vec<RuntimeMetricLabel>);
82
83#[derive(Clone, Default, Encode, Decode)]
85#[cfg_attr(feature = "std", derive(Debug))]
86pub struct RuntimeMetricLabel(Vec<u8>);
87
88pub type RuntimeMetricLabelValue = RuntimeMetricLabel;
90
91pub 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
100pub mod metric_definitions {
102 pub struct CounterDefinition {
104 pub name: &'static str,
106 pub description: &'static str,
108 }
109
110 pub struct CounterVecDefinition<'a> {
112 pub name: &'static str,
114 pub description: &'static str,
116 pub labels: &'a [&'static str],
118 }
119
120 pub struct HistogramDefinition<'a> {
122 pub name: &'static str,
124 pub description: &'static str,
126 pub buckets: &'a [f64],
128 }
129
130 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 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 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 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 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 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}