Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add additional metrics (#152)
Browse files Browse the repository at this point in the history
* Add additional metrics

* add skipped session metric

* add some comment for temp metric
  • Loading branch information
adoerr authored Apr 19, 2021
1 parent 40e81a6 commit bd3a85f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
56 changes: 53 additions & 3 deletions client/beefy/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ use prometheus::{register, Counter, Gauge, PrometheusError, Registry, U64};
pub(crate) struct Metrics {
/// Current active validator set id
pub beefy_validator_set_id: Gauge<U64>,
pub beefy_gadget_votes: Counter<U64>,
/// Total number of votes sent by this node
pub beefy_votes_sent: Counter<U64>,
/// Most recent concluded voting round
pub beefy_round_concluded: Gauge<U64>,
/// Best block finalized by BEEFY
pub beefy_best_block: Gauge<U64>,
/// Next block BEEFY should vote on
pub beefy_should_vote_on: Gauge<U64>,
/// Number of sessions without a signed commitment
pub beefy_skipped_sessions: Counter<U64>,
}

impl Metrics {
Expand All @@ -32,10 +41,51 @@ impl Metrics {
Gauge::new("beefy_validator_set_id", "Current BEEFY active validator set id.")?,
registry,
)?,
beefy_gadget_votes: register(
Counter::new("beefy_gadget_votes_total", "Total number of vote messages gossiped.")?,
beefy_votes_sent: register(
Counter::new("beefy_votes_sent", "Number of votes sent by this node")?,
registry,
)?,
beefy_round_concluded: register(
Gauge::new("beefy_round_concluded", "Voting round, that has been concluded")?,
registry,
)?,
beefy_best_block: register(
Gauge::new("beefy_best_block", "Best block finalized by BEEFY")?,
registry,
)?,
beefy_should_vote_on: register(
Gauge::new("beefy_should_vote_on", "Next block, BEEFY should vote on")?,
registry,
)?,
beefy_skipped_sessions: register(
Counter::new(
"beefy_skipped_sessions",
"Number of sessions without a signed commitment",
)?,
registry,
)?,
})
}
}

// Note: we use the `format` macro to convert an expr into a `u64`. This will fail,
// if expr does not derive `Display`.
#[macro_export]
macro_rules! metric_set {
($self:ident, $m:ident, $v:expr) => {{
let val: u64 = format!("{}", $v).parse().unwrap();

if let Some(metrics) = $self.metrics.as_ref() {
metrics.$m.set(val);
}
}};
}

#[macro_export]
macro_rules! metric_inc {
($self:ident, $m:ident) => {{
if let Some(metrics) = $self.metrics.as_ref() {
metrics.$m.inc();
}
}};
}
32 changes: 26 additions & 6 deletions client/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use beefy_primitives::{

use crate::{
error::{self},
metric_inc, metric_set,
metrics::Metrics,
notification, round,
validator::{topic, BeefyGossipValidator},
Expand All @@ -67,6 +68,7 @@ where
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
gossip_engine: Arc<Mutex<GossipEngine<B>>>,
gossip_validator: Arc<BeefyGossipValidator<B, P>>,
/// Min delta in block numbers between two blocks, BEEFY should vote on
min_block_delta: u32,
metrics: Option<Metrics>,
rounds: round::Rounds<MmrRootHash, NumberFor<B>, P::Public, P::Signature>,
Expand All @@ -77,6 +79,9 @@ where
best_beefy_block: Option<NumberFor<B>>,
/// Best block this node has voted for
best_block_voted_on: NumberFor<B>,
/// Validator set id for the last signed commitment
last_signed_id: u64,
// keep rustc happy
_backend: PhantomData<BE>,
_pair: PhantomData<P>,
}
Expand Down Expand Up @@ -119,6 +124,7 @@ where
best_grandpa_block: client.info().finalized_number,
best_beefy_block: None,
best_block_voted_on: Zero::zero(),
last_signed_id: 0,
_backend: PhantomData,
_pair: PhantomData,
}
Expand Down Expand Up @@ -160,6 +166,8 @@ where
next_block_to_vote_on,
);

metric_set!(self, beefy_should_vote_on, next_block_to_vote_on);

number == next_block_to_vote_on
}

Expand Down Expand Up @@ -212,8 +220,11 @@ where
if let Some(active) = self.validator_set(&notification.header) {
debug!(target: "beefy", "🥩 Active validator set id: {:?}", active);

if let Some(metrics) = self.metrics.as_ref() {
metrics.beefy_validator_set_id.set(active.id);
metric_set!(self, beefy_validator_set_id, active.id);

// BEEFY should produce a signed commitment for each session
if (active.id != GENESIS_AUTHORITY_SET_ID) && (active.id != (1 + self.last_signed_id)) {
metric_inc!(self, beefy_skipped_sessions);
}

// Authority set change or genesis set id triggers new voting rounds
Expand All @@ -227,6 +238,10 @@ where
debug!(target: "beefy", "🥩 New Rounds for id: {:?}", active.id);

self.best_beefy_block = Some(*notification.header.number());

// this metric is kind of 'fake'. Best BEEFY block should only be updated once we have a
// signed commitment for the block. Remove once the above TODO is done.
metric_set!(self, beefy_best_block, *notification.header.number());
}
};

Expand Down Expand Up @@ -269,9 +284,7 @@ where

let encoded_message = message.encode();

if let Some(metrics) = self.metrics.as_ref() {
metrics.beefy_gadget_votes.inc();
}
metric_inc!(self, beefy_votes_sent);

debug!(target: "beefy", "🥩 Sent vote message: {:?}", message);

Expand All @@ -293,18 +306,25 @@ where

if vote_added && self.rounds.is_done(&round) {
if let Some(signatures) = self.rounds.drop(&round) {
// id is stored for skipped session metric calculation
self.last_signed_id = self.rounds.validator_set_id();

let commitment = Commitment {
payload: round.0,
block_number: round.1,
validator_set_id: self.rounds.validator_set_id(),
validator_set_id: self.last_signed_id,
};

let signed_commitment = SignedCommitment { commitment, signatures };

metric_set!(self, beefy_round_concluded, round.1);

info!(target: "beefy", "🥩 Round #{} concluded, committed: {:?}.", round.1, signed_commitment);

self.signed_commitment_sender.notify(signed_commitment);
self.best_beefy_block = Some(round.1);

metric_set!(self, beefy_best_block, round.1);
}
}
}
Expand Down

0 comments on commit bd3a85f

Please sign in to comment.