referrerpolicy=no-referrer-when-downgrade

polkadot_gossip_support/
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::{
18	metrics,
19	metrics::{
20		prometheus,
21		prometheus::{Gauge, PrometheusError, Registry, U64},
22	},
23};
24
25/// Dispute Distribution metrics.
26#[derive(Clone, Default)]
27pub struct Metrics(Option<MetricsInner>);
28
29#[derive(Clone)]
30struct MetricsInner {
31	/// Tracks authority status for producing relay chain blocks.
32	is_authority: Gauge<U64>,
33	/// Tracks authority status for parachain approval checking.
34	is_parachain_validator: Gauge<U64>,
35}
36
37impl Metrics {
38	/// Dummy constructor for testing.
39	#[cfg(test)]
40	pub fn new_dummy() -> Self {
41		Self(None)
42	}
43
44	/// Set the `relaychain validator` metric.
45	pub fn on_is_authority(&self) {
46		if let Some(metrics) = &self.0 {
47			metrics.is_authority.set(1);
48		}
49	}
50
51	/// Unset the `relaychain validator` metric.
52	pub fn on_is_not_authority(&self) {
53		if let Some(metrics) = &self.0 {
54			metrics.is_authority.set(0);
55		}
56	}
57
58	/// Set the `parachain validator` metric.
59	pub fn on_is_parachain_validator(&self) {
60		if let Some(metrics) = &self.0 {
61			metrics.is_parachain_validator.set(1);
62		}
63	}
64
65	/// Unset the `parachain validator` metric.
66	pub fn on_is_not_parachain_validator(&self) {
67		if let Some(metrics) = &self.0 {
68			metrics.is_parachain_validator.set(0);
69		}
70	}
71}
72
73impl metrics::Metrics for Metrics {
74	fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
75		let metrics = MetricsInner {
76			is_authority: prometheus::register(
77				Gauge::new("polkadot_node_is_active_validator", "Tracks if the validator is in the active set. \
78				Updates at session boundary.")?,
79				registry,
80			)?,
81			is_parachain_validator: prometheus::register(
82				Gauge::new("polkadot_node_is_parachain_validator", 
83				"Tracks if the validator participates in parachain consensus. Parachain validators are a \
84				subset of the active set validators that perform approval checking of all parachain candidates in a session.\
85				Updates at session boundary.")?,
86				registry,
87			)?,
88		};
89		Ok(Metrics(Some(metrics)))
90	}
91}