From e756a0aaa400ef9d96fcd0b4649088b7c479526b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 28 May 2019 16:11:16 +1000 Subject: [PATCH] Add extra metrics for prom --- beacon_node/http_server/src/metrics.rs | 31 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/beacon_node/http_server/src/metrics.rs b/beacon_node/http_server/src/metrics.rs index eb7816d0e..366d642b5 100644 --- a/beacon_node/http_server/src/metrics.rs +++ b/beacon_node/http_server/src/metrics.rs @@ -29,16 +29,22 @@ fn handle_metrics(req: &mut Request) -> IronResul let r = Registry::new(); - let present_slot = if let Ok(Some(slot)) = beacon_chain.slot_clock.present_slot() { - slot - } else { - Slot::new(0) - }; - register_and_set_slot( + let present_slot = beacon_chain + .slot_clock + .present_slot() + .unwrap_or_else(|_| None) + .unwrap_or_else(|| Slot::new(0)); + register_and_set_slot(&r, "present_slot", "slock_clock_reading", present_slot); + + let best_slot = beacon_chain.head().beacon_block.slot; + register_and_set_slot(&r, "best_slot", "slot_of_block_at_head_of_chain", best_slot); + + let validator_count = beacon_chain.head().beacon_state.validator_registry.len(); + register_and_set( &r, - "present_slot", - "direct_slock_clock_reading", - present_slot, + "validator_count", + "total_number_of_validators", + validator_count as i64, ); // Gather the metrics. @@ -52,6 +58,13 @@ fn handle_metrics(req: &mut Request) -> IronResul Ok(Response::with((Status::Ok, prom_string))) } +fn register_and_set(registry: &Registry, name: &str, help: &str, value: i64) { + let counter_opts = Opts::new(name, help); + let counter = IntCounter::with_opts(counter_opts).unwrap(); + registry.register(Box::new(counter.clone())).unwrap(); + counter.inc_by(value); +} + fn register_and_set_slot(registry: &Registry, name: &str, help: &str, slot: Slot) { let counter_opts = Opts::new(name, help); let counter = IntCounter::with_opts(counter_opts).unwrap();