Add additional metrics to BeaconChain

This commit is contained in:
Paul Hauner 2019-05-29 15:45:09 +10:00
parent a153f24bfa
commit 64fbc6bf3c
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
2 changed files with 42 additions and 9 deletions

View File

@ -530,6 +530,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> { pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
trace!("BeaconChain::produce_attestation: shard: {}", shard); trace!("BeaconChain::produce_attestation: shard: {}", shard);
self.metrics.attestation_production_requests.inc();
let timer = self.metrics.attestation_production_histogram.start_timer();
let state = self.state.read(); let state = self.state.read();
let current_epoch_start_slot = self let current_epoch_start_slot = self
@ -557,6 +560,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
*self.state.read().get_block_root(current_epoch_start_slot)? *self.state.read().get_block_root(current_epoch_start_slot)?
}; };
self.metrics.attestation_production_successes.inc();
timer.observe_duration();
Ok(AttestationData { Ok(AttestationData {
slot: self.state.read().slot, slot: self.state.read().slot,
shard, shard,
@ -625,7 +631,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
/// Will accept blocks from prior slots, however it will reject any block from a future slot. /// Will accept blocks from prior slots, however it will reject any block from a future slot.
pub fn process_block(&self, block: BeaconBlock) -> Result<BlockProcessingOutcome, Error> { pub fn process_block(&self, block: BeaconBlock) -> Result<BlockProcessingOutcome, Error> {
debug!("Processing block with slot {}...", block.slot); debug!("Processing block with slot {}...", block.slot);
self.metrics.blocks_processed.inc(); self.metrics.block_processing_requests.inc();
let timer = self.metrics.block_processing_historgram.start_timer();
let block_root = block.block_header().canonical_root(); let block_root = block.block_header().canonical_root();
@ -709,7 +716,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
self.update_state(state)?; self.update_state(state)?;
} }
self.metrics.valid_blocks_processed.inc(); self.metrics.block_processing_successes.inc();
timer.observe_duration();
Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed)) Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed))
} }
@ -724,6 +732,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
) -> Result<(BeaconBlock, BeaconState<T::EthSpec>), BlockProductionError> { ) -> Result<(BeaconBlock, BeaconState<T::EthSpec>), BlockProductionError> {
debug!("Producing block at slot {}...", self.state.read().slot); debug!("Producing block at slot {}...", self.state.read().slot);
self.metrics.block_production_requests.inc(); self.metrics.block_production_requests.inc();
let timer = self.metrics.block_production_historgram.start_timer();
let mut state = self.state.read().clone(); let mut state = self.state.read().clone();
@ -775,6 +784,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
block.state_root = state_root; block.state_root = state_root;
self.metrics.block_production_successes.inc(); self.metrics.block_production_successes.inc();
timer.observe_duration();
Ok((block, state)) Ok((block, state))
} }

View File

@ -1,26 +1,34 @@
pub use prometheus::Error; pub use prometheus::Error;
use prometheus::{IntCounter, Opts, Registry}; use prometheus::{Histogram, HistogramOpts, IntCounter, Opts, Registry};
pub struct Metrics { pub struct Metrics {
pub blocks_processed: IntCounter, pub block_processing_requests: IntCounter,
pub valid_blocks_processed: IntCounter, pub block_processing_successes: IntCounter,
pub block_processing_historgram: Histogram,
pub block_production_requests: IntCounter, pub block_production_requests: IntCounter,
pub block_production_successes: IntCounter, pub block_production_successes: IntCounter,
pub block_production_historgram: Histogram,
pub attestation_production_requests: IntCounter, pub attestation_production_requests: IntCounter,
pub attestation_production_successes: IntCounter, pub attestation_production_successes: IntCounter,
pub attestation_production_histogram: Histogram,
} }
impl Metrics { impl Metrics {
pub fn new() -> Result<Self, Error> { pub fn new() -> Result<Self, Error> {
Ok(Self { Ok(Self {
blocks_processed: { block_processing_requests: {
let opts = Opts::new("blocks_processed", "total_blocks_processed"); let opts = Opts::new("blocks_processed", "total_blocks_processed");
IntCounter::with_opts(opts)? IntCounter::with_opts(opts)?
}, },
valid_blocks_processed: { block_processing_successes: {
let opts = Opts::new("valid_blocks_processed", "total_valid_blocks_processed"); let opts = Opts::new("valid_blocks_processed", "total_valid_blocks_processed");
IntCounter::with_opts(opts)? IntCounter::with_opts(opts)?
}, },
block_processing_historgram: {
let opts =
HistogramOpts::new("block_processing_historgram", "block_processing_time");
Histogram::with_opts(opts)?
},
block_production_requests: { block_production_requests: {
let opts = Opts::new("block_production_requests", "attempts_to_produce_new_block"); let opts = Opts::new("block_production_requests", "attempts_to_produce_new_block");
IntCounter::with_opts(opts)? IntCounter::with_opts(opts)?
@ -29,6 +37,11 @@ impl Metrics {
let opts = Opts::new("block_production_successes", "blocks_successfully_produced"); let opts = Opts::new("block_production_successes", "blocks_successfully_produced");
IntCounter::with_opts(opts)? IntCounter::with_opts(opts)?
}, },
block_production_historgram: {
let opts =
HistogramOpts::new("block_production_historgram", "block_production_time");
Histogram::with_opts(opts)?
},
attestation_production_requests: { attestation_production_requests: {
let opts = Opts::new( let opts = Opts::new(
"attestation_production_requests", "attestation_production_requests",
@ -43,16 +56,26 @@ impl Metrics {
); );
IntCounter::with_opts(opts)? IntCounter::with_opts(opts)?
}, },
attestation_production_histogram: {
let opts = HistogramOpts::new(
"attestation_production_histogram",
"attestation_production_time",
);
Histogram::with_opts(opts)?
},
}) })
} }
pub fn register(&self, registry: &Registry) -> Result<(), Error> { pub fn register(&self, registry: &Registry) -> Result<(), Error> {
registry.register(Box::new(self.blocks_processed.clone()))?; registry.register(Box::new(self.block_processing_requests.clone()))?;
registry.register(Box::new(self.valid_blocks_processed.clone()))?; registry.register(Box::new(self.block_processing_successes.clone()))?;
registry.register(Box::new(self.block_processing_historgram.clone()))?;
registry.register(Box::new(self.block_production_requests.clone()))?; registry.register(Box::new(self.block_production_requests.clone()))?;
registry.register(Box::new(self.block_production_successes.clone()))?; registry.register(Box::new(self.block_production_successes.clone()))?;
registry.register(Box::new(self.block_production_historgram.clone()))?;
registry.register(Box::new(self.attestation_production_requests.clone()))?; registry.register(Box::new(self.attestation_production_requests.clone()))?;
registry.register(Box::new(self.attestation_production_successes.clone()))?; registry.register(Box::new(self.attestation_production_successes.clone()))?;
registry.register(Box::new(self.attestation_production_histogram.clone()))?;
Ok(()) Ok(())
} }