From 2108895fca7c34928b7d0540d8ea0c84740d56ac Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 11 Aug 2019 15:34:10 +1000 Subject: [PATCH] Unify common metric fns into a crate --- Cargo.toml | 1 + beacon_node/beacon_chain/Cargo.toml | 2 +- beacon_node/beacon_chain/src/errors.rs | 8 --- beacon_node/beacon_chain/src/lib.rs | 3 -- beacon_node/beacon_chain/src/metrics.rs | 67 ++++++++---------------- eth2/utils/lighthouse_metrics/Cargo.toml | 11 ++++ eth2/utils/lighthouse_metrics/src/lib.rs | 49 +++++++++++++++++ 7 files changed, 83 insertions(+), 58 deletions(-) create mode 100644 eth2/utils/lighthouse_metrics/Cargo.toml create mode 100644 eth2/utils/lighthouse_metrics/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index f5ee02a17..9b7b87a0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ members = [ "eth2/utils/eth2_interop_keypairs", "eth2/utils/logging", "eth2/utils/eth2_hashing", + "eth2/utils/lighthouse_metrics", "eth2/utils/merkle_proof", "eth2/utils/int_to_bytes", "eth2/utils/serde_hex", diff --git a/beacon_node/beacon_chain/Cargo.toml b/beacon_node/beacon_chain/Cargo.toml index 43e7614b6..850aa2e94 100644 --- a/beacon_node/beacon_chain/Cargo.toml +++ b/beacon_node/beacon_chain/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] store = { path = "../store" } parking_lot = "0.7" -prometheus = "^0.6" +lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" } log = "0.4" operation_pool = { path = "../../eth2/operation_pool" } serde = "1.0" diff --git a/beacon_node/beacon_chain/src/errors.rs b/beacon_node/beacon_chain/src/errors.rs index 7a51fc425..22df90397 100644 --- a/beacon_node/beacon_chain/src/errors.rs +++ b/beacon_node/beacon_chain/src/errors.rs @@ -1,5 +1,4 @@ use crate::fork_choice::Error as ForkChoiceError; -use crate::metrics::Error as MetricsError; use state_processing::per_block_processing::errors::{ AttestationValidationError, IndexedAttestationValidationError, }; @@ -34,7 +33,6 @@ pub enum BeaconChainError { MissingBeaconBlock(Hash256), MissingBeaconState(Hash256), SlotProcessingError(SlotProcessingError), - MetricsError(String), NoStateForAttestation { beacon_block_root: Hash256, }, @@ -44,12 +42,6 @@ pub enum BeaconChainError { easy_from_to!(SlotProcessingError, BeaconChainError); -impl From for BeaconChainError { - fn from(e: MetricsError) -> BeaconChainError { - BeaconChainError::MetricsError(format!("{:?}", e)) - } -} - #[derive(Debug, PartialEq)] pub enum BlockProductionError { UnableToGetBlockRootFromState, diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index e24534a2e..98bd60a35 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -1,6 +1,4 @@ #[macro_use] -extern crate prometheus; -#[macro_use] extern crate lazy_static; mod beacon_chain; @@ -18,7 +16,6 @@ pub use self::beacon_chain::{ pub use self::checkpoint::CheckPoint; pub use self::errors::{BeaconChainError, BlockProductionError}; pub use lmd_ghost; -pub use metrics::gather_metrics; pub use parking_lot; pub use slot_clock; pub use state_processing::per_block_processing::errors::{ diff --git a/beacon_node/beacon_chain/src/metrics.rs b/beacon_node/beacon_chain/src/metrics.rs index dc2919cc4..03f4783ff 100644 --- a/beacon_node/beacon_chain/src/metrics.rs +++ b/beacon_node/beacon_chain/src/metrics.rs @@ -1,67 +1,42 @@ -pub use prometheus::Error; -use prometheus::{Histogram, HistogramTimer, IntCounter, Result}; - -pub fn start_timer(histogram: &Result) -> Option { - if let Ok(histogram) = histogram { - Some(histogram.start_timer()) - } else { - None - } -} - -pub fn stop_timer(timer: Option) { - timer.map(|t| t.observe_duration()); -} - -pub fn inc_counter(counter: &Result) { - if let Ok(counter) = counter { - counter.inc(); - } -} - -pub fn observe(histogram: &Result, value: f64) { - if let Ok(histogram) = histogram { - histogram.observe(value); - } -} +pub use lighthouse_metrics::*; lazy_static! { /* * Block Processing */ - pub static ref BLOCK_PROCESSING_DB_READ: Result = register_histogram!( + pub static ref BLOCK_PROCESSING_DB_READ: Result = try_create_histogram( "block_processing_db_read_times", "Time spent loading block and state from DB" ); - pub static ref BLOCK_PROCESSING_REQUESTS: Result = register_int_counter!( + pub static ref BLOCK_PROCESSING_REQUESTS: Result = try_create_int_counter( "block_processing_requests", - "Count of blocks sumbitted for processing" + "Count of blocks submitted for processing" ); - pub static ref BLOCK_PROCESSING_SUCCESSES: Result = register_int_counter!( + pub static ref BLOCK_PROCESSING_SUCCESSES: Result = try_create_int_counter( "block_processing_successes", "Count of blocks processed without error" ); pub static ref BLOCK_PROCESSING_TIMES: Result = - register_histogram!("block_processing_times", "Full runtime of block processing"); + try_create_histogram("block_processing_times", "Full runtime of block processing"); /* * Block Production */ - pub static ref BLOCK_PRODUCTION_REQUESTS: Result = register_int_counter!( + pub static ref BLOCK_PRODUCTION_REQUESTS: Result = try_create_int_counter( "block_production_requests", "Count of all block production requests" ); - pub static ref BLOCK_PRODUCTION_SUCCESSES: Result = register_int_counter!( + pub static ref BLOCK_PRODUCTION_SUCCESSES: Result = try_create_int_counter( "block_production_successes", - "Count of blocks sucessfully produced." + "Count of blocks successfully produced." ); pub static ref BLOCK_PRODUCTION_TIMES: Result = - register_histogram!("block_production_times", "Full runtime of block production"); + try_create_histogram("block_production_times", "Full runtime of block production"); /* * Block Statistics */ - pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Result = register_histogram!( + pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Result = try_create_histogram( "operations_per_block_attestation", "Number of attestations in a block" ); @@ -69,15 +44,15 @@ lazy_static! { /* * Attestation Processing */ - pub static ref ATTESTATION_PROCESSING_REQUESTS: Result = register_int_counter!( + pub static ref ATTESTATION_PROCESSING_REQUESTS: Result = try_create_int_counter( "attestation_processing_requests", "Count of all attestations submitted for processing" ); - pub static ref ATTESTATION_PROCESSING_SUCCESSES: Result = register_int_counter!( + pub static ref ATTESTATION_PROCESSING_SUCCESSES: Result = try_create_int_counter( "attestation_processing_successes", "total_attestation_processing_successes" ); - pub static ref ATTESTATION_PROCESSING_TIMES: Result = register_histogram!( + pub static ref ATTESTATION_PROCESSING_TIMES: Result = try_create_histogram( "attestation_processing_times", "Full runtime of attestation processing" ); @@ -85,15 +60,15 @@ lazy_static! { /* * Attestation Production */ - pub static ref ATTESTATION_PRODUCTION_REQUESTS: Result = register_int_counter!( + pub static ref ATTESTATION_PRODUCTION_REQUESTS: Result = try_create_int_counter( "attestation_production_requests", "Count of all attestation production requests" ); - pub static ref ATTESTATION_PRODUCTION_SUCCESSES: Result = register_int_counter!( + pub static ref ATTESTATION_PRODUCTION_SUCCESSES: Result = try_create_int_counter( "attestation_production_successes", "Count of attestations processed without error" ); - pub static ref ATTESTATION_PRODUCTION_TIMES: Result = register_histogram!( + pub static ref ATTESTATION_PRODUCTION_TIMES: Result = try_create_histogram( "attestation_production_times", "Full runtime of attestation production" ); @@ -101,18 +76,18 @@ lazy_static! { /* * Fork Choice */ - pub static ref FORK_CHOICE_REQUESTS: Result = register_int_counter!( + pub static ref FORK_CHOICE_REQUESTS: Result = try_create_int_counter( "fork_choice_requests", "Count of occasions where fork choice has tried to find a head" ); - pub static ref FORK_CHOICE_CHANGED_HEAD: Result = register_int_counter!( + pub static ref FORK_CHOICE_CHANGED_HEAD: Result = try_create_int_counter( "fork_choice_changed_head", "Count of occasions fork choice has found a new head" ); - pub static ref FORK_CHOICE_REORG_COUNT: Result = register_int_counter!( + pub static ref FORK_CHOICE_REORG_COUNT: Result = try_create_int_counter( "fork_choice_reorg_count", "Count of occasions fork choice has switched to a different chain" ); pub static ref FORK_CHOICE_TIMES: Result = - register_histogram!("fork_choice_time", "Full runtime of fork choice"); + try_create_histogram("fork_choice_time", "Full runtime of fork choice"); } diff --git a/eth2/utils/lighthouse_metrics/Cargo.toml b/eth2/utils/lighthouse_metrics/Cargo.toml new file mode 100644 index 000000000..0a24a96fb --- /dev/null +++ b/eth2/utils/lighthouse_metrics/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "lighthouse_metrics" +version = "0.1.0" +authors = ["Paul Hauner "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lazy_static = "1.3.0" +prometheus = "^0.6" diff --git a/eth2/utils/lighthouse_metrics/src/lib.rs b/eth2/utils/lighthouse_metrics/src/lib.rs new file mode 100644 index 000000000..e6e30f6bb --- /dev/null +++ b/eth2/utils/lighthouse_metrics/src/lib.rs @@ -0,0 +1,49 @@ +use prometheus::{HistogramOpts, HistogramTimer, Opts}; + +pub use prometheus::{Histogram, IntCounter, Result}; + +pub fn try_create_int_counter(name: &str, help: &str) -> Result { + let opts = Opts::new(name, help); + let counter = IntCounter::with_opts(opts)?; + prometheus::register(Box::new(counter.clone()))?; + Ok(counter) +} + +pub fn try_create_histogram(name: &str, help: &str) -> Result { + let opts = HistogramOpts::new(name, help); + let histogram = Histogram::with_opts(opts)?; + prometheus::register(Box::new(histogram.clone()))?; + Ok(histogram) +} + +pub fn start_timer(histogram: &Result) -> Option { + if let Ok(histogram) = histogram { + Some(histogram.start_timer()) + } else { + None + } +} + +pub fn stop_timer(timer: Option) { + timer.map(|t| t.observe_duration()); +} + +pub fn inc_counter(counter: &Result) { + if let Ok(counter) = counter { + counter.inc(); + } +} + +pub fn observe(histogram: &Result, value: f64) { + if let Ok(histogram) = histogram { + histogram.observe(value); + } +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +}