referrerpolicy=no-referrer-when-downgrade

polkadot_node_collation_generation/
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) collations_generated_total: prometheus::Counter<prometheus::U64>,
22	pub(crate) new_activation: prometheus::Histogram,
23	pub(crate) submit_collation: prometheus::Histogram,
24}
25
26/// `CollationGenerationSubsystem` metrics.
27#[derive(Default, Clone)]
28pub struct Metrics(pub(crate) Option<MetricsInner>);
29
30impl Metrics {
31	pub fn on_collation_generated(&self) {
32		if let Some(metrics) = &self.0 {
33			metrics.collations_generated_total.inc();
34		}
35	}
36
37	/// Provide a timer for new activations which updates on drop.
38	pub fn time_new_activation(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
39		self.0.as_ref().map(|metrics| metrics.new_activation.start_timer())
40	}
41
42	/// Provide a timer for submitting a collation which updates on drop.
43	pub fn time_submit_collation(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
44		self.0.as_ref().map(|metrics| metrics.submit_collation.start_timer())
45	}
46}
47
48impl metrics::Metrics for Metrics {
49	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
50		let metrics = MetricsInner {
51			collations_generated_total: prometheus::register(
52				prometheus::Counter::new(
53					"polkadot_parachain_collations_generated_total",
54					"Number of collations generated.",
55				)?,
56				registry,
57			)?,
58			new_activation: prometheus::register(
59				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
60					"polkadot_parachain_collation_generation_new_activations",
61					"Time spent within fn handle_new_activation",
62				))?,
63				registry,
64			)?,
65			submit_collation: prometheus::register(
66				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
67					"polkadot_parachain_collation_generation_submit_collation",
68					"Time spent preparing and submitting a collation to the network protocol",
69				))?,
70				registry,
71			)?,
72		};
73		Ok(Metrics(Some(metrics)))
74	}
75}