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

grandpa: report metrics on prevotes and precommits cast #6970

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 62 additions & 8 deletions client/finality-grandpa/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use sp_runtime::generic::BlockId;
use sp_runtime::traits::{
Block as BlockT, Header as HeaderT, NumberFor, One, Zero,
};
use sc_telemetry::{telemetry, CONSENSUS_INFO};
use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO};

use crate::{
CommandOrError, Commit, Config, Error, Precommit, Prevote,
Expand All @@ -59,7 +59,7 @@ use sp_finality_grandpa::{
AuthorityId, AuthoritySignature, Equivocation, EquivocationProof,
GrandpaApi, RoundNumber, SetId,
};
use prometheus_endpoint::{Gauge, U64, register, PrometheusError};
use prometheus_endpoint::{register, Counter, Gauge, PrometheusError, U64};

type HistoricalVotes<Block> = finality_grandpa::HistoricalVotes<
<Block as BlockT>::Hash,
Expand Down Expand Up @@ -378,14 +378,32 @@ impl<Block: BlockT> SharedVoterSetState<Block> {
#[derive(Clone)]
pub(crate) struct Metrics {
finality_grandpa_round: Gauge<U64>,
finality_grandpa_prevotes: Counter<U64>,
finality_grandpa_precommits: Counter<U64>,
}

impl Metrics {
pub(crate) fn register(registry: &prometheus_endpoint::Registry) -> Result<Self, PrometheusError> {
pub(crate) fn register(
registry: &prometheus_endpoint::Registry,
) -> Result<Self, PrometheusError> {
Ok(Self {
finality_grandpa_round: register(
Gauge::new("finality_grandpa_round", "Highest completed GRANDPA round.")?,
registry
registry,
)?,
finality_grandpa_prevotes: register(
Counter::new(
"finality_grandpa_prevotes_total",
"Total number of GRANDPA prevotes cast locally.",
)?,
registry,
)?,
finality_grandpa_precommits: register(
Counter::new(
"finality_grandpa_precommits_total",
"Total number of GRANDPA precommits cast locally.",
)?,
registry,
)?,
})
}
Expand Down Expand Up @@ -804,9 +822,22 @@ where
None => return Ok(()),
};

let report_prevote_metrics = |prevote: &Prevote<Block>| {
telemetry!(CONSENSUS_DEBUG; "afg.prevote_issued";
"round" => round,
"target_number" => ?prevote.target_number,
"target_hash" => ?prevote.target_hash,
);

if let Some(metrics) = self.metrics.as_ref() {
metrics.finality_grandpa_prevotes.inc();
}
};

self.update_voter_set_state(|voter_set_state| {
let (completed_rounds, current_rounds) = voter_set_state.with_current_round(round)?;
let current_round = current_rounds.get(&round)
let current_round = current_rounds
.get(&round)
.expect("checked in with_current_round that key exists; qed.");

if !current_round.can_prevote() {
Expand All @@ -816,6 +847,9 @@ where
return Ok(None);
}

// report to telemetry and prometheus
report_prevote_metrics(&prevote);

let propose = current_round.propose();

let mut current_rounds = current_rounds.clone();
Expand All @@ -837,17 +871,34 @@ where
Ok(())
}

fn precommitted(&self, round: RoundNumber, precommit: Precommit<Block>) -> Result<(), Self::Error> {
fn precommitted(
&self,
round: RoundNumber,
precommit: Precommit<Block>,
) -> Result<(), Self::Error> {
let local_id = crate::is_voter(&self.voters, self.config.keystore.as_ref());

let local_id = match local_id {
Some(id) => id,
None => return Ok(()),
};

let report_precommit_metrics = |precommit: &Precommit<Block>| {
telemetry!(CONSENSUS_DEBUG; "afg.precommit_issued";
"round" => round,
"target_number" => ?precommit.target_number,
"target_hash" => ?precommit.target_hash,
);

if let Some(metrics) = self.metrics.as_ref() {
metrics.finality_grandpa_precommits.inc();
}
};

self.update_voter_set_state(|voter_set_state| {
let (completed_rounds, current_rounds) = voter_set_state.with_current_round(round)?;
let current_round = current_rounds.get(&round)
let current_round = current_rounds
.get(&round)
.expect("checked in with_current_round that key exists; qed.");

if !current_round.can_precommit() {
Expand All @@ -857,13 +908,16 @@ where
return Ok(None);
}

// report to telemetry and prometheus
report_precommit_metrics(&precommit);

let propose = current_round.propose();
let prevote = match current_round {
HasVoted::Yes(_, Vote::Prevote(_, prevote)) => prevote,
_ => {
let msg = "Voter precommitting before prevoting.";
return Err(Error::Safety(msg.to_string()));
},
}
};

let mut current_rounds = current_rounds.clone();
Expand Down