Move beacon_chain to new metrics structure.

This commit is contained in:
Paul Hauner 2019-08-11 14:11:13 +10:00
parent 48733917be
commit 9995b390b5
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
3 changed files with 117 additions and 171 deletions

View File

@ -3,7 +3,6 @@ use crate::errors::{BeaconChainError as Error, BlockProductionError};
use crate::fork_choice::{Error as ForkChoiceError, ForkChoice};
use crate::iter::{ReverseBlockRootIterator, ReverseStateRootIterator};
use crate::metrics;
use crate::metrics::Metrics;
use crate::persisted_beacon_chain::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY};
use lmd_ghost::LmdGhost;
use log::trace;
@ -107,8 +106,6 @@ pub struct BeaconChain<T: BeaconChainTypes> {
/// A state-machine that is updated with information from the network and chooses a canonical
/// head block.
pub fork_choice: ForkChoice<T>,
/// Stores metrics about this `BeaconChain`.
pub metrics: Metrics,
/// Logging to CLI, etc.
log: Logger,
}
@ -158,7 +155,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
canonical_head,
genesis_block_root,
fork_choice: ForkChoice::new(store.clone(), &genesis_block, genesis_block_root),
metrics: Metrics::new()?,
store,
log,
})
@ -196,7 +192,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
canonical_head: RwLock::new(p.canonical_head),
state: RwLock::new(p.state),
genesis_block_root: p.genesis_block_root,
metrics: Metrics::new()?,
store,
log,
}))
@ -473,8 +468,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
state: &BeaconState<T::EthSpec>,
) -> Result<AttestationData, Error> {
// Collect some metrics.
self.metrics.attestation_production_requests.inc();
let timer = self.metrics.attestation_production_times.start_timer();
metrics::ATTESTATION_PRODUCTION_REQUESTS.inc();
let timer = metrics::ATTESTATION_PRODUCTION_TIMES.start_timer();
let slots_per_epoch = T::EthSpec::slots_per_epoch();
let current_epoch_start_slot = state.current_epoch().start_slot(slots_per_epoch);
@ -521,7 +516,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
};
// Collect some metrics.
self.metrics.attestation_production_successes.inc();
metrics::ATTESTATION_PRODUCTION_SUCCESSES.inc();
timer.observe_duration();
Ok(AttestationData {
@ -708,8 +703,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
state: &BeaconState<T::EthSpec>,
block: &BeaconBlock<T::EthSpec>,
) -> Result<AttestationProcessingOutcome, Error> {
self.metrics.attestation_processing_requests.inc();
let timer = self.metrics.attestation_processing_times.start_timer();
metrics::ATTESTATION_PROCESSING_REQUESTS.inc();
let timer = metrics::ATTESTATION_PROCESSING_TIMES.start_timer();
// Find the highest between:
//
@ -754,7 +749,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.insert_attestation(attestation, state, &self.spec)?;
// Update the metrics.
self.metrics.attestation_processing_successes.inc();
metrics::ATTESTATION_PROCESSING_SUCCESSES.inc();
Ok(AttestationProcessingOutcome::Processed)
};
@ -810,8 +805,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self,
block: BeaconBlock<T::EthSpec>,
) -> Result<BlockProcessingOutcome, Error> {
self.metrics.block_processing_requests.inc();
let timer = self.metrics.block_processing_times.start_timer();
metrics::BLOCK_PROCESSING_REQUESTS.inc();
let timer = metrics::BLOCK_PROCESSING_TIMES.start_timer();
let finalized_slot = self
.state
@ -926,10 +921,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
)
};
self.metrics.block_processing_successes.inc();
self.metrics
.operations_per_block_attestation
.observe(block.body.attestations.len() as f64);
metrics::BLOCK_PROCESSING_SUCCESSES.inc();
metrics::OPERATIONS_PER_BLOCK_ATTESTATION.observe(block.body.attestations.len() as f64);
timer.observe_duration();
Ok(BlockProcessingOutcome::Processed { block_root })
@ -965,8 +958,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
produce_at_slot: Slot,
randao_reveal: Signature,
) -> Result<(BeaconBlock<T::EthSpec>, BeaconState<T::EthSpec>), BlockProductionError> {
self.metrics.block_production_requests.inc();
let timer = self.metrics.block_production_times.start_timer();
metrics::BLOCK_PRODUCTION_REQUESTS.inc();
let timer = metrics::BLOCK_PRODUCTION_TIMES.start_timer();
// If required, transition the new state to the present slot.
while state.slot < produce_at_slot {
@ -1018,7 +1011,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
block.state_root = state_root;
self.metrics.block_production_successes.inc();
metrics::BLOCK_PRODUCTION_SUCCESSES.inc();
timer.observe_duration();
Ok((block, state))
@ -1026,10 +1019,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
/// Execute the fork choice algorithm and enthrone the result as the canonical head.
pub fn fork_choice(&self) -> Result<(), Error> {
self.metrics.fork_choice_requests.inc();
metrics::FORK_CHOICE_REQUESTS.inc();
// Start fork choice metrics timer.
let timer = self.metrics.fork_choice_times.start_timer();
let timer = metrics::FORK_CHOICE_TIMES.start_timer();
// Determine the root of the block that is the head of the chain.
let beacon_block_root = self.fork_choice.find_head(&self)?;
@ -1039,7 +1032,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// If a new head was chosen.
if beacon_block_root != self.head().beacon_block_root {
self.metrics.fork_choice_changed_head.inc();
metrics::FORK_CHOICE_CHANGED_HEAD.inc();
let beacon_block: BeaconBlock<T::EthSpec> = self
.store
@ -1057,7 +1050,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 {
self.metrics.fork_choice_reorg_count.inc();
metrics::FORK_CHOICE_REORG_COUNT.inc();
warn!(
self.log,
"Beacon chain re-org";

View File

@ -1,155 +1,113 @@
pub use prometheus::Error;
use prometheus::{Histogram, HistogramOpts, IntCounter, Opts, Registry};
use prometheus::{Histogram, IntCounter};
lazy_static! {
/*
* Block Processing
*/
pub static ref BLOCK_PROCESSING_DB_READ: 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!(
"block_processing_requests",
"Count of blocks sumbitted for processing"
)
.unwrap();
pub static ref BLOCK_PROCESSING_SUCCESSES: 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();
/*
* Block Production
*/
pub static ref BLOCK_PRODUCTION_REQUESTS: IntCounter = register_int_counter!(
"block_production_requests",
"Count of all block production requests"
)
.unwrap();
pub static ref BLOCK_PRODUCTION_SUCCESSES: 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();
/*
* Block Statistics
*/
pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: 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!(
"attestation_processing_requests",
"Count of all attestations submitted for processing"
)
.unwrap();
pub static ref ATTESTATION_PROCESSING_SUCCESSES: IntCounter = register_int_counter!(
"attestation_processing_successes",
"total_attestation_processing_successes"
)
.unwrap();
pub static ref ATTESTATION_PROCESSING_TIMES: Histogram = register_histogram!(
"attestation_processing_times",
"Full runtime of attestation processing"
)
.unwrap();
/*
* Attestation Production
*/
pub static ref ATTESTATION_PRODUCTION_REQUESTS: IntCounter = register_int_counter!(
"attestation_production_requests",
"Count of all attestation production requests"
)
.unwrap();
pub static ref ATTESTATION_PRODUCTION_SUCCESSES: IntCounter = register_int_counter!(
"attestation_production_successes",
"Count of attestations processed without error"
)
.unwrap();
pub static ref ATTESTATION_PRODUCTION_TIMES: Histogram = register_histogram!(
"attestation_production_times",
"Full runtime of attestation production"
).unwrap();
/*
* Fork Choice
*/
pub static ref FORK_CHOICE_REQUESTS: 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!(
"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!(
"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 fn gather_metrics() -> Vec<prometheus::proto::MetricFamily> {
prometheus::gather()
}
pub struct Metrics {
pub block_processing_requests: IntCounter,
pub block_processing_successes: IntCounter,
pub block_processing_times: Histogram,
pub block_production_requests: IntCounter,
pub block_production_successes: IntCounter,
pub block_production_times: Histogram,
pub attestation_production_requests: IntCounter,
pub attestation_production_successes: IntCounter,
pub attestation_production_times: Histogram,
pub attestation_processing_requests: IntCounter,
pub attestation_processing_successes: IntCounter,
pub attestation_processing_times: Histogram,
pub fork_choice_requests: IntCounter,
pub fork_choice_changed_head: IntCounter,
pub fork_choice_reorg_count: IntCounter,
pub fork_choice_times: Histogram,
pub operations_per_block_attestation: Histogram,
}
impl Metrics {
pub fn new() -> Result<Self, Error> {
Ok(Self {
block_processing_requests: {
let opts = Opts::new("block_processing_requests", "total_blocks_processed");
IntCounter::with_opts(opts)?
},
block_processing_successes: {
let opts = Opts::new("block_processing_successes", "total_valid_blocks_processed");
IntCounter::with_opts(opts)?
},
block_processing_times: {
let opts = HistogramOpts::new("block_processing_times", "block_processing_time");
Histogram::with_opts(opts)?
},
block_production_requests: {
let opts = Opts::new("block_production_requests", "attempts_to_produce_new_block");
IntCounter::with_opts(opts)?
},
block_production_successes: {
let opts = Opts::new("block_production_successes", "blocks_successfully_produced");
IntCounter::with_opts(opts)?
},
block_production_times: {
let opts = HistogramOpts::new("block_production_times", "block_production_time");
Histogram::with_opts(opts)?
},
attestation_production_requests: {
let opts = Opts::new(
"attestation_production_requests",
"total_attestation_production_requests",
);
IntCounter::with_opts(opts)?
},
attestation_production_successes: {
let opts = Opts::new(
"attestation_production_successes",
"total_attestation_production_successes",
);
IntCounter::with_opts(opts)?
},
attestation_production_times: {
let opts = HistogramOpts::new(
"attestation_production_times",
"attestation_production_time",
);
Histogram::with_opts(opts)?
},
attestation_processing_requests: {
let opts = Opts::new(
"attestation_processing_requests",
"total_attestation_processing_requests",
);
IntCounter::with_opts(opts)?
},
attestation_processing_successes: {
let opts = Opts::new(
"attestation_processing_successes",
"total_attestation_processing_successes",
);
IntCounter::with_opts(opts)?
},
attestation_processing_times: {
let opts = HistogramOpts::new(
"attestation_processing_times",
"attestation_processing_time",
);
Histogram::with_opts(opts)?
},
fork_choice_requests: {
let opts = Opts::new("fork_choice_requests", "total_times_fork_choice_called");
IntCounter::with_opts(opts)?
},
fork_choice_changed_head: {
let opts = Opts::new(
"fork_choice_changed_head",
"total_times_fork_choice_chose_a_new_head",
);
IntCounter::with_opts(opts)?
},
fork_choice_reorg_count: {
let opts = Opts::new("fork_choice_reorg_count", "number_of_reorgs");
IntCounter::with_opts(opts)?
},
fork_choice_times: {
let opts = HistogramOpts::new("fork_choice_time", "total_time_to_run_fork_choice");
Histogram::with_opts(opts)?
},
operations_per_block_attestation: {
let opts = HistogramOpts::new(
"operations_per_block_attestation",
"count_of_attestations_per_block",
);
Histogram::with_opts(opts)?
},
})
}
pub fn register(&self, registry: &Registry) -> Result<(), Error> {
registry.register(Box::new(self.block_processing_requests.clone()))?;
registry.register(Box::new(self.block_processing_successes.clone()))?;
registry.register(Box::new(self.block_processing_times.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_times.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_times.clone()))?;
registry.register(Box::new(self.attestation_processing_requests.clone()))?;
registry.register(Box::new(self.attestation_processing_successes.clone()))?;
registry.register(Box::new(self.attestation_processing_times.clone()))?;
registry.register(Box::new(self.fork_choice_requests.clone()))?;
registry.register(Box::new(self.fork_choice_changed_head.clone()))?;
registry.register(Box::new(self.fork_choice_reorg_count.clone()))?;
registry.register(Box::new(self.fork_choice_times.clone()))?;
registry.register(Box::new(self.operations_per_block_attestation.clone()))?;
Ok(())
}
}

View File

@ -71,11 +71,6 @@ where
eth2_config.spec.clone(),
log.clone(),
)?);
// Registry all beacon chain metrics with the global registry.
beacon_chain
.metrics
.register(&metrics_registry)
.expect("Failed to registry metrics");
if beacon_chain.read_slot_clock().is_none() {
panic!("Cannot start client before genesis!")