referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Metrics definitions for the PVF pre-checking subsystem.

use polkadot_node_subsystem_util::metrics::{self, prometheus};

#[derive(Clone)]
struct MetricsInner {
	pre_check_judgement: prometheus::Histogram,
	votes_total: prometheus::Counter<prometheus::U64>,
	votes_started: prometheus::Counter<prometheus::U64>,
	votes_duplicate: prometheus::Counter<prometheus::U64>,
	pvfs_observed: prometheus::Counter<prometheus::U64>,
	pvfs_left: prometheus::Counter<prometheus::U64>,
}

#[derive(Default, Clone)]
pub struct Metrics(Option<MetricsInner>);

impl Metrics {
	/// Time between sending the pre-check request to receiving the response.
	pub(crate) fn time_pre_check_judgement(
		&self,
	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
		self.0.as_ref().map(|metrics| metrics.pre_check_judgement.start_timer())
	}

	/// Called when a PVF vote/statement is submitted.
	pub(crate) fn on_vote_submitted(&self) {
		if let Some(metrics) = &self.0 {
			metrics.votes_total.inc();
		}
	}

	/// Called when a PVF vote/statement is started submission.
	pub(crate) fn on_vote_submission_started(&self) {
		if let Some(metrics) = &self.0 {
			metrics.votes_started.inc();
		}
	}

	/// Called when the vote is a duplicate.
	pub(crate) fn on_vote_duplicate(&self) {
		if let Some(metrics) = &self.0 {
			metrics.votes_duplicate.inc();
		}
	}

	/// Called when a new PVF is observed.
	pub(crate) fn on_pvf_observed(&self, num: usize) {
		if let Some(metrics) = &self.0 {
			metrics.pvfs_observed.inc_by(num as u64);
		}
	}

	/// Called when a PVF left the view.
	pub(crate) fn on_pvf_left(&self, num: usize) {
		if let Some(metrics) = &self.0 {
			metrics.pvfs_left.inc_by(num as u64);
		}
	}
}

impl metrics::Metrics for Metrics {
	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
		let metrics = MetricsInner {
			pre_check_judgement: prometheus::register(
				prometheus::Histogram::with_opts(
					prometheus::HistogramOpts::new(
						"polkadot_pvf_precheck_judgement",
						"Time between sending the pre-check request to receiving the response.",
					)
					.buckets(vec![0.1, 0.5, 1.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0]),
				)?,
				registry,
			)?,
			votes_total: prometheus::register(
				prometheus::Counter::new(
					"polkadot_pvf_precheck_votes_total",
					"The total number of votes submitted.",
				)?,
				registry,
			)?,
			votes_started: prometheus::register(
				prometheus::Counter::new(
					"polkadot_pvf_precheck_votes_started",
					"The number of votes that are pending submission",
				)?,
				registry,
			)?,
			votes_duplicate: prometheus::register(
				prometheus::Counter::new(
					"polkadot_pvf_precheck_votes_duplicate",
					"The number of votes that are submitted more than once for the same code within\
the same session.",
				)?,
				registry,
			)?,
			pvfs_observed: prometheus::register(
				prometheus::Counter::new(
					"polkadot_pvf_precheck_pvfs_observed",
					"The number of new PVFs observed.",
				)?,
				registry,
			)?,
			pvfs_left: prometheus::register(
				prometheus::Counter::new(
					"polkadot_pvf_precheck_pvfs_left",
					"The number of PVFs removed from the view.",
				)?,
				registry,
			)?,
		};
		Ok(Self(Some(metrics)))
	}
}