Unify common metric fns into a crate

This commit is contained in:
Paul Hauner 2019-08-11 15:34:10 +10:00
parent 36ff115b04
commit 2108895fca
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
7 changed files with 83 additions and 58 deletions

View File

@ -11,6 +11,7 @@ members = [
"eth2/utils/eth2_interop_keypairs", "eth2/utils/eth2_interop_keypairs",
"eth2/utils/logging", "eth2/utils/logging",
"eth2/utils/eth2_hashing", "eth2/utils/eth2_hashing",
"eth2/utils/lighthouse_metrics",
"eth2/utils/merkle_proof", "eth2/utils/merkle_proof",
"eth2/utils/int_to_bytes", "eth2/utils/int_to_bytes",
"eth2/utils/serde_hex", "eth2/utils/serde_hex",

View File

@ -7,7 +7,7 @@ edition = "2018"
[dependencies] [dependencies]
store = { path = "../store" } store = { path = "../store" }
parking_lot = "0.7" parking_lot = "0.7"
prometheus = "^0.6" lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
log = "0.4" log = "0.4"
operation_pool = { path = "../../eth2/operation_pool" } operation_pool = { path = "../../eth2/operation_pool" }
serde = "1.0" serde = "1.0"

View File

@ -1,5 +1,4 @@
use crate::fork_choice::Error as ForkChoiceError; use crate::fork_choice::Error as ForkChoiceError;
use crate::metrics::Error as MetricsError;
use state_processing::per_block_processing::errors::{ use state_processing::per_block_processing::errors::{
AttestationValidationError, IndexedAttestationValidationError, AttestationValidationError, IndexedAttestationValidationError,
}; };
@ -34,7 +33,6 @@ pub enum BeaconChainError {
MissingBeaconBlock(Hash256), MissingBeaconBlock(Hash256),
MissingBeaconState(Hash256), MissingBeaconState(Hash256),
SlotProcessingError(SlotProcessingError), SlotProcessingError(SlotProcessingError),
MetricsError(String),
NoStateForAttestation { NoStateForAttestation {
beacon_block_root: Hash256, beacon_block_root: Hash256,
}, },
@ -44,12 +42,6 @@ pub enum BeaconChainError {
easy_from_to!(SlotProcessingError, BeaconChainError); easy_from_to!(SlotProcessingError, BeaconChainError);
impl From<MetricsError> for BeaconChainError {
fn from(e: MetricsError) -> BeaconChainError {
BeaconChainError::MetricsError(format!("{:?}", e))
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum BlockProductionError { pub enum BlockProductionError {
UnableToGetBlockRootFromState, UnableToGetBlockRootFromState,

View File

@ -1,6 +1,4 @@
#[macro_use] #[macro_use]
extern crate prometheus;
#[macro_use]
extern crate lazy_static; extern crate lazy_static;
mod beacon_chain; mod beacon_chain;
@ -18,7 +16,6 @@ pub use self::beacon_chain::{
pub use self::checkpoint::CheckPoint; pub use self::checkpoint::CheckPoint;
pub use self::errors::{BeaconChainError, BlockProductionError}; pub use self::errors::{BeaconChainError, BlockProductionError};
pub use lmd_ghost; pub use lmd_ghost;
pub use metrics::gather_metrics;
pub use parking_lot; pub use parking_lot;
pub use slot_clock; pub use slot_clock;
pub use state_processing::per_block_processing::errors::{ pub use state_processing::per_block_processing::errors::{

View File

@ -1,67 +1,42 @@
pub use prometheus::Error; pub use lighthouse_metrics::*;
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: Result<Histogram> = register_histogram!( pub static ref BLOCK_PROCESSING_DB_READ: Result<Histogram> = try_create_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"
); );
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", "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", "block_processing_successes",
"Count of blocks processed without error" "Count of blocks processed without error"
); );
pub static ref BLOCK_PROCESSING_TIMES: Result<Histogram> = 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 * 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", "block_production_requests",
"Count of all 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", "block_production_successes",
"Count of blocks sucessfully produced." "Count of blocks successfully produced."
); );
pub static ref BLOCK_PRODUCTION_TIMES: Result<Histogram> = 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 * 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", "operations_per_block_attestation",
"Number of attestations in a block" "Number of attestations in a block"
); );
@ -69,15 +44,15 @@ lazy_static! {
/* /*
* Attestation Processing * 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", "attestation_processing_requests",
"Count of all attestations submitted for processing" "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", "attestation_processing_successes",
"total_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", "attestation_processing_times",
"Full runtime of attestation processing" "Full runtime of attestation processing"
); );
@ -85,15 +60,15 @@ lazy_static! {
/* /*
* Attestation Production * 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", "attestation_production_requests",
"Count of all 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", "attestation_production_successes",
"Count of attestations processed without error" "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", "attestation_production_times",
"Full runtime of attestation production" "Full runtime of attestation production"
); );
@ -101,18 +76,18 @@ lazy_static! {
/* /*
* Fork Choice * 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", "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"
); );
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", "fork_choice_changed_head",
"Count of occasions fork choice has found a new 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", "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"
); );
pub static ref FORK_CHOICE_TIMES: Result<Histogram> = 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");
} }

View 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"

View 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);
}
}