diff --git a/node/actors/network/src/gossip/attestation/metrics.rs b/node/actors/network/src/gossip/attestation/metrics.rs index 4e5137da..6ddffc0f 100644 --- a/node/actors/network/src/gossip/attestation/metrics.rs +++ b/node/actors/network/src/gossip/attestation/metrics.rs @@ -1,3 +1,7 @@ +//! Attestation metrics. +use super::Controller; +use std::sync::Weak; + /// Metrics related to the gossiping of L1 batch votes. #[derive(Debug, vise::Metrics)] #[metrics(prefix = "network_gossip_attestation")] @@ -15,5 +19,26 @@ pub(crate) struct Metrics { pub(crate) weight_collected: vise::Gauge, } -#[vise::register] -pub(super) static METRICS: vise::Global = vise::Global::new(); +impl Metrics { + /// Registers metrics to a global collector. + pub(crate) fn register(ctrl: Weak) { + #[vise::register] + static COLLECTOR: vise::Collector> = vise::Collector::new(); + let res = COLLECTOR.before_scrape(move || { + ctrl.upgrade().and_then(|ctrl| { + let ctrl = (*ctrl.state.subscribe().borrow()).clone()?; + let m = Metrics::default(); + m.batch_number.set(ctrl.info.batch_to_attest.number.0); + m.committee_size.set(ctrl.info.committee.len()); + m.votes_collected.set(ctrl.votes.len()); + #[allow(clippy::float_arithmetic)] + m.weight_collected + .set(ctrl.total_weight as f64 / ctrl.info.committee.total_weight() as f64); + Some(m) + }) + }); + if let Err(err) = res { + tracing::warn!("Failed registering attestation metrics: {err:#}"); + } + } +} diff --git a/node/actors/network/src/gossip/attestation/mod.rs b/node/actors/network/src/gossip/attestation/mod.rs index 6effd612..ede861af 100644 --- a/node/actors/network/src/gossip/attestation/mod.rs +++ b/node/actors/network/src/gossip/attestation/mod.rs @@ -242,6 +242,11 @@ impl Controller { } } + /// Registers metrics for this controller. + pub(crate) fn register_metrics(self: &Arc) { + metrics::Metrics::register(Arc::downgrade(self)); + } + /// Subscribes to state diffs. pub(crate) fn subscribe(&self) -> DiffReceiver { let mut recv = self.state.subscribe(); @@ -265,11 +270,6 @@ impl Controller { let before = state.total_weight; let res = state.insert_votes(votes); if state.total_weight > before { - metrics::METRICS.votes_collected.set(state.votes.len()); - #[allow(clippy::float_arithmetic)] - metrics::METRICS - .weight_collected - .set(state.total_weight as f64 / state.info.committee.total_weight() as f64); locked.send_replace(Some(state)); } res @@ -351,17 +351,6 @@ impl Controller { new.insert_vote(Arc::new(vote)).unwrap(); } } - metrics::METRICS - .batch_number - .set(new.info.batch_to_attest.number.0); - metrics::METRICS - .committee_size - .set(new.info.committee.len()); - metrics::METRICS.votes_collected.set(new.votes.len()); - #[allow(clippy::float_arithmetic)] - metrics::METRICS - .weight_collected - .set(new.total_weight as f64 / new.info.committee.total_weight() as f64); locked.send_replace(Some(new)); Ok(()) } diff --git a/node/actors/network/src/lib.rs b/node/actors/network/src/lib.rs index 1e7874d2..7f754809 100644 --- a/node/actors/network/src/lib.rs +++ b/node/actors/network/src/lib.rs @@ -74,6 +74,7 @@ impl Network { /// Registers metrics for this state. pub fn register_metrics(self: &Arc) { metrics::NetworkGauges::register(Arc::downgrade(self)); + self.gossip.attestation.register_metrics(); } /// Handles a dispatcher message.