referrerpolicy=no-referrer-when-downgrade

finality_relay/
sync_loop_metrics.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common 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// Parity Bridges Common 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 Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Metrics for headers synchronization relay loop.
18
19use relay_utils::{
20	metrics::{metric_name, register, IntGauge, Metric, PrometheusError, Registry},
21	UniqueSaturatedInto,
22};
23
24/// Headers sync metrics.
25#[derive(Clone)]
26pub struct SyncLoopMetrics {
27	/// Best syncing header at the source.
28	best_source_block_number: IntGauge,
29	/// Best syncing header at the target.
30	best_target_block_number: IntGauge,
31	/// Flag that has `0` value when best source headers at the source node and at-target-chain
32	/// are matching and `1` otherwise.
33	using_different_forks: IntGauge,
34}
35
36impl SyncLoopMetrics {
37	/// Create and register headers loop metrics.
38	pub fn new(
39		prefix: Option<&str>,
40		at_source_chain_label: &str,
41		at_target_chain_label: &str,
42	) -> Result<Self, PrometheusError> {
43		Ok(SyncLoopMetrics {
44			best_source_block_number: IntGauge::new(
45				metric_name(prefix, &format!("best_{at_source_chain_label}_block_number")),
46				format!("Best block number at the {at_source_chain_label}"),
47			)?,
48			best_target_block_number: IntGauge::new(
49				metric_name(prefix, &format!("best_{at_target_chain_label}_block_number")),
50				format!("Best block number at the {at_target_chain_label}"),
51			)?,
52			using_different_forks: IntGauge::new(
53				metric_name(prefix, &format!("is_{at_source_chain_label}_and_{at_target_chain_label}_using_different_forks")),
54				"Whether the best finalized source block at target node is different (value 1) from the \
55				corresponding block at the source node",
56			)?,
57		})
58	}
59
60	/// Returns current value of the using-same-fork flag.
61	#[cfg(test)]
62	pub(crate) fn is_using_same_fork(&self) -> bool {
63		self.using_different_forks.get() == 0
64	}
65
66	/// Update best block number at source.
67	pub fn update_best_block_at_source<Number: UniqueSaturatedInto<u64>>(
68		&self,
69		source_best_number: Number,
70	) {
71		self.best_source_block_number.set(source_best_number.unique_saturated_into());
72	}
73
74	/// Update best block number at target.
75	pub fn update_best_block_at_target<Number: UniqueSaturatedInto<u64>>(
76		&self,
77		target_best_number: Number,
78	) {
79		self.best_target_block_number.set(target_best_number.unique_saturated_into());
80	}
81
82	/// Update using-same-fork flag.
83	pub fn update_using_same_fork(&self, using_same_fork: bool) {
84		self.using_different_forks.set((!using_same_fork).into())
85	}
86}
87
88impl Metric for SyncLoopMetrics {
89	fn register(&self, registry: &Registry) -> Result<(), PrometheusError> {
90		register(self.best_source_block_number.clone(), registry)?;
91		register(self.best_target_block_number.clone(), registry)?;
92		register(self.using_different_forks.clone(), registry)?;
93		Ok(())
94	}
95}