use bp_polkadot_core::parachains::ParaId;
use relay_utils::{
metrics::{metric_name, register, Gauge, Metric, PrometheusError, Registry, U64},
UniqueSaturatedInto,
};
#[derive(Clone)]
pub struct ParachainsLoopMetrics {
best_source_block_numbers: Gauge<U64>,
best_target_block_numbers: Gauge<U64>,
}
impl ParachainsLoopMetrics {
pub fn new(prefix: Option<&str>) -> Result<Self, PrometheusError> {
Ok(ParachainsLoopMetrics {
best_source_block_numbers: Gauge::new(
metric_name(prefix, "best_parachain_block_number_at_source"),
"Best parachain block numbers at the source relay chain".to_string(),
)?,
best_target_block_numbers: Gauge::new(
metric_name(prefix, "best_parachain_block_number_at_target"),
"Best parachain block numbers at the target chain".to_string(),
)?,
})
}
pub fn update_best_parachain_block_at_source<Number: UniqueSaturatedInto<u64>>(
&self,
parachain: ParaId,
block_number: Number,
) {
let block_number = block_number.unique_saturated_into();
log::trace!(
target: "bridge-metrics",
"Updated value of metric 'best_parachain_block_number_at_source[{:?}]': {:?}",
parachain,
block_number,
);
self.best_source_block_numbers.set(block_number);
}
pub fn update_best_parachain_block_at_target<Number: UniqueSaturatedInto<u64>>(
&self,
parachain: ParaId,
block_number: Number,
) {
let block_number = block_number.unique_saturated_into();
log::trace!(
target: "bridge-metrics",
"Updated value of metric 'best_parachain_block_number_at_target[{:?}]': {:?}",
parachain,
block_number,
);
self.best_target_block_numbers.set(block_number);
}
}
impl Metric for ParachainsLoopMetrics {
fn register(&self, registry: &Registry) -> Result<(), PrometheusError> {
register(self.best_source_block_numbers.clone(), registry)?;
register(self.best_target_block_numbers.clone(), registry)?;
Ok(())
}
}