Unify common metric fns into a crate
This commit is contained in:
parent
36ff115b04
commit
2108895fca
@ -11,6 +11,7 @@ members = [
|
||||
"eth2/utils/eth2_interop_keypairs",
|
||||
"eth2/utils/logging",
|
||||
"eth2/utils/eth2_hashing",
|
||||
"eth2/utils/lighthouse_metrics",
|
||||
"eth2/utils/merkle_proof",
|
||||
"eth2/utils/int_to_bytes",
|
||||
"eth2/utils/serde_hex",
|
||||
|
@ -7,7 +7,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
store = { path = "../store" }
|
||||
parking_lot = "0.7"
|
||||
prometheus = "^0.6"
|
||||
lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
|
||||
log = "0.4"
|
||||
operation_pool = { path = "../../eth2/operation_pool" }
|
||||
serde = "1.0"
|
||||
|
@ -1,5 +1,4 @@
|
||||
use crate::fork_choice::Error as ForkChoiceError;
|
||||
use crate::metrics::Error as MetricsError;
|
||||
use state_processing::per_block_processing::errors::{
|
||||
AttestationValidationError, IndexedAttestationValidationError,
|
||||
};
|
||||
@ -34,7 +33,6 @@ pub enum BeaconChainError {
|
||||
MissingBeaconBlock(Hash256),
|
||||
MissingBeaconState(Hash256),
|
||||
SlotProcessingError(SlotProcessingError),
|
||||
MetricsError(String),
|
||||
NoStateForAttestation {
|
||||
beacon_block_root: Hash256,
|
||||
},
|
||||
@ -44,12 +42,6 @@ pub enum BeaconChainError {
|
||||
|
||||
easy_from_to!(SlotProcessingError, BeaconChainError);
|
||||
|
||||
impl From<MetricsError> for BeaconChainError {
|
||||
fn from(e: MetricsError) -> BeaconChainError {
|
||||
BeaconChainError::MetricsError(format!("{:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum BlockProductionError {
|
||||
UnableToGetBlockRootFromState,
|
||||
|
@ -1,6 +1,4 @@
|
||||
#[macro_use]
|
||||
extern crate prometheus;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod beacon_chain;
|
||||
@ -18,7 +16,6 @@ pub use self::beacon_chain::{
|
||||
pub use self::checkpoint::CheckPoint;
|
||||
pub use self::errors::{BeaconChainError, BlockProductionError};
|
||||
pub use lmd_ghost;
|
||||
pub use metrics::gather_metrics;
|
||||
pub use parking_lot;
|
||||
pub use slot_clock;
|
||||
pub use state_processing::per_block_processing::errors::{
|
||||
|
@ -1,67 +1,42 @@
|
||||
pub use prometheus::Error;
|
||||
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);
|
||||
}
|
||||
}
|
||||
pub use lighthouse_metrics::*;
|
||||
|
||||
lazy_static! {
|
||||
/*
|
||||
* Block Processing
|
||||
*/
|
||||
pub static ref BLOCK_PROCESSING_DB_READ: Result<Histogram> = register_histogram!(
|
||||
pub static ref BLOCK_PROCESSING_DB_READ: Result<Histogram> = try_create_histogram(
|
||||
"block_processing_db_read_times",
|
||||
"Time spent loading block and state from DB"
|
||||
);
|
||||
pub static ref BLOCK_PROCESSING_REQUESTS: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref BLOCK_PROCESSING_REQUESTS: Result<IntCounter> = try_create_int_counter(
|
||||
"block_processing_requests",
|
||||
"Count of blocks sumbitted for processing"
|
||||
"Count of blocks submitted for processing"
|
||||
);
|
||||
pub static ref BLOCK_PROCESSING_SUCCESSES: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref BLOCK_PROCESSING_SUCCESSES: Result<IntCounter> = try_create_int_counter(
|
||||
"block_processing_successes",
|
||||
"Count of blocks processed without error"
|
||||
);
|
||||
pub static ref BLOCK_PROCESSING_TIMES: Result<Histogram> =
|
||||
register_histogram!("block_processing_times", "Full runtime of block processing");
|
||||
try_create_histogram("block_processing_times", "Full runtime of block processing");
|
||||
|
||||
/*
|
||||
* Block Production
|
||||
*/
|
||||
pub static ref BLOCK_PRODUCTION_REQUESTS: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref BLOCK_PRODUCTION_REQUESTS: Result<IntCounter> = try_create_int_counter(
|
||||
"block_production_requests",
|
||||
"Count of all block production requests"
|
||||
);
|
||||
pub static ref BLOCK_PRODUCTION_SUCCESSES: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref BLOCK_PRODUCTION_SUCCESSES: Result<IntCounter> = try_create_int_counter(
|
||||
"block_production_successes",
|
||||
"Count of blocks sucessfully produced."
|
||||
"Count of blocks successfully produced."
|
||||
);
|
||||
pub static ref BLOCK_PRODUCTION_TIMES: Result<Histogram> =
|
||||
register_histogram!("block_production_times", "Full runtime of block production");
|
||||
try_create_histogram("block_production_times", "Full runtime of block production");
|
||||
|
||||
/*
|
||||
* Block Statistics
|
||||
*/
|
||||
pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Result<Histogram> = register_histogram!(
|
||||
pub static ref OPERATIONS_PER_BLOCK_ATTESTATION: Result<Histogram> = try_create_histogram(
|
||||
"operations_per_block_attestation",
|
||||
"Number of attestations in a block"
|
||||
);
|
||||
@ -69,15 +44,15 @@ lazy_static! {
|
||||
/*
|
||||
* Attestation Processing
|
||||
*/
|
||||
pub static ref ATTESTATION_PROCESSING_REQUESTS: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref ATTESTATION_PROCESSING_REQUESTS: Result<IntCounter> = try_create_int_counter(
|
||||
"attestation_processing_requests",
|
||||
"Count of all attestations submitted for processing"
|
||||
);
|
||||
pub static ref ATTESTATION_PROCESSING_SUCCESSES: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref ATTESTATION_PROCESSING_SUCCESSES: Result<IntCounter> = try_create_int_counter(
|
||||
"attestation_processing_successes",
|
||||
"total_attestation_processing_successes"
|
||||
);
|
||||
pub static ref ATTESTATION_PROCESSING_TIMES: Result<Histogram> = register_histogram!(
|
||||
pub static ref ATTESTATION_PROCESSING_TIMES: Result<Histogram> = try_create_histogram(
|
||||
"attestation_processing_times",
|
||||
"Full runtime of attestation processing"
|
||||
);
|
||||
@ -85,15 +60,15 @@ lazy_static! {
|
||||
/*
|
||||
* Attestation Production
|
||||
*/
|
||||
pub static ref ATTESTATION_PRODUCTION_REQUESTS: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref ATTESTATION_PRODUCTION_REQUESTS: Result<IntCounter> = try_create_int_counter(
|
||||
"attestation_production_requests",
|
||||
"Count of all attestation production requests"
|
||||
);
|
||||
pub static ref ATTESTATION_PRODUCTION_SUCCESSES: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref ATTESTATION_PRODUCTION_SUCCESSES: Result<IntCounter> = try_create_int_counter(
|
||||
"attestation_production_successes",
|
||||
"Count of attestations processed without error"
|
||||
);
|
||||
pub static ref ATTESTATION_PRODUCTION_TIMES: Result<Histogram> = register_histogram!(
|
||||
pub static ref ATTESTATION_PRODUCTION_TIMES: Result<Histogram> = try_create_histogram(
|
||||
"attestation_production_times",
|
||||
"Full runtime of attestation production"
|
||||
);
|
||||
@ -101,18 +76,18 @@ lazy_static! {
|
||||
/*
|
||||
* Fork Choice
|
||||
*/
|
||||
pub static ref FORK_CHOICE_REQUESTS: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref FORK_CHOICE_REQUESTS: Result<IntCounter> = try_create_int_counter(
|
||||
"fork_choice_requests",
|
||||
"Count of occasions where fork choice has tried to find a head"
|
||||
);
|
||||
pub static ref FORK_CHOICE_CHANGED_HEAD: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref FORK_CHOICE_CHANGED_HEAD: Result<IntCounter> = try_create_int_counter(
|
||||
"fork_choice_changed_head",
|
||||
"Count of occasions fork choice has found a new head"
|
||||
);
|
||||
pub static ref FORK_CHOICE_REORG_COUNT: Result<IntCounter> = register_int_counter!(
|
||||
pub static ref FORK_CHOICE_REORG_COUNT: Result<IntCounter> = try_create_int_counter(
|
||||
"fork_choice_reorg_count",
|
||||
"Count of occasions fork choice has switched to a different chain"
|
||||
);
|
||||
pub static ref FORK_CHOICE_TIMES: Result<Histogram> =
|
||||
register_histogram!("fork_choice_time", "Full runtime of fork choice");
|
||||
try_create_histogram("fork_choice_time", "Full runtime of fork choice");
|
||||
}
|
||||
|
11
eth2/utils/lighthouse_metrics/Cargo.toml
Normal file
11
eth2/utils/lighthouse_metrics/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "lighthouse_metrics"
|
||||
version = "0.1.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1.3.0"
|
||||
prometheus = "^0.6"
|
49
eth2/utils/lighthouse_metrics/src/lib.rs
Normal file
49
eth2/utils/lighthouse_metrics/src/lib.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use prometheus::{HistogramOpts, HistogramTimer, Opts};
|
||||
|
||||
pub use prometheus::{Histogram, IntCounter, Result};
|
||||
|
||||
pub fn try_create_int_counter(name: &str, help: &str) -> Result<IntCounter> {
|
||||
let opts = Opts::new(name, help);
|
||||
let counter = IntCounter::with_opts(opts)?;
|
||||
prometheus::register(Box::new(counter.clone()))?;
|
||||
Ok(counter)
|
||||
}
|
||||
|
||||
pub fn try_create_histogram(name: &str, help: &str) -> Result<Histogram> {
|
||||
let opts = HistogramOpts::new(name, help);
|
||||
let histogram = Histogram::with_opts(opts)?;
|
||||
prometheus::register(Box::new(histogram.clone()))?;
|
||||
Ok(histogram)
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert_eq!(2 + 2, 4);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user