Add metrics to HTTP server

This commit is contained in:
Paul Hauner 2019-08-12 14:30:46 +10:00
parent cac0e5c832
commit af334b2cf0
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 36 additions and 4 deletions

View File

@ -24,3 +24,5 @@ futures = "0.1"
exit-future = "0.1.3" exit-future = "0.1.3"
tokio = "0.1.17" tokio = "0.1.17"
url = "2.0" url = "2.0"
lazy_static = "1.3.0"
lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }

View File

@ -1,5 +1,6 @@
extern crate futures; #[macro_use]
extern crate hyper; extern crate lazy_static;
mod beacon; mod beacon;
mod config; mod config;
mod helpers; mod helpers;
@ -100,6 +101,9 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
// Create a simple handler for the router, inject our stateful objects into the request. // Create a simple handler for the router, inject our stateful objects into the request.
service_fn_ok(move |mut req| { service_fn_ok(move |mut req| {
metrics::inc_counter(&metrics::REQUEST_COUNT);
let timer = metrics::start_timer(&metrics::REQUEST_RESPONSE_TIME);
req.extensions_mut().insert::<slog::Logger>(log.clone()); req.extensions_mut().insert::<slog::Logger>(log.clone());
req.extensions_mut() req.extensions_mut()
.insert::<Arc<BeaconChain<T>>>(beacon_chain.clone()); .insert::<Arc<BeaconChain<T>>>(beacon_chain.clone());
@ -117,9 +121,10 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
_ => Err(ApiError::MethodNotAllowed(path.clone())), _ => Err(ApiError::MethodNotAllowed(path.clone())),
}; };
match result { let response = match result {
// Return the `hyper::Response`. // Return the `hyper::Response`.
Ok(response) => { Ok(response) => {
metrics::inc_counter(&metrics::SUCCESS_COUNT);
slog::debug!(log, "Request successful: {:?}", path); slog::debug!(log, "Request successful: {:?}", path);
response response
} }
@ -128,7 +133,11 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
slog::debug!(log, "Request failure: {:?}", path); slog::debug!(log, "Request failure: {:?}", path);
e.into() e.into()
} }
} };
metrics::stop_timer(timer);
response
}) })
}; };

View File

@ -4,7 +4,28 @@ use hyper::{Body, Request};
use prometheus::{Encoder, TextEncoder}; use prometheus::{Encoder, TextEncoder};
use std::sync::Arc; use std::sync::Arc;
pub use lighthouse_metrics::*;
lazy_static! {
pub static ref REQUEST_RESPONSE_TIME: Result<Histogram> = try_create_histogram(
"http_server_request_response_time",
"Time taken to build a response to a HTTP request"
);
pub static ref REQUEST_COUNT: Result<IntCounter> = try_create_int_counter(
"http_server_request_count",
"Total count of HTTP requests received"
);
pub static ref SUCCESS_COUNT: Result<IntCounter> = try_create_int_counter(
"http_server_success_count",
"Total count of HTTP 200 responses sent"
);
}
/// Returns the full set of Prometheus metrics for the Beacon Node application. /// Returns the full set of Prometheus metrics for the Beacon Node application.
///
/// # Note
///
/// This is a HTTP handler method.
pub fn get_prometheus<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult { pub fn get_prometheus<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let mut buffer = vec![]; let mut buffer = vec![];
let encoder = TextEncoder::new(); let encoder = TextEncoder::new();