referrerpolicy=no-referrer-when-downgrade

polkadot_availability_distribution/
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
17use polkadot_node_subsystem_util::{
18	metrics,
19	metrics::{
20		prometheus,
21		prometheus::{Counter, CounterVec, Opts, PrometheusError, Registry, U64},
22	},
23};
24
25/// Label for success counters.
26pub const SUCCEEDED: &'static str = "succeeded";
27
28/// Label for fail counters.
29pub const FAILED: &'static str = "failed";
30
31/// Label for chunks/PoVs that could not be served, because they were not available.
32pub const NOT_FOUND: &'static str = "not-found";
33
34/// Availability Distribution metrics.
35#[derive(Clone, Default)]
36pub struct Metrics(Option<MetricsInner>);
37
38#[derive(Clone)]
39struct MetricsInner {
40	/// Number of chunks fetched.
41	///
42	/// Note: The failed count gets incremented, when we were not able to fetch the chunk at all.
43	/// For times, where we failed downloading, but succeeded on the next try (with different
44	/// backers), see `retries`.
45	fetched_chunks: CounterVec<U64>,
46
47	/// Number of chunks served.
48	served_chunks: CounterVec<U64>,
49
50	/// Number of received fetch PoV responses.
51	fetched_povs: CounterVec<U64>,
52
53	/// Number of PoVs served.
54	served_povs: CounterVec<U64>,
55
56	/// Number of times our first set of validators did not provide the needed chunk and we had to
57	/// query further validators.
58	retries: Counter<U64>,
59}
60
61impl Metrics {
62	/// Create new dummy metrics, not reporting anything.
63	pub fn new_dummy() -> Self {
64		Metrics(None)
65	}
66
67	/// Increment counter on fetched labels.
68	pub fn on_fetch(&self, label: &'static str) {
69		if let Some(metrics) = &self.0 {
70			metrics.fetched_chunks.with_label_values(&[label]).inc()
71		}
72	}
73
74	/// Increment counter on served chunks.
75	pub fn on_served_chunk(&self, label: &'static str) {
76		if let Some(metrics) = &self.0 {
77			metrics.served_chunks.with_label_values(&[label]).inc()
78		}
79	}
80
81	/// Increment counter on fetched PoVs.
82	pub fn on_fetched_pov(&self, label: &'static str) {
83		if let Some(metrics) = &self.0 {
84			metrics.fetched_povs.with_label_values(&[label]).inc()
85		}
86	}
87
88	/// Increment counter on served PoVs.
89	pub fn on_served_pov(&self, label: &'static str) {
90		if let Some(metrics) = &self.0 {
91			metrics.served_povs.with_label_values(&[label]).inc()
92		}
93	}
94
95	/// Increment retry counter.
96	pub fn on_retry(&self) {
97		if let Some(metrics) = &self.0 {
98			metrics.retries.inc()
99		}
100	}
101}
102
103impl metrics::Metrics for Metrics {
104	fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
105		let metrics = MetricsInner {
106			fetched_chunks: prometheus::register(
107				CounterVec::new(
108					Opts::new(
109						"polkadot_parachain_fetched_chunks_total",
110						"Total number of fetched chunks.",
111					),
112					&["success"]
113				)?,
114				registry,
115			)?,
116			served_chunks: prometheus::register(
117				CounterVec::new(
118					Opts::new(
119						"polkadot_parachain_served_chunks_total",
120						"Total number of chunks served by this backer.",
121					),
122					&["success"]
123				)?,
124				registry,
125			)?,
126			fetched_povs: prometheus::register(
127				CounterVec::new(
128					Opts::new(
129						"polkadot_parachain_fetched_povs_total",
130						"Total number of povs fetches by this backer.",
131					),
132					&["success"]
133				)?,
134				registry,
135			)?,
136			served_povs: prometheus::register(
137				CounterVec::new(
138					Opts::new(
139						"polkadot_parachain_served_povs_total",
140						"Total number of povs served by this backer.",
141					),
142					&["success"]
143				)?,
144				registry,
145			)?,
146			retries: prometheus::register(
147				Counter::new(
148					"polkadot_parachain_fetch_retries_total",
149					"Number of times we did not succeed in fetching a chunk and needed to try more backers.",
150				)?,
151				registry,
152			)?,
153		};
154		Ok(Metrics(Some(metrics)))
155	}
156}