diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index df9523624a..b0bb6a1592 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -468,8 +468,8 @@ impl BeaconChain { state: &BeaconState, ) -> Result { // Collect some metrics. - metrics::ATTESTATION_PRODUCTION_REQUESTS.inc(); - let timer = metrics::ATTESTATION_PRODUCTION_TIMES.start_timer(); + metrics::inc_counter(&metrics::ATTESTATION_PRODUCTION_REQUESTS); + let timer = metrics::start_timer(&metrics::ATTESTATION_PRODUCTION_TIMES); let slots_per_epoch = T::EthSpec::slots_per_epoch(); let current_epoch_start_slot = state.current_epoch().start_slot(slots_per_epoch); @@ -516,8 +516,8 @@ impl BeaconChain { }; // Collect some metrics. - metrics::ATTESTATION_PRODUCTION_SUCCESSES.inc(); - timer.observe_duration(); + metrics::inc_counter(&metrics::ATTESTATION_PRODUCTION_SUCCESSES); + metrics::stop_timer(timer); Ok(AttestationData { beacon_block_root: head_block_root, @@ -703,8 +703,8 @@ impl BeaconChain { state: &BeaconState, block: &BeaconBlock, ) -> Result { - metrics::ATTESTATION_PROCESSING_REQUESTS.inc(); - let timer = metrics::ATTESTATION_PROCESSING_TIMES.start_timer(); + metrics::inc_counter(&metrics::ATTESTATION_PROCESSING_REQUESTS); + let timer = metrics::start_timer(&metrics::ATTESTATION_PROCESSING_TIMES); // Find the highest between: // @@ -749,12 +749,12 @@ impl BeaconChain { .insert_attestation(attestation, state, &self.spec)?; // Update the metrics. - metrics::ATTESTATION_PROCESSING_SUCCESSES.inc(); + metrics::inc_counter(&metrics::ATTESTATION_PROCESSING_SUCCESSES); Ok(AttestationProcessingOutcome::Processed) }; - timer.observe_duration(); + timer.map(|t| t.observe_duration()); result } @@ -805,8 +805,8 @@ impl BeaconChain { &self, block: BeaconBlock, ) -> Result { - metrics::BLOCK_PROCESSING_REQUESTS.inc(); - let timer = metrics::BLOCK_PROCESSING_TIMES.start_timer(); + metrics::inc_counter(&metrics::BLOCK_PROCESSING_REQUESTS); + let timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_TIMES); let finalized_slot = self .state @@ -846,7 +846,7 @@ impl BeaconChain { // Records the time taken to load the block and state from the database during block // processing. - let db_read_timer = metrics::BLOCK_PROCESSING_DB_READ.start_timer(); + let db_read_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_DB_READ); // Load the blocks parent block from the database, returning invalid if that block is not // found. @@ -867,7 +867,7 @@ impl BeaconChain { .get(&parent_state_root)? .ok_or_else(|| Error::DBInconsistent(format!("Missing state {}", parent_state_root)))?; - db_read_timer.observe_duration(); + metrics::stop_timer(db_read_timer); // Transition the parent state to the block slot. let mut state: BeaconState = parent_state; @@ -921,9 +921,12 @@ impl BeaconChain { ) }; - metrics::BLOCK_PROCESSING_SUCCESSES.inc(); - metrics::OPERATIONS_PER_BLOCK_ATTESTATION.observe(block.body.attestations.len() as f64); - timer.observe_duration(); + metrics::inc_counter(&metrics::BLOCK_PROCESSING_SUCCESSES); + metrics::observe( + &metrics::OPERATIONS_PER_BLOCK_ATTESTATION, + block.body.attestations.len() as f64, + ); + metrics::stop_timer(timer); Ok(BlockProcessingOutcome::Processed { block_root }) } @@ -958,8 +961,8 @@ impl BeaconChain { produce_at_slot: Slot, randao_reveal: Signature, ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { - metrics::BLOCK_PRODUCTION_REQUESTS.inc(); - let timer = metrics::BLOCK_PRODUCTION_TIMES.start_timer(); + metrics::inc_counter(&metrics::BLOCK_PRODUCTION_REQUESTS); + let timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_TIMES); // If required, transition the new state to the present slot. while state.slot < produce_at_slot { @@ -1011,28 +1014,28 @@ impl BeaconChain { block.state_root = state_root; - metrics::BLOCK_PRODUCTION_SUCCESSES.inc(); - timer.observe_duration(); + metrics::inc_counter(&metrics::BLOCK_PRODUCTION_SUCCESSES); + metrics::stop_timer(timer); Ok((block, state)) } /// Execute the fork choice algorithm and enthrone the result as the canonical head. pub fn fork_choice(&self) -> Result<(), Error> { - metrics::FORK_CHOICE_REQUESTS.inc(); + metrics::inc_counter(&metrics::FORK_CHOICE_REQUESTS); // Start fork choice metrics timer. - let timer = metrics::FORK_CHOICE_TIMES.start_timer(); + let timer = metrics::start_timer(&metrics::FORK_CHOICE_TIMES); // Determine the root of the block that is the head of the chain. let beacon_block_root = self.fork_choice.find_head(&self)?; // End fork choice metrics timer. - timer.observe_duration(); + metrics::stop_timer(timer); // If a new head was chosen. if beacon_block_root != self.head().beacon_block_root { - metrics::FORK_CHOICE_CHANGED_HEAD.inc(); + metrics::inc_counter(&metrics::FORK_CHOICE_CHANGED_HEAD); let beacon_block: BeaconBlock = self .store @@ -1050,7 +1053,7 @@ impl BeaconChain { // If we switched to a new chain (instead of building atop the present chain). if self.head().beacon_block_root != beacon_block.parent_root { - metrics::FORK_CHOICE_REORG_COUNT.inc(); + metrics::inc_counter(&metrics::FORK_CHOICE_REORG_COUNT); warn!( self.log, "Beacon chain re-org"; diff --git a/beacon_node/beacon_chain/src/metrics.rs b/beacon_node/beacon_chain/src/metrics.rs index 8b8307e93b..417c2904ab 100644 --- a/beacon_node/beacon_chain/src/metrics.rs +++ b/beacon_node/beacon_chain/src/metrics.rs @@ -1,111 +1,120 @@ pub use prometheus::Error; -use prometheus::{Histogram, IntCounter}; +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); + } +} lazy_static! { /* * Block Processing */ - pub static ref BLOCK_PROCESSING_DB_READ: Histogram = register_histogram!( + pub static ref BLOCK_PROCESSING_DB_READ: Result = register_histogram!( "block_processing_db_read_times", "Time spent loading block and state from DB" - ) - .unwrap(); - pub static ref BLOCK_PROCESSING_REQUESTS: IntCounter = register_int_counter!( + ); + pub static ref BLOCK_PROCESSING_REQUESTS: Result = register_int_counter!( "block_processing_requests", "Count of blocks sumbitted for processing" - ) - .unwrap(); - pub static ref BLOCK_PROCESSING_SUCCESSES: IntCounter = register_int_counter!( + ); + pub static ref BLOCK_PROCESSING_SUCCESSES: Result = register_int_counter!( "block_processing_successes", "Count of blocks processed without error" - ) - .unwrap(); - pub static ref BLOCK_PROCESSING_TIMES: Histogram = - register_histogram!("block_processing_times", "Full runtime of block processing") - .unwrap(); + ); + pub static ref BLOCK_PROCESSING_TIMES: Result = + register_histogram!("block_processing_times", "Full runtime of block processing"); /* * Block Production */ - pub static ref BLOCK_PRODUCTION_REQUESTS: IntCounter = register_int_counter!( + pub static ref BLOCK_PRODUCTION_REQUESTS: Result = register_int_counter!( "block_production_requests", "Count of all block production requests" - ) - .unwrap(); - pub static ref BLOCK_PRODUCTION_SUCCESSES: IntCounter = register_int_counter!( + ); + pub static ref BLOCK_PRODUCTION_SUCCESSES: Result = register_int_counter!( "block_production_successes", "Count of blocks sucessfully produced." - ) - .unwrap(); - pub static ref BLOCK_PRODUCTION_TIMES: Histogram = - register_histogram!("block_production_times", "Full runtime of block production").unwrap(); + ); + pub static ref BLOCK_PRODUCTION_TIMES: Result = + register_histogram!("block_production_times", "Full runtime of block production"); /* * Block Statistics */ - pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Histogram = register_histogram!( + pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Result = register_histogram!( "operations_per_block_attestation", "Number of attestations in a block" - ) - .unwrap(); + ); /* * Attestation Processing */ - pub static ref ATTESTATION_PROCESSING_REQUESTS: IntCounter = register_int_counter!( + pub static ref ATTESTATION_PROCESSING_REQUESTS: Result = register_int_counter!( "attestation_processing_requests", "Count of all attestations submitted for processing" - ) - .unwrap(); - pub static ref ATTESTATION_PROCESSING_SUCCESSES: IntCounter = register_int_counter!( + ); + pub static ref ATTESTATION_PROCESSING_SUCCESSES: Result = register_int_counter!( "attestation_processing_successes", "total_attestation_processing_successes" - ) - .unwrap(); - pub static ref ATTESTATION_PROCESSING_TIMES: Histogram = register_histogram!( + ); + pub static ref ATTESTATION_PROCESSING_TIMES: Result = register_histogram!( "attestation_processing_times", "Full runtime of attestation processing" - ) - .unwrap(); + ); /* * Attestation Production */ - pub static ref ATTESTATION_PRODUCTION_REQUESTS: IntCounter = register_int_counter!( + pub static ref ATTESTATION_PRODUCTION_REQUESTS: Result = register_int_counter!( "attestation_production_requests", "Count of all attestation production requests" - ) - .unwrap(); - pub static ref ATTESTATION_PRODUCTION_SUCCESSES: IntCounter = register_int_counter!( + ); + pub static ref ATTESTATION_PRODUCTION_SUCCESSES: Result = register_int_counter!( "attestation_production_successes", "Count of attestations processed without error" - ) - .unwrap(); - pub static ref ATTESTATION_PRODUCTION_TIMES: Histogram = register_histogram!( + ); + pub static ref ATTESTATION_PRODUCTION_TIMES: Result = register_histogram!( "attestation_production_times", "Full runtime of attestation production" - ).unwrap(); + ); /* * Fork Choice */ - pub static ref FORK_CHOICE_REQUESTS: IntCounter = register_int_counter!( + pub static ref FORK_CHOICE_REQUESTS: Result = register_int_counter!( "fork_choice_requests", "Count of occasions where fork choice has tried to find a head" - ) - .unwrap(); - pub static ref FORK_CHOICE_CHANGED_HEAD: IntCounter = register_int_counter!( + ); + pub static ref FORK_CHOICE_CHANGED_HEAD: Result = register_int_counter!( "fork_choice_changed_head", "Count of occasions fork choice has found a new head" - ) - .unwrap(); - pub static ref FORK_CHOICE_REORG_COUNT: IntCounter = register_int_counter!( + ); + pub static ref FORK_CHOICE_REORG_COUNT: Result = register_int_counter!( "fork_choice_reorg_count", "Count of occasions fork choice has switched to a different chain" - ) - .unwrap(); - pub static ref FORK_CHOICE_TIMES: Histogram = - register_histogram!("fork_choice_time", "Full runtime of fork choice").unwrap(); + ); + pub static ref FORK_CHOICE_TIMES: Result = + register_histogram!("fork_choice_time", "Full runtime of fork choice"); } pub fn gather_metrics() -> Vec {