1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
34// 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.
89// 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.
1314// 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/>.
1617//! Metrics for headers synchronization relay loop.
1819use relay_utils::{
20 metrics::{metric_name, register, IntGauge, Metric, PrometheusError, Registry},
21 UniqueSaturatedInto,
22};
2324/// Headers sync metrics.
25#[derive(Clone)]
26pub struct SyncLoopMetrics {
27/// Best syncing header at the source.
28best_source_block_number: IntGauge,
29/// Best syncing header at the target.
30best_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.
33using_different_forks: IntGauge,
34}
3536impl SyncLoopMetrics {
37/// Create and register headers loop metrics.
38pub fn new(
39 prefix: Option<&str>,
40 at_source_chain_label: &str,
41 at_target_chain_label: &str,
42 ) -> Result<Self, PrometheusError> {
43Ok(SyncLoopMetrics {
44 best_source_block_number: IntGauge::new(
45 metric_name(prefix, &format!("best_{at_source_chain_label}_block_number")),
46format!("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")),
50format!("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 }
5960/// Returns current value of the using-same-fork flag.
61#[cfg(test)]
62pub(crate) fn is_using_same_fork(&self) -> bool {
63self.using_different_forks.get() == 0
64}
6566/// Update best block number at source.
67pub fn update_best_block_at_source<Number: UniqueSaturatedInto<u64>>(
68&self,
69 source_best_number: Number,
70 ) {
71self.best_source_block_number.set(source_best_number.unique_saturated_into());
72 }
7374/// Update best block number at target.
75pub fn update_best_block_at_target<Number: UniqueSaturatedInto<u64>>(
76&self,
77 target_best_number: Number,
78 ) {
79self.best_target_block_number.set(target_best_number.unique_saturated_into());
80 }
8182/// Update using-same-fork flag.
83pub fn update_using_same_fork(&self, using_same_fork: bool) {
84self.using_different_forks.set((!using_same_fork).into())
85 }
86}
8788impl Metric for SyncLoopMetrics {
89fn 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)?;
93Ok(())
94 }
95}