Skip to content

Commit

Permalink
made attestation metrics scrapeable (#184)
Browse files Browse the repository at this point in the history
It is easier to manage gauge metrics if they are scraped on demand,
rather than actively updated.
  • Loading branch information
pompon0 authored Aug 21, 2024
1 parent 5de3ba3 commit fa035ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
29 changes: 27 additions & 2 deletions node/actors/network/src/gossip/attestation/metrics.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -15,5 +19,26 @@ pub(crate) struct Metrics {
pub(crate) weight_collected: vise::Gauge<f64>,
}

#[vise::register]
pub(super) static METRICS: vise::Global<Metrics> = vise::Global::new();
impl Metrics {
/// Registers metrics to a global collector.
pub(crate) fn register(ctrl: Weak<Controller>) {
#[vise::register]
static COLLECTOR: vise::Collector<Option<Metrics>> = 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:#}");
}
}
}
21 changes: 5 additions & 16 deletions node/actors/network/src/gossip/attestation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ impl Controller {
}
}

/// Registers metrics for this controller.
pub(crate) fn register_metrics(self: &Arc<Self>) {
metrics::Metrics::register(Arc::downgrade(self));
}

/// Subscribes to state diffs.
pub(crate) fn subscribe(&self) -> DiffReceiver {
let mut recv = self.state.subscribe();
Expand All @@ -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
Expand Down Expand Up @@ -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(())
}
Expand Down
1 change: 1 addition & 0 deletions node/actors/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Network {
/// Registers metrics for this state.
pub fn register_metrics(self: &Arc<Self>) {
metrics::NetworkGauges::register(Arc::downgrade(self));
self.gossip.attestation.register_metrics();
}

/// Handles a dispatcher message.
Expand Down

0 comments on commit fa035ec

Please sign in to comment.