referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_prospective_parachains/
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::prometheus::Opts;
18use polkadot_node_subsystem_util::metrics::{
19	self,
20	prometheus::{self, Gauge, GaugeVec, U64},
21};
22
23#[derive(Clone)]
24pub(crate) struct MetricsInner {
25	time_active_leaves_update: prometheus::Histogram,
26	time_introduce_seconded_candidate: prometheus::Histogram,
27	time_candidate_backed: prometheus::Histogram,
28	time_hypothetical_membership: prometheus::Histogram,
29	candidate_count: prometheus::GaugeVec<U64>,
30	active_leaves_count: prometheus::GaugeVec<U64>,
31	implicit_view_candidate_count: prometheus::Gauge<U64>,
32}
33
34/// Candidate backing metrics.
35#[derive(Default, Clone)]
36pub struct Metrics(pub(crate) Option<MetricsInner>);
37
38impl Metrics {
39	/// Provide a timer for handling `ActiveLeavesUpdate` which observes on drop.
40	pub fn time_handle_active_leaves_update(
41		&self,
42	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
43		self.0.as_ref().map(|metrics| metrics.time_active_leaves_update.start_timer())
44	}
45
46	/// Provide a timer for handling `IntroduceSecondedCandidate` which observes on drop.
47	pub fn time_introduce_seconded_candidate(
48		&self,
49	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
50		self.0
51			.as_ref()
52			.map(|metrics| metrics.time_introduce_seconded_candidate.start_timer())
53	}
54
55	/// Provide a timer for handling `CandidateBacked` which observes on drop.
56	pub fn time_candidate_backed(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
57		self.0.as_ref().map(|metrics| metrics.time_candidate_backed.start_timer())
58	}
59
60	/// Provide a timer for handling `GetHypotheticalMembership` which observes on drop.
61	pub fn time_hypothetical_membership_request(
62		&self,
63	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
64		self.0
65			.as_ref()
66			.map(|metrics| metrics.time_hypothetical_membership.start_timer())
67	}
68
69	/// Record number of candidates across all fragment chains. First param is the connected
70	/// candidates count, second param is the unconnected candidates count.
71	pub fn record_candidate_count(&self, connected_count: u64, unconnected_count: u64) {
72		self.0.as_ref().map(|metrics| {
73			metrics.candidate_count.with_label_values(&["connected"]).set(connected_count);
74			metrics
75				.candidate_count
76				.with_label_values(&["unconnected"])
77				.set(unconnected_count);
78		});
79	}
80
81	/// Record the number of candidates present in the implicit view of the subsystem.
82	pub fn record_candidate_count_in_implicit_view(&self, count: u64) {
83		self.0.as_ref().map(|metrics| {
84			metrics.implicit_view_candidate_count.set(count);
85		});
86	}
87
88	/// Record the number of active/inactive leaves kept by the subsystem.
89	pub fn record_leaves_count(&self, active_count: u64, inactive_count: u64) {
90		self.0.as_ref().map(|metrics| {
91			metrics.active_leaves_count.with_label_values(&["active"]).set(active_count);
92			metrics.active_leaves_count.with_label_values(&["inactive"]).set(inactive_count);
93		});
94	}
95}
96
97impl metrics::Metrics for Metrics {
98	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
99		let metrics = MetricsInner {
100			time_active_leaves_update: prometheus::register(
101				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
102					"polkadot_parachain_prospective_parachains_time_active_leaves_update",
103					"Time spent within `prospective_parachains::handle_active_leaves_update`",
104				))?,
105				registry,
106			)?,
107			time_introduce_seconded_candidate: prometheus::register(
108				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
109					"polkadot_parachain_prospective_parachains_time_introduce_seconded_candidate",
110					"Time spent within `prospective_parachains::handle_introduce_seconded_candidate`",
111				))?,
112				registry,
113			)?,
114			time_candidate_backed: prometheus::register(
115				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
116					"polkadot_parachain_prospective_parachains_time_candidate_backed",
117					"Time spent within `prospective_parachains::handle_candidate_backed`",
118				))?,
119				registry,
120			)?,
121			time_hypothetical_membership: prometheus::register(
122				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
123					"polkadot_parachain_prospective_parachains_time_hypothetical_membership",
124					"Time spent responding to `GetHypotheticalMembership`",
125				))?,
126				registry,
127			)?,
128			candidate_count: prometheus::register(
129				GaugeVec::new(
130					Opts::new(
131						"polkadot_parachain_prospective_parachains_candidate_count",
132						"Number of candidates present across all fragment chains, split by connected and unconnected"
133					),
134					&["type"],
135				)?,
136				registry,
137			)?,
138			active_leaves_count: prometheus::register(
139				GaugeVec::new(
140					Opts::new(
141						"polkadot_parachain_prospective_parachains_active_leaves_count",
142						"Number of leaves kept by the subsystem, split by active/inactive"
143					),
144					&["type"],
145				)?,
146				registry,
147			)?,
148			implicit_view_candidate_count: prometheus::register(
149				Gauge::new(
150					"polkadot_parachain_prospective_parachains_implicit_view_candidate_count", 
151					"Number of candidates present in the implicit view"
152				)?,
153				registry
154			)?,
155		};
156		Ok(Metrics(Some(metrics)))
157	}
158}