referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_backing/
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	pub(crate) signed_statements_total: prometheus::Counter<prometheus::U64>,
22	pub(crate) candidates_seconded_total: prometheus::Counter<prometheus::U64>,
23	pub(crate) process_second: prometheus::Histogram,
24	pub(crate) process_statement: prometheus::Histogram,
25	pub(crate) get_backed_candidates: prometheus::Histogram,
26}
27
28/// Candidate backing metrics.
29#[derive(Default, Clone)]
30pub struct Metrics(pub(crate) Option<MetricsInner>);
31
32impl Metrics {
33	pub fn on_statement_signed(&self) {
34		if let Some(metrics) = &self.0 {
35			metrics.signed_statements_total.inc();
36		}
37	}
38
39	pub fn on_candidate_seconded(&self) {
40		if let Some(metrics) = &self.0 {
41			metrics.candidates_seconded_total.inc();
42		}
43	}
44
45	/// Provide a timer for handling `CandidateBackingMessage:Second` which observes on drop.
46	pub fn time_process_second(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
47		self.0.as_ref().map(|metrics| metrics.process_second.start_timer())
48	}
49
50	/// Provide a timer for handling `CandidateBackingMessage::Statement` which observes on drop.
51	pub fn time_process_statement(
52		&self,
53	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
54		self.0.as_ref().map(|metrics| metrics.process_statement.start_timer())
55	}
56
57	/// Provide a timer for handling `CandidateBackingMessage::GetBackedCandidates` which observes
58	/// on drop.
59	pub fn time_get_backed_candidates(
60		&self,
61	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
62		self.0.as_ref().map(|metrics| metrics.get_backed_candidates.start_timer())
63	}
64}
65
66impl metrics::Metrics for Metrics {
67	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
68		let metrics = MetricsInner {
69			signed_statements_total: prometheus::register(
70				prometheus::Counter::new(
71					"polkadot_parachain_candidate_backing_signed_statements_total",
72					"Number of statements signed.",
73				)?,
74				registry,
75			)?,
76			candidates_seconded_total: prometheus::register(
77				prometheus::Counter::new(
78					"polkadot_parachain_candidate_backing_candidates_seconded_total",
79					"Number of candidates seconded.",
80				)?,
81				registry,
82			)?,
83			process_second: prometheus::register(
84				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
85					"polkadot_parachain_candidate_backing_process_second",
86					"Time spent within `candidate_backing::process_second`",
87				))?,
88				registry,
89			)?,
90			process_statement: prometheus::register(
91				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
92					"polkadot_parachain_candidate_backing_process_statement",
93					"Time spent within `candidate_backing::process_statement`",
94				))?,
95				registry,
96			)?,
97			get_backed_candidates: prometheus::register(
98				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
99					"polkadot_parachain_candidate_backing_get_backed_candidates",
100					"Time spent within `candidate_backing::get_backed_candidates`",
101				))?,
102				registry,
103			)?,
104		};
105		Ok(Metrics(Some(metrics)))
106	}
107}