referrerpolicy=no-referrer-when-downgrade

polkadot_availability_bitfield_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::metrics::{prometheus, Metrics as MetricsTrait};
18
19#[derive(Clone)]
20struct MetricsInner {
21	sent_own_availability_bitfields: prometheus::Counter<prometheus::U64>,
22	received_availability_bitfields: prometheus::Counter<prometheus::U64>,
23	active_leaves_update: prometheus::Histogram,
24	handle_bitfield_distribution: prometheus::Histogram,
25	handle_network_msg: prometheus::Histogram,
26}
27
28/// Bitfield Distribution metrics.
29#[derive(Default, Clone)]
30pub struct Metrics(Option<MetricsInner>);
31
32impl Metrics {
33	pub(crate) fn on_own_bitfield_sent(&self) {
34		if let Some(metrics) = &self.0 {
35			metrics.sent_own_availability_bitfields.inc();
36		}
37	}
38
39	pub(crate) fn on_bitfield_received(&self) {
40		if let Some(metrics) = &self.0 {
41			metrics.received_availability_bitfields.inc();
42		}
43	}
44
45	/// Provide a timer for `active_leaves_update` which observes on drop.
46	pub(crate) fn time_active_leaves_update(
47		&self,
48	) -> Option<prometheus::prometheus::HistogramTimer> {
49		self.0.as_ref().map(|metrics| metrics.active_leaves_update.start_timer())
50	}
51
52	/// Provide a timer for `handle_bitfield_distribution` which observes on drop.
53	pub(crate) fn time_handle_bitfield_distribution(
54		&self,
55	) -> Option<prometheus::prometheus::HistogramTimer> {
56		self.0
57			.as_ref()
58			.map(|metrics| metrics.handle_bitfield_distribution.start_timer())
59	}
60
61	/// Provide a timer for `handle_network_msg` which observes on drop.
62	pub(crate) fn time_handle_network_msg(&self) -> Option<prometheus::prometheus::HistogramTimer> {
63		self.0.as_ref().map(|metrics| metrics.handle_network_msg.start_timer())
64	}
65}
66
67impl MetricsTrait for Metrics {
68	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
69		let metrics = MetricsInner {
70			sent_own_availability_bitfields: prometheus::register(
71				prometheus::Counter::new(
72					"polkadot_parachain_sent_own_availability_bitfields_total",
73					"Number of own availability bitfields sent to other peers.",
74				)?,
75				registry,
76			)?,
77			received_availability_bitfields: prometheus::register(
78				prometheus::Counter::new(
79					"polkadot_parachain_received_availability_bitfields_total",
80					"Number of valid availability bitfields received from other peers.",
81				)?,
82				registry,
83			)?,
84			active_leaves_update: prometheus::register(
85				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
86					"polkadot_parachain_bitfield_distribution_active_leaves_update",
87					"Time spent within `bitfield_distribution::active_leaves_update`",
88				))?,
89				registry,
90			)?,
91			handle_bitfield_distribution: prometheus::register(
92				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
93					"polkadot_parachain_bitfield_distribution_handle_bitfield_distribution",
94					"Time spent within `bitfield_distribution::handle_bitfield_distribution`",
95				))?,
96				registry,
97			)?,
98			handle_network_msg: prometheus::register(
99				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
100					"polkadot_parachain_bitfield_distribution_handle_network_msg",
101					"Time spent within `bitfield_distribution::handle_network_msg`",
102				))?,
103				registry,
104			)?,
105		};
106		Ok(Metrics(Some(metrics)))
107	}
108}