referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_pvf_checker/
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
17//! Metrics definitions for the PVF pre-checking subsystem.
18
19use polkadot_node_subsystem_util::metrics::{self, prometheus};
20
21#[derive(Clone)]
22struct MetricsInner {
23	pre_check_judgement: prometheus::Histogram,
24	votes_total: prometheus::Counter<prometheus::U64>,
25	votes_started: prometheus::Counter<prometheus::U64>,
26	votes_duplicate: prometheus::Counter<prometheus::U64>,
27	pvfs_observed: prometheus::Counter<prometheus::U64>,
28	pvfs_left: prometheus::Counter<prometheus::U64>,
29}
30
31#[derive(Default, Clone)]
32pub struct Metrics(Option<MetricsInner>);
33
34impl Metrics {
35	/// Time between sending the pre-check request to receiving the response.
36	pub(crate) fn time_pre_check_judgement(
37		&self,
38	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
39		self.0.as_ref().map(|metrics| metrics.pre_check_judgement.start_timer())
40	}
41
42	/// Called when a PVF vote/statement is submitted.
43	pub(crate) fn on_vote_submitted(&self) {
44		if let Some(metrics) = &self.0 {
45			metrics.votes_total.inc();
46		}
47	}
48
49	/// Called when a PVF vote/statement is started submission.
50	pub(crate) fn on_vote_submission_started(&self) {
51		if let Some(metrics) = &self.0 {
52			metrics.votes_started.inc();
53		}
54	}
55
56	/// Called when the vote is a duplicate.
57	pub(crate) fn on_vote_duplicate(&self) {
58		if let Some(metrics) = &self.0 {
59			metrics.votes_duplicate.inc();
60		}
61	}
62
63	/// Called when a new PVF is observed.
64	pub(crate) fn on_pvf_observed(&self, num: usize) {
65		if let Some(metrics) = &self.0 {
66			metrics.pvfs_observed.inc_by(num as u64);
67		}
68	}
69
70	/// Called when a PVF left the view.
71	pub(crate) fn on_pvf_left(&self, num: usize) {
72		if let Some(metrics) = &self.0 {
73			metrics.pvfs_left.inc_by(num as u64);
74		}
75	}
76}
77
78impl metrics::Metrics for Metrics {
79	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
80		let metrics = MetricsInner {
81			pre_check_judgement: prometheus::register(
82				prometheus::Histogram::with_opts(
83					prometheus::HistogramOpts::new(
84						"polkadot_pvf_precheck_judgement",
85						"Time between sending the pre-check request to receiving the response.",
86					)
87					.buckets(vec![0.1, 0.5, 1.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0]),
88				)?,
89				registry,
90			)?,
91			votes_total: prometheus::register(
92				prometheus::Counter::new(
93					"polkadot_pvf_precheck_votes_total",
94					"The total number of votes submitted.",
95				)?,
96				registry,
97			)?,
98			votes_started: prometheus::register(
99				prometheus::Counter::new(
100					"polkadot_pvf_precheck_votes_started",
101					"The number of votes that are pending submission",
102				)?,
103				registry,
104			)?,
105			votes_duplicate: prometheus::register(
106				prometheus::Counter::new(
107					"polkadot_pvf_precheck_votes_duplicate",
108					"The number of votes that are submitted more than once for the same code within\
109the same session.",
110				)?,
111				registry,
112			)?,
113			pvfs_observed: prometheus::register(
114				prometheus::Counter::new(
115					"polkadot_pvf_precheck_pvfs_observed",
116					"The number of new PVFs observed.",
117				)?,
118				registry,
119			)?,
120			pvfs_left: prometheus::register(
121				prometheus::Counter::new(
122					"polkadot_pvf_precheck_pvfs_left",
123					"The number of PVFs removed from the view.",
124				)?,
125				registry,
126			)?,
127		};
128		Ok(Self(Some(metrics)))
129	}
130}