Make metrics not panic if already defined

This commit is contained in:
Paul Hauner 2019-08-11 14:43:31 +10:00
parent 9995b390b5
commit e33d0703ef
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 88 additions and 76 deletions

View File

@ -468,8 +468,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
state: &BeaconState<T::EthSpec>, state: &BeaconState<T::EthSpec>,
) -> Result<AttestationData, Error> { ) -> Result<AttestationData, Error> {
// Collect some metrics. // Collect some metrics.
metrics::ATTESTATION_PRODUCTION_REQUESTS.inc(); metrics::inc_counter(&metrics::ATTESTATION_PRODUCTION_REQUESTS);
let timer = metrics::ATTESTATION_PRODUCTION_TIMES.start_timer(); let timer = metrics::start_timer(&metrics::ATTESTATION_PRODUCTION_TIMES);
let slots_per_epoch = T::EthSpec::slots_per_epoch(); let slots_per_epoch = T::EthSpec::slots_per_epoch();
let current_epoch_start_slot = state.current_epoch().start_slot(slots_per_epoch); let current_epoch_start_slot = state.current_epoch().start_slot(slots_per_epoch);
@ -516,8 +516,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}; };
// Collect some metrics. // Collect some metrics.
metrics::ATTESTATION_PRODUCTION_SUCCESSES.inc(); metrics::inc_counter(&metrics::ATTESTATION_PRODUCTION_SUCCESSES);
timer.observe_duration(); metrics::stop_timer(timer);
Ok(AttestationData { Ok(AttestationData {
beacon_block_root: head_block_root, beacon_block_root: head_block_root,
@ -703,8 +703,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
state: &BeaconState<T::EthSpec>, state: &BeaconState<T::EthSpec>,
block: &BeaconBlock<T::EthSpec>, block: &BeaconBlock<T::EthSpec>,
) -> Result<AttestationProcessingOutcome, Error> { ) -> Result<AttestationProcessingOutcome, Error> {
metrics::ATTESTATION_PROCESSING_REQUESTS.inc(); metrics::inc_counter(&metrics::ATTESTATION_PROCESSING_REQUESTS);
let timer = metrics::ATTESTATION_PROCESSING_TIMES.start_timer(); let timer = metrics::start_timer(&metrics::ATTESTATION_PROCESSING_TIMES);
// Find the highest between: // Find the highest between:
// //
@ -749,12 +749,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.insert_attestation(attestation, state, &self.spec)?; .insert_attestation(attestation, state, &self.spec)?;
// Update the metrics. // Update the metrics.
metrics::ATTESTATION_PROCESSING_SUCCESSES.inc(); metrics::inc_counter(&metrics::ATTESTATION_PROCESSING_SUCCESSES);
Ok(AttestationProcessingOutcome::Processed) Ok(AttestationProcessingOutcome::Processed)
}; };
timer.observe_duration(); timer.map(|t| t.observe_duration());
result result
} }
@ -805,8 +805,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self, &self,
block: BeaconBlock<T::EthSpec>, block: BeaconBlock<T::EthSpec>,
) -> Result<BlockProcessingOutcome, Error> { ) -> Result<BlockProcessingOutcome, Error> {
metrics::BLOCK_PROCESSING_REQUESTS.inc(); metrics::inc_counter(&metrics::BLOCK_PROCESSING_REQUESTS);
let timer = metrics::BLOCK_PROCESSING_TIMES.start_timer(); let timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_TIMES);
let finalized_slot = self let finalized_slot = self
.state .state
@ -846,7 +846,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// Records the time taken to load the block and state from the database during block // Records the time taken to load the block and state from the database during block
// processing. // 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 // Load the blocks parent block from the database, returning invalid if that block is not
// found. // found.
@ -867,7 +867,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.get(&parent_state_root)? .get(&parent_state_root)?
.ok_or_else(|| Error::DBInconsistent(format!("Missing state {}", 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. // Transition the parent state to the block slot.
let mut state: BeaconState<T::EthSpec> = parent_state; let mut state: BeaconState<T::EthSpec> = parent_state;
@ -921,9 +921,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
) )
}; };
metrics::BLOCK_PROCESSING_SUCCESSES.inc(); metrics::inc_counter(&metrics::BLOCK_PROCESSING_SUCCESSES);
metrics::OPERATIONS_PER_BLOCK_ATTESTATION.observe(block.body.attestations.len() as f64); metrics::observe(
timer.observe_duration(); &metrics::OPERATIONS_PER_BLOCK_ATTESTATION,
block.body.attestations.len() as f64,
);
metrics::stop_timer(timer);
Ok(BlockProcessingOutcome::Processed { block_root }) Ok(BlockProcessingOutcome::Processed { block_root })
} }
@ -958,8 +961,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
produce_at_slot: Slot, produce_at_slot: Slot,
randao_reveal: Signature, randao_reveal: Signature,
) -> Result<(BeaconBlock<T::EthSpec>, BeaconState<T::EthSpec>), BlockProductionError> { ) -> Result<(BeaconBlock<T::EthSpec>, BeaconState<T::EthSpec>), BlockProductionError> {
metrics::BLOCK_PRODUCTION_REQUESTS.inc(); metrics::inc_counter(&metrics::BLOCK_PRODUCTION_REQUESTS);
let timer = metrics::BLOCK_PRODUCTION_TIMES.start_timer(); let timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_TIMES);
// If required, transition the new state to the present slot. // If required, transition the new state to the present slot.
while state.slot < produce_at_slot { while state.slot < produce_at_slot {
@ -1011,28 +1014,28 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
block.state_root = state_root; block.state_root = state_root;
metrics::BLOCK_PRODUCTION_SUCCESSES.inc(); metrics::inc_counter(&metrics::BLOCK_PRODUCTION_SUCCESSES);
timer.observe_duration(); metrics::stop_timer(timer);
Ok((block, state)) Ok((block, state))
} }
/// Execute the fork choice algorithm and enthrone the result as the canonical head. /// Execute the fork choice algorithm and enthrone the result as the canonical head.
pub fn fork_choice(&self) -> Result<(), Error> { pub fn fork_choice(&self) -> Result<(), Error> {
metrics::FORK_CHOICE_REQUESTS.inc(); metrics::inc_counter(&metrics::FORK_CHOICE_REQUESTS);
// Start fork choice metrics timer. // 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. // Determine the root of the block that is the head of the chain.
let beacon_block_root = self.fork_choice.find_head(&self)?; let beacon_block_root = self.fork_choice.find_head(&self)?;
// End fork choice metrics timer. // End fork choice metrics timer.
timer.observe_duration(); metrics::stop_timer(timer);
// If a new head was chosen. // If a new head was chosen.
if beacon_block_root != self.head().beacon_block_root { 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<T::EthSpec> = self let beacon_block: BeaconBlock<T::EthSpec> = self
.store .store
@ -1050,7 +1053,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// If we switched to a new chain (instead of building atop the present chain). // If we switched to a new chain (instead of building atop the present chain).
if self.head().beacon_block_root != beacon_block.parent_root { 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!( warn!(
self.log, self.log,
"Beacon chain re-org"; "Beacon chain re-org";

View File

@ -1,111 +1,120 @@
pub use prometheus::Error; pub use prometheus::Error;
use prometheus::{Histogram, IntCounter}; use prometheus::{Histogram, HistogramTimer, IntCounter, Result};
pub fn start_timer(histogram: &Result<Histogram>) -> Option<HistogramTimer> {
if let Ok(histogram) = histogram {
Some(histogram.start_timer())
} else {
None
}
}
pub fn stop_timer(timer: Option<HistogramTimer>) {
timer.map(|t| t.observe_duration());
}
pub fn inc_counter(counter: &Result<IntCounter>) {
if let Ok(counter) = counter {
counter.inc();
}
}
pub fn observe(histogram: &Result<Histogram>, value: f64) {
if let Ok(histogram) = histogram {
histogram.observe(value);
}
}
lazy_static! { lazy_static! {
/* /*
* Block Processing * Block Processing
*/ */
pub static ref BLOCK_PROCESSING_DB_READ: Histogram = register_histogram!( pub static ref BLOCK_PROCESSING_DB_READ: Result<Histogram> = register_histogram!(
"block_processing_db_read_times", "block_processing_db_read_times",
"Time spent loading block and state from DB" "Time spent loading block and state from DB"
) );
.unwrap(); pub static ref BLOCK_PROCESSING_REQUESTS: Result<IntCounter> = register_int_counter!(
pub static ref BLOCK_PROCESSING_REQUESTS: IntCounter = register_int_counter!(
"block_processing_requests", "block_processing_requests",
"Count of blocks sumbitted for processing" "Count of blocks sumbitted for processing"
) );
.unwrap(); pub static ref BLOCK_PROCESSING_SUCCESSES: Result<IntCounter> = register_int_counter!(
pub static ref BLOCK_PROCESSING_SUCCESSES: IntCounter = register_int_counter!(
"block_processing_successes", "block_processing_successes",
"Count of blocks processed without error" "Count of blocks processed without error"
) );
.unwrap(); pub static ref BLOCK_PROCESSING_TIMES: Result<Histogram> =
pub static ref BLOCK_PROCESSING_TIMES: Histogram = register_histogram!("block_processing_times", "Full runtime of block processing");
register_histogram!("block_processing_times", "Full runtime of block processing")
.unwrap();
/* /*
* Block Production * Block Production
*/ */
pub static ref BLOCK_PRODUCTION_REQUESTS: IntCounter = register_int_counter!( pub static ref BLOCK_PRODUCTION_REQUESTS: Result<IntCounter> = register_int_counter!(
"block_production_requests", "block_production_requests",
"Count of all block production requests" "Count of all block production requests"
) );
.unwrap(); pub static ref BLOCK_PRODUCTION_SUCCESSES: Result<IntCounter> = register_int_counter!(
pub static ref BLOCK_PRODUCTION_SUCCESSES: IntCounter = register_int_counter!(
"block_production_successes", "block_production_successes",
"Count of blocks sucessfully produced." "Count of blocks sucessfully produced."
) );
.unwrap(); pub static ref BLOCK_PRODUCTION_TIMES: Result<Histogram> =
pub static ref BLOCK_PRODUCTION_TIMES: Histogram = register_histogram!("block_production_times", "Full runtime of block production");
register_histogram!("block_production_times", "Full runtime of block production").unwrap();
/* /*
* Block Statistics * Block Statistics
*/ */
pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Histogram = register_histogram!( pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Result<Histogram> = register_histogram!(
"operations_per_block_attestation", "operations_per_block_attestation",
"Number of attestations in a block" "Number of attestations in a block"
) );
.unwrap();
/* /*
* Attestation Processing * Attestation Processing
*/ */
pub static ref ATTESTATION_PROCESSING_REQUESTS: IntCounter = register_int_counter!( pub static ref ATTESTATION_PROCESSING_REQUESTS: Result<IntCounter> = register_int_counter!(
"attestation_processing_requests", "attestation_processing_requests",
"Count of all attestations submitted for processing" "Count of all attestations submitted for processing"
) );
.unwrap(); pub static ref ATTESTATION_PROCESSING_SUCCESSES: Result<IntCounter> = register_int_counter!(
pub static ref ATTESTATION_PROCESSING_SUCCESSES: IntCounter = register_int_counter!(
"attestation_processing_successes", "attestation_processing_successes",
"total_attestation_processing_successes" "total_attestation_processing_successes"
) );
.unwrap(); pub static ref ATTESTATION_PROCESSING_TIMES: Result<Histogram> = register_histogram!(
pub static ref ATTESTATION_PROCESSING_TIMES: Histogram = register_histogram!(
"attestation_processing_times", "attestation_processing_times",
"Full runtime of attestation processing" "Full runtime of attestation processing"
) );
.unwrap();
/* /*
* Attestation Production * Attestation Production
*/ */
pub static ref ATTESTATION_PRODUCTION_REQUESTS: IntCounter = register_int_counter!( pub static ref ATTESTATION_PRODUCTION_REQUESTS: Result<IntCounter> = register_int_counter!(
"attestation_production_requests", "attestation_production_requests",
"Count of all attestation production requests" "Count of all attestation production requests"
) );
.unwrap(); pub static ref ATTESTATION_PRODUCTION_SUCCESSES: Result<IntCounter> = register_int_counter!(
pub static ref ATTESTATION_PRODUCTION_SUCCESSES: IntCounter = register_int_counter!(
"attestation_production_successes", "attestation_production_successes",
"Count of attestations processed without error" "Count of attestations processed without error"
) );
.unwrap(); pub static ref ATTESTATION_PRODUCTION_TIMES: Result<Histogram> = register_histogram!(
pub static ref ATTESTATION_PRODUCTION_TIMES: Histogram = register_histogram!(
"attestation_production_times", "attestation_production_times",
"Full runtime of attestation production" "Full runtime of attestation production"
).unwrap(); );
/* /*
* Fork Choice * Fork Choice
*/ */
pub static ref FORK_CHOICE_REQUESTS: IntCounter = register_int_counter!( pub static ref FORK_CHOICE_REQUESTS: Result<IntCounter> = register_int_counter!(
"fork_choice_requests", "fork_choice_requests",
"Count of occasions where fork choice has tried to find a head" "Count of occasions where fork choice has tried to find a head"
) );
.unwrap(); pub static ref FORK_CHOICE_CHANGED_HEAD: Result<IntCounter> = register_int_counter!(
pub static ref FORK_CHOICE_CHANGED_HEAD: IntCounter = register_int_counter!(
"fork_choice_changed_head", "fork_choice_changed_head",
"Count of occasions fork choice has found a new head" "Count of occasions fork choice has found a new head"
) );
.unwrap(); pub static ref FORK_CHOICE_REORG_COUNT: Result<IntCounter> = register_int_counter!(
pub static ref FORK_CHOICE_REORG_COUNT: IntCounter = register_int_counter!(
"fork_choice_reorg_count", "fork_choice_reorg_count",
"Count of occasions fork choice has switched to a different chain" "Count of occasions fork choice has switched to a different chain"
) );
.unwrap(); pub static ref FORK_CHOICE_TIMES: Result<Histogram> =
pub static ref FORK_CHOICE_TIMES: Histogram = register_histogram!("fork_choice_time", "Full runtime of fork choice");
register_histogram!("fork_choice_time", "Full runtime of fork choice").unwrap();
} }
pub fn gather_metrics() -> Vec<prometheus::proto::MetricFamily> { pub fn gather_metrics() -> Vec<prometheus::proto::MetricFamily> {