Skip to content

Commit ea9eb5d

Browse files
expensesGeneral-Beck
authored andcommitted
Add more metrics to prometheus (paritytech#5034)
* Add a few things * Add finality_grandpa_round * fix fg tests * Nitpicks * Nitpicks * Fix name of prometheus crate
1 parent c5cc966 commit ea9eb5d

File tree

16 files changed

+135
-24
lines changed

16 files changed

+135
-24
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node-template/node/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub fn new_full(config: Configuration<GenesisConfig>)
157157
on_exit: service.on_exit(),
158158
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
159159
voting_rule: grandpa::VotingRulesBuilder::default().build(),
160+
prometheus_registry: service.prometheus_registry()
160161
};
161162

162163
// the GRANDPA voter task is considered infallible, i.e.

bin/node/cli/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ macro_rules! new_full {
228228
on_exit: service.on_exit(),
229229
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
230230
voting_rule: grandpa::VotingRulesBuilder::default().build(),
231+
prometheus_registry: service.prometheus_registry(),
231232
};
232233

233234
// the GRANDPA voter task is considered infallible, i.e.

client/finality-grandpa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ sc-network = { version = "0.8.0-alpha.2", path = "../network" }
3535
sc-network-gossip = { version = "0.8.0-alpha.2", path = "../network-gossip" }
3636
sp-finality-tracker = { version = "2.0.0-alpha.2", path = "../../primitives/finality-tracker" }
3737
sp-finality-grandpa = { version = "2.0.0-alpha.2", path = "../../primitives/finality-grandpa" }
38+
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0-alpha.2" }
3839
sc-block-builder = { version = "0.8.0-alpha.2", path = "../block-builder" }
3940
finality-grandpa = { version = "0.11.1", features = ["derive-codec"] }
4041
pin-project = "0.4.6"

client/finality-grandpa/src/environment.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use crate::justification::GrandpaJustification;
5858
use crate::until_imported::UntilVoteTargetImported;
5959
use crate::voting_rule::VotingRule;
6060
use sp_finality_grandpa::{AuthorityId, AuthoritySignature, SetId, RoundNumber};
61+
use prometheus_endpoint::{Gauge, U64, register, PrometheusError};
6162

6263
type HistoricalVotes<Block> = finality_grandpa::HistoricalVotes<
6364
<Block as BlockT>::Hash,
@@ -372,6 +373,24 @@ impl<Block: BlockT> SharedVoterSetState<Block> {
372373
}
373374
}
374375

376+
/// Prometheus metrics for GRANDPA.
377+
#[derive(Clone)]
378+
pub(crate) struct Metrics {
379+
finality_grandpa_round: Gauge<U64>,
380+
}
381+
382+
impl Metrics {
383+
pub(crate) fn register(registry: &prometheus_endpoint::Registry) -> Result<Self, PrometheusError> {
384+
Ok(Self {
385+
finality_grandpa_round: register(
386+
Gauge::new("finality_grandpa_round", "Highest completed GRANDPA round.")?,
387+
registry
388+
)?,
389+
})
390+
}
391+
}
392+
393+
375394
/// The environment we run GRANDPA in.
376395
pub(crate) struct Environment<Backend, Block: BlockT, C, N: NetworkT<Block>, SC, VR> {
377396
pub(crate) client: Arc<C>,
@@ -384,6 +403,7 @@ pub(crate) struct Environment<Backend, Block: BlockT, C, N: NetworkT<Block>, SC,
384403
pub(crate) set_id: SetId,
385404
pub(crate) voter_set_state: SharedVoterSetState<Block>,
386405
pub(crate) voting_rule: VR,
406+
pub(crate) metrics: Option<Metrics>,
387407
pub(crate) _phantom: PhantomData<Backend>,
388408
}
389409

@@ -397,6 +417,17 @@ impl<Backend, Block: BlockT, C, N: NetworkT<Block>, SC, VR> Environment<Backend,
397417
self.voter_set_state.with(|voter_set_state| {
398418
if let Some(set_state) = f(&voter_set_state)? {
399419
*voter_set_state = set_state;
420+
421+
if let Some(metrics) = self.metrics.as_ref() {
422+
if let VoterSetState::Live { completed_rounds, .. } = voter_set_state {
423+
let highest = completed_rounds.rounds.iter()
424+
.map(|round| round.number)
425+
.max()
426+
.expect("There is always one completed round (genesis); qed");
427+
428+
metrics.finality_grandpa_round.set(highest);
429+
}
430+
}
400431
}
401432
Ok(())
402433
})

client/finality-grandpa/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub use voting_rule::{
104104
};
105105

106106
use aux_schema::PersistentData;
107-
use environment::{Environment, VoterSetState};
107+
use environment::{Environment, VoterSetState, Metrics};
108108
use import::GrandpaBlockImport;
109109
use until_imported::UntilGlobalMessageBlocksImported;
110110
use communication::{NetworkBridge, Network as NetworkT};
@@ -551,6 +551,8 @@ pub struct GrandpaParams<Block: BlockT, C, N, SC, VR, X> {
551551
pub telemetry_on_connect: Option<futures::channel::mpsc::UnboundedReceiver<()>>,
552552
/// A voting rule used to potentially restrict target votes.
553553
pub voting_rule: VR,
554+
/// The prometheus metrics registry.
555+
pub prometheus_registry: Option<prometheus_endpoint::Registry>,
554556
}
555557

556558
/// Run a GRANDPA voter as a task. Provide configuration and a link to a
@@ -576,6 +578,7 @@ pub fn run_grandpa_voter<Block: BlockT, BE: 'static, C, N, SC, VR, X>(
576578
on_exit,
577579
telemetry_on_connect,
578580
voting_rule,
581+
prometheus_registry,
579582
} = grandpa_params;
580583

581584
// NOTE: we have recently removed `run_grandpa_observer` from the public
@@ -634,6 +637,7 @@ pub fn run_grandpa_voter<Block: BlockT, BE: 'static, C, N, SC, VR, X>(
634637
voting_rule,
635638
persistent_data,
636639
voter_commands_rx,
640+
prometheus_registry,
637641
);
638642

639643
let voter_work = voter_work
@@ -673,6 +677,7 @@ where
673677
voting_rule: VR,
674678
persistent_data: PersistentData<Block>,
675679
voter_commands_rx: mpsc::UnboundedReceiver<VoterCommand<Block::Hash, NumberFor<Block>>>,
680+
prometheus_registry: Option<prometheus_endpoint::Registry>,
676681
) -> Self {
677682

678683
let voters = persistent_data.authority_set.current_authorities();
@@ -687,6 +692,10 @@ where
687692
authority_set: persistent_data.authority_set.clone(),
688693
consensus_changes: persistent_data.consensus_changes.clone(),
689694
voter_set_state: persistent_data.set_state.clone(),
695+
metrics: prometheus_registry.map(|registry| {
696+
Metrics::register(&registry)
697+
.expect("Other metrics would have failed to register before these; qed")
698+
}),
690699
_phantom: PhantomData,
691700
});
692701

@@ -807,6 +816,7 @@ where
807816
consensus_changes: self.env.consensus_changes.clone(),
808817
network: self.env.network.clone(),
809818
voting_rule: self.env.voting_rule.clone(),
819+
metrics: self.env.metrics.clone(),
810820
_phantom: PhantomData,
811821
});
812822

client/finality-grandpa/src/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ fn run_to_completion_with<F>(
447447
on_exit: Exit,
448448
telemetry_on_connect: None,
449449
voting_rule: (),
450+
prometheus_registry: None,
450451
};
451452
let voter = run_grandpa_voter(grandpa_params).expect("all in order with client and network");
452453

@@ -578,6 +579,7 @@ fn finalize_3_voters_1_full_observer() {
578579
on_exit: Exit,
579580
telemetry_on_connect: None,
580581
voting_rule: (),
582+
prometheus_registry: None,
581583
};
582584

583585
voters.push(run_grandpa_voter(grandpa_params).expect("all in order with client and network"));
@@ -741,6 +743,7 @@ fn transition_3_voters_twice_1_full_observer() {
741743
on_exit: Exit,
742744
telemetry_on_connect: None,
743745
voting_rule: (),
746+
prometheus_registry: None,
744747
};
745748
let voter = run_grandpa_voter(grandpa_params).expect("all in order with client and network");
746749

@@ -1166,6 +1169,7 @@ fn voter_persists_its_votes() {
11661169
on_exit: Exit,
11671170
telemetry_on_connect: None,
11681171
voting_rule: VotingRulesBuilder::default().build(),
1172+
prometheus_registry: None,
11691173
};
11701174

11711175
let voter = run_grandpa_voter(grandpa_params)
@@ -1511,6 +1515,7 @@ fn voter_catches_up_to_latest_round_when_behind() {
15111515
on_exit: Exit,
15121516
telemetry_on_connect: None,
15131517
voting_rule: (),
1518+
prometheus_registry: None,
15141519
};
15151520

15161521
Box::pin(run_grandpa_voter(grandpa_params).expect("all in order with client and network"))
@@ -1642,6 +1647,7 @@ fn grandpa_environment_respects_voting_rules() {
16421647
voters: Arc::new(authority_set.current_authorities()),
16431648
network,
16441649
voting_rule,
1650+
metrics: None,
16451651
_phantom: PhantomData,
16461652
}
16471653
};

client/network/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ sp-consensus = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/c
5252
sp-consensus-babe = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/babe" }
5353
sp-core = { version = "2.0.0-alpha.2", path = "../../primitives/core" }
5454
sp-runtime = { version = "2.0.0-alpha.2", path = "../../primitives/runtime" }
55+
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../../utils/prometheus" }
5556
thiserror = "1"
5657
unsigned-varint = { version = "0.3.1", features = ["futures", "futures-codec"] }
5758
void = "1.0.2"

client/network/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use core::{fmt, iter};
4141
use std::{future::Future, pin::Pin};
4242
use std::{error::Error, fs, io::{self, Write}, net::Ipv4Addr, path::{Path, PathBuf}, sync::Arc};
4343
use zeroize::Zeroize;
44+
use prometheus_endpoint::Registry;
4445

4546
/// Network initialization parameters.
4647
pub struct Params<B: BlockT, H: ExHashT> {
@@ -90,6 +91,9 @@ pub struct Params<B: BlockT, H: ExHashT> {
9091

9192
/// Type to check incoming block announcements.
9293
pub block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
94+
95+
/// Registry for recording prometheus metrics to.
96+
pub metrics_registry: Option<Registry>,
9397
}
9498

9599
bitflags! {

client/network/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum Error {
4545
/// The second peer id that was found for the bootnode.
4646
second_id: PeerId,
4747
},
48+
/// Prometheus metrics error.
49+
Prometheus(prometheus_endpoint::PrometheusError)
4850
}
4951

5052
// Make `Debug` use the `Display` implementation.
@@ -60,6 +62,7 @@ impl std::error::Error for Error {
6062
Error::Io(ref err) => Some(err),
6163
Error::Client(ref err) => Some(err),
6264
Error::DuplicateBootnode { .. } => None,
65+
Error::Prometheus(ref err) => Some(err),
6366
}
6467
}
6568
}

0 commit comments

Comments
 (0)