referrerpolicy=no-referrer-when-downgrade

polkadot_collator_protocol/validator_side/
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, Default)]
20pub struct Metrics(Option<MetricsInner>);
21
22impl Metrics {
23	pub fn on_request(&self, succeeded: std::result::Result<(), ()>) {
24		if let Some(metrics) = &self.0 {
25			match succeeded {
26				Ok(()) => metrics.collation_requests.with_label_values(&["succeeded"]).inc(),
27				Err(()) => metrics.collation_requests.with_label_values(&["failed"]).inc(),
28			}
29		}
30	}
31
32	/// Provide a timer for `process_msg` which observes on drop.
33	pub fn time_process_msg(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
34		self.0.as_ref().map(|metrics| metrics.process_msg.start_timer())
35	}
36
37	/// Provide a timer for `handle_collation_request_result` which observes on drop.
38	pub fn time_handle_collation_request_result(
39		&self,
40	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
41		self.0
42			.as_ref()
43			.map(|metrics| metrics.handle_collation_request_result.start_timer())
44	}
45
46	/// Note the current number of collator peers.
47	pub fn note_collator_peer_count(&self, collator_peers: usize) {
48		self.0
49			.as_ref()
50			.map(|metrics| metrics.collator_peer_count.set(collator_peers as u64));
51	}
52
53	/// Provide a timer for `CollationFetchRequest` structure which observes on drop.
54	pub fn time_collation_request_duration(
55		&self,
56	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
57		self.0.as_ref().map(|metrics| metrics.collation_request_duration.start_timer())
58	}
59
60	/// Provide a timer for `request_unblocked_collations` which observes on drop.
61	pub fn time_request_unblocked_collations(
62		&self,
63	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
64		self.0
65			.as_ref()
66			.map(|metrics| metrics.request_unblocked_collations.start_timer())
67	}
68}
69
70#[derive(Clone)]
71struct MetricsInner {
72	collation_requests: prometheus::CounterVec<prometheus::U64>,
73	process_msg: prometheus::Histogram,
74	handle_collation_request_result: prometheus::Histogram,
75	collator_peer_count: prometheus::Gauge<prometheus::U64>,
76	collation_request_duration: prometheus::Histogram,
77	request_unblocked_collations: prometheus::Histogram,
78}
79
80impl metrics::Metrics for Metrics {
81	fn try_register(
82		registry: &prometheus::Registry,
83	) -> std::result::Result<Self, prometheus::PrometheusError> {
84		let metrics = MetricsInner {
85			collation_requests: prometheus::register(
86				prometheus::CounterVec::new(
87					prometheus::Opts::new(
88						"polkadot_parachain_collation_requests_total",
89						"Number of collations requested from Collators.",
90					),
91					&["success"],
92				)?,
93				registry,
94			)?,
95			process_msg: prometheus::register(
96				prometheus::Histogram::with_opts(
97					prometheus::HistogramOpts::new(
98						"polkadot_parachain_collator_protocol_validator_process_msg",
99						"Time spent within `collator_protocol_validator::process_msg`",
100					)
101				)?,
102				registry,
103			)?,
104			handle_collation_request_result: prometheus::register(
105				prometheus::Histogram::with_opts(
106					prometheus::HistogramOpts::new(
107						"polkadot_parachain_collator_protocol_validator_handle_collation_request_result",
108						"Time spent within `collator_protocol_validator::handle_collation_request_result`",
109					)
110				)?,
111				registry,
112			)?,
113			collator_peer_count: prometheus::register(
114				prometheus::Gauge::new(
115					"polkadot_parachain_collator_peer_count",
116					"Amount of collator peers connected",
117				)?,
118				registry,
119			)?,
120			collation_request_duration: prometheus::register(
121				prometheus::Histogram::with_opts(
122					prometheus::HistogramOpts::new(
123						"polkadot_parachain_collator_protocol_validator_collation_request_duration",
124						"Lifetime of the `CollationFetchRequest` structure",
125					).buckets(vec![0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.75, 0.9, 1.0, 1.2, 1.5, 1.75]),
126				)?,
127				registry,
128			)?,
129			request_unblocked_collations: prometheus::register(
130				prometheus::Histogram::with_opts(
131					prometheus::HistogramOpts::new(
132						"polkadot_parachain_collator_protocol_validator_request_unblocked_collations",
133						"Time spent within `collator_protocol_validator::request_unblocked_collations`",
134					)
135				)?,
136				registry,
137			)?,
138		};
139
140		Ok(Metrics(Some(metrics)))
141	}
142}