referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_av_store/
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::metrics::{self, prometheus};
18
19#[derive(Clone)]
20pub(crate) struct MetricsInner {
21	received_availability_chunks_total: prometheus::Counter<prometheus::U64>,
22	pruning: prometheus::Histogram,
23	process_block_finalized: prometheus::Histogram,
24	block_activated: prometheus::Histogram,
25	process_message: prometheus::Histogram,
26	store_available_data: prometheus::Histogram,
27	store_chunk: prometheus::Histogram,
28	get_chunk: prometheus::Histogram,
29}
30
31/// Availability metrics.
32#[derive(Default, Clone)]
33pub struct Metrics(Option<MetricsInner>);
34
35impl Metrics {
36	pub(crate) fn on_chunks_received(&self, count: usize) {
37		if let Some(metrics) = &self.0 {
38			// assume usize fits into u64
39			let by = u64::try_from(count).unwrap_or_default();
40			metrics.received_availability_chunks_total.inc_by(by);
41		}
42	}
43
44	/// Provide a timer for `prune_povs` which observes on drop.
45	pub(crate) fn time_pruning(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
46		self.0.as_ref().map(|metrics| metrics.pruning.start_timer())
47	}
48
49	/// Provide a timer for `process_block_finalized` which observes on drop.
50	pub(crate) fn time_process_block_finalized(
51		&self,
52	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
53		self.0.as_ref().map(|metrics| metrics.process_block_finalized.start_timer())
54	}
55
56	/// Provide a timer for `block_activated` which observes on drop.
57	pub(crate) fn time_block_activated(
58		&self,
59	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
60		self.0.as_ref().map(|metrics| metrics.block_activated.start_timer())
61	}
62
63	/// Provide a timer for `process_message` which observes on drop.
64	pub(crate) fn time_process_message(
65		&self,
66	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
67		self.0.as_ref().map(|metrics| metrics.process_message.start_timer())
68	}
69
70	/// Provide a timer for `store_available_data` which observes on drop.
71	pub(crate) fn time_store_available_data(
72		&self,
73	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
74		self.0.as_ref().map(|metrics| metrics.store_available_data.start_timer())
75	}
76
77	/// Provide a timer for `store_chunk` which observes on drop.
78	pub(crate) fn time_store_chunk(
79		&self,
80	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
81		self.0.as_ref().map(|metrics| metrics.store_chunk.start_timer())
82	}
83
84	/// Provide a timer for `get_chunk` which observes on drop.
85	pub(crate) fn time_get_chunk(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
86		self.0.as_ref().map(|metrics| metrics.get_chunk.start_timer())
87	}
88}
89
90impl metrics::Metrics for Metrics {
91	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
92		let metrics = MetricsInner {
93			received_availability_chunks_total: prometheus::register(
94				prometheus::Counter::new(
95					"polkadot_parachain_received_availability_chunks_total",
96					"Number of availability chunks received.",
97				)?,
98				registry,
99			)?,
100			pruning: prometheus::register(
101				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
102					"polkadot_parachain_av_store_pruning",
103					"Time spent within `av_store::prune_all`",
104				))?,
105				registry,
106			)?,
107			process_block_finalized: prometheus::register(
108				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
109					"polkadot_parachain_av_store_process_block_finalized",
110					"Time spent within `av_store::process_block_finalized`",
111				))?,
112				registry,
113			)?,
114			block_activated: prometheus::register(
115				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
116					"polkadot_parachain_av_store_block_activated",
117					"Time spent within `av_store::process_block_activated`",
118				))?,
119				registry,
120			)?,
121			process_message: prometheus::register(
122				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
123					"polkadot_parachain_av_store_process_message",
124					"Time spent within `av_store::process_message`",
125				))?,
126				registry,
127			)?,
128			store_available_data: prometheus::register(
129				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
130					"polkadot_parachain_av_store_store_available_data",
131					"Time spent within `av_store::store_available_data`",
132				))?,
133				registry,
134			)?,
135			store_chunk: prometheus::register(
136				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
137					"polkadot_parachain_av_store_store_chunk",
138					"Time spent within `av_store::store_chunk`",
139				))?,
140				registry,
141			)?,
142			get_chunk: prometheus::register(
143				prometheus::Histogram::with_opts(
144					prometheus::HistogramOpts::new(
145						"polkadot_parachain_av_store_get_chunk",
146						"Time spent fetching requested chunks.`",
147					)
148					.buckets(vec![
149						0.000625, 0.00125, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.1, 0.25,
150						0.5, 1.0, 2.5, 5.0, 10.0,
151					]),
152				)?,
153				registry,
154			)?,
155		};
156		Ok(Metrics(Some(metrics)))
157	}
158}