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>,
) -> Result<AttestationData, Error> {
// 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<T: BeaconChainTypes> BeaconChain<T> {
};
// 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<T: BeaconChainTypes> BeaconChain<T> {
state: &BeaconState<T::EthSpec>,
block: &BeaconBlock<T::EthSpec>,
) -> Result<AttestationProcessingOutcome, Error> {
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<T: BeaconChainTypes> BeaconChain<T> {
.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<T: BeaconChainTypes> BeaconChain<T> {
&self,
block: BeaconBlock<T::EthSpec>,
) -> Result<BlockProcessingOutcome, Error> {
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<T: BeaconChainTypes> BeaconChain<T> {
// 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<T: BeaconChainTypes> BeaconChain<T> {
.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<T::EthSpec> = parent_state;
@ -921,9 +921,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
)
};
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<T: BeaconChainTypes> BeaconChain<T> {
produce_at_slot: Slot,
randao_reveal: Signature,
) -> Result<(BeaconBlock<T::EthSpec>, BeaconState<T::EthSpec>), 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<T: BeaconChainTypes> BeaconChain<T> {
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<T::EthSpec> = self
.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 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";

View File

@ -1,111 +1,120 @@
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! {
/*
* 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",
"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<IntCounter> = 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<IntCounter> = 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<Histogram> =
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<IntCounter> = 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<IntCounter> = 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<Histogram> =
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<Histogram> = 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<IntCounter> = 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<IntCounter> = 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<Histogram> = 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<IntCounter> = 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<IntCounter> = 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<Histogram> = 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<IntCounter> = 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<IntCounter> = 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<IntCounter> = 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<Histogram> =
register_histogram!("fork_choice_time", "Full runtime of fork choice");
}
pub fn gather_metrics() -> Vec<prometheus::proto::MetricFamily> {