use relay_utils::{
metrics::{metric_name, register, IntGauge, Metric, PrometheusError, Registry},
UniqueSaturatedInto,
};
#[derive(Clone)]
pub struct SyncLoopMetrics {
best_source_block_number: IntGauge,
best_target_block_number: IntGauge,
using_different_forks: IntGauge,
}
impl SyncLoopMetrics {
pub fn new(
prefix: Option<&str>,
at_source_chain_label: &str,
at_target_chain_label: &str,
) -> Result<Self, PrometheusError> {
Ok(SyncLoopMetrics {
best_source_block_number: IntGauge::new(
metric_name(prefix, &format!("best_{at_source_chain_label}_block_number")),
format!("Best block number at the {at_source_chain_label}"),
)?,
best_target_block_number: IntGauge::new(
metric_name(prefix, &format!("best_{at_target_chain_label}_block_number")),
format!("Best block number at the {at_target_chain_label}"),
)?,
using_different_forks: IntGauge::new(
metric_name(prefix, &format!("is_{at_source_chain_label}_and_{at_target_chain_label}_using_different_forks")),
"Whether the best finalized source block at target node is different (value 1) from the \
corresponding block at the source node",
)?,
})
}
#[cfg(test)]
pub(crate) fn is_using_same_fork(&self) -> bool {
self.using_different_forks.get() == 0
}
pub fn update_best_block_at_source<Number: UniqueSaturatedInto<u64>>(
&self,
source_best_number: Number,
) {
self.best_source_block_number.set(source_best_number.unique_saturated_into());
}
pub fn update_best_block_at_target<Number: UniqueSaturatedInto<u64>>(
&self,
target_best_number: Number,
) {
self.best_target_block_number.set(target_best_number.unique_saturated_into());
}
pub fn update_using_same_fork(&self, using_same_fork: bool) {
self.using_different_forks.set((!using_same_fork).into())
}
}
impl Metric for SyncLoopMetrics {
fn register(&self, registry: &Registry) -> Result<(), PrometheusError> {
register(self.best_source_block_number.clone(), registry)?;
register(self.best_target_block_number.clone(), registry)?;
register(self.using_different_forks.clone(), registry)?;
Ok(())
}
}