Begin metrics refactor

This commit is contained in:
Paul Hauner 2019-08-11 12:12:19 +10:00
parent 4020d13064
commit 48733917be
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
7 changed files with 46 additions and 1 deletions

View File

@ -17,6 +17,7 @@ sloggers = { version = "^0.3" }
slot_clock = { path = "../../eth2/utils/slot_clock" }
eth2_ssz = "0.1"
eth2_ssz_derive = "0.1"
lazy_static = "1.3.0"
state_processing = { path = "../../eth2/state_processing" }
tree_hash = "0.1"
types = { path = "../../eth2/types" }
@ -24,4 +25,3 @@ lmd_ghost = { path = "../../eth2/lmd_ghost" }
[dev-dependencies]
rand = "0.5.5"
lazy_static = "1.3.0"

View File

@ -2,6 +2,7 @@ use crate::checkpoint::CheckPoint;
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;
@ -848,6 +849,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
return Ok(BlockProcessingOutcome::BlockIsAlreadyKnown);
}
// 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();
// Load the blocks parent block from the database, returning invalid if that block is not
// found.
let parent_block: BeaconBlock<T::EthSpec> = match self.store.get(&block.parent_root)? {
@ -867,6 +872,8 @@ 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();
// Transition the parent state to the block slot.
let mut state: BeaconState<T::EthSpec> = parent_state;
for _ in state.slot.as_u64()..block.slot.as_u64() {

View File

@ -1,3 +1,8 @@
#[macro_use]
extern crate prometheus;
#[macro_use]
extern crate lazy_static;
mod beacon_chain;
mod checkpoint;
mod errors;
@ -13,6 +18,7 @@ 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::{

View File

@ -1,6 +1,18 @@
pub use prometheus::Error;
use prometheus::{Histogram, HistogramOpts, IntCounter, Opts, Registry};
lazy_static! {
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 fn gather_metrics() -> Vec<prometheus::proto::MetricFamily> {
prometheus::gather()
}
pub struct Metrics {
pub block_processing_requests: IntCounter,
pub block_processing_successes: IntCounter,

View File

@ -18,6 +18,7 @@ state_processing = { path = "../../eth2/state_processing" }
types = { path = "../../eth2/types" }
clap = "2.32.0"
http = "^0.1.17"
prometheus = { version = "^0.6", features = ["process"] }
hyper = "0.12.32"
futures = "0.1"
exit-future = "0.1.3"

View File

@ -3,6 +3,7 @@ extern crate hyper;
mod beacon;
mod config;
mod helpers;
mod metrics;
mod node;
mod url_query;
@ -103,6 +104,7 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
let result = match (req.method(), path.as_ref()) {
(&Method::GET, "/beacon/state") => beacon::get_state::<T>(req),
(&Method::GET, "/beacon/state_root") => beacon::get_state_root::<T>(req),
(&Method::GET, "/metrics") => metrics::get_prometheus(req),
(&Method::GET, "/node/version") => node::get_version(req),
(&Method::GET, "/node/genesis_time") => node::get_genesis_time::<T>(req),
_ => Err(ApiError::MethodNotAllowed(path.clone())),

View File

@ -0,0 +1,17 @@
use crate::{success_response, ApiError, ApiResult};
use hyper::{Body, Request};
use prometheus::{Encoder, TextEncoder};
/// Returns the full set of Prometheus metrics for the Beacon Node application.
pub fn get_prometheus(_req: Request<Body>) -> ApiResult {
let mut buffer = vec![];
let encoder = TextEncoder::new();
encoder
.encode(&beacon_chain::gather_metrics(), &mut buffer)
.unwrap();
String::from_utf8(buffer)
.map(|string| success_response(Body::from(string)))
.map_err(|e| ApiError::ServerError(format!("Failed to encode prometheus info: {:?}", e)))
}