referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_candidate_validation/
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 super::{ValidationFailed, ValidationResult};
18use polkadot_node_metrics::metrics::{self, prometheus};
19
20#[derive(Clone)]
21pub(crate) struct MetricsInner {
22	pub(crate) validation_requests: prometheus::CounterVec<prometheus::U64>,
23	pub(crate) validate_from_exhaustive: prometheus::Histogram,
24	pub(crate) validate_candidate_exhaustive: prometheus::Histogram,
25}
26
27/// Candidate validation metrics.
28#[derive(Default, Clone)]
29pub struct Metrics(Option<MetricsInner>);
30
31impl Metrics {
32	pub fn on_validation_event(&self, event: &Result<ValidationResult, ValidationFailed>) {
33		if let Some(metrics) = &self.0 {
34			match event {
35				Ok(ValidationResult::Valid(_, _)) => {
36					metrics.validation_requests.with_label_values(&["valid"]).inc();
37				},
38				Ok(ValidationResult::Invalid(_)) => {
39					metrics.validation_requests.with_label_values(&["invalid"]).inc();
40				},
41				Err(_) => {
42					metrics.validation_requests.with_label_values(&["validation failure"]).inc();
43				},
44			}
45		}
46	}
47
48	/// Provide a timer for `validate_from_exhaustive` which observes on drop.
49	pub fn time_validate_from_exhaustive(
50		&self,
51	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
52		self.0.as_ref().map(|metrics| metrics.validate_from_exhaustive.start_timer())
53	}
54
55	/// Provide a timer for `validate_candidate_exhaustive` which observes on drop.
56	pub fn time_validate_candidate_exhaustive(
57		&self,
58	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
59		self.0
60			.as_ref()
61			.map(|metrics| metrics.validate_candidate_exhaustive.start_timer())
62	}
63}
64
65impl metrics::Metrics for Metrics {
66	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
67		let metrics = MetricsInner {
68			validation_requests: prometheus::register(
69				prometheus::CounterVec::new(
70					prometheus::Opts::new(
71						"polkadot_parachain_validation_requests_total",
72						"Number of validation requests served.",
73					),
74					&["validity"],
75				)?,
76				registry,
77			)?,
78			validate_from_exhaustive: prometheus::register(
79				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
80					"polkadot_parachain_candidate_validation_validate_from_exhaustive",
81					"Time spent within `candidate_validation::validate_from_exhaustive`",
82				))?,
83				registry,
84			)?,
85			validate_candidate_exhaustive: prometheus::register(
86				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
87					"polkadot_parachain_candidate_validation_validate_candidate_exhaustive",
88					"Time spent within `candidate_validation::validate_candidate_exhaustive`",
89				))?,
90				registry,
91			)?,
92		};
93		Ok(Metrics(Some(metrics)))
94	}
95}