2019-09-13 08:58:08 +00:00
|
|
|
use crate::response_builder::ResponseBuilder;
|
2019-11-25 04:48:24 +00:00
|
|
|
use crate::{ApiError, ApiResult};
|
|
|
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
2019-08-19 11:02:34 +00:00
|
|
|
use hyper::{Body, Request};
|
2019-11-25 04:48:24 +00:00
|
|
|
use lighthouse_metrics::{Encoder, TextEncoder};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
2019-08-19 11:02:34 +00:00
|
|
|
|
|
|
|
pub use lighthouse_metrics::*;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref REQUEST_RESPONSE_TIME: Result<Histogram> = try_create_histogram(
|
|
|
|
"http_server_request_duration_seconds",
|
|
|
|
"Time taken to build a response to a HTTP request"
|
|
|
|
);
|
|
|
|
pub static ref REQUEST_COUNT: Result<IntCounter> = try_create_int_counter(
|
|
|
|
"http_server_request_total",
|
|
|
|
"Total count of HTTP requests received"
|
|
|
|
);
|
|
|
|
pub static ref SUCCESS_COUNT: Result<IntCounter> = try_create_int_counter(
|
|
|
|
"http_server_success_total",
|
|
|
|
"Total count of HTTP 200 responses sent"
|
|
|
|
);
|
2020-03-04 06:10:22 +00:00
|
|
|
pub static ref VALIDATOR_GET_BLOCK_REQUEST_RESPONSE_TIME: Result<Histogram> =
|
|
|
|
try_create_histogram(
|
|
|
|
"http_server_validator_block_get_request_duration_seconds",
|
|
|
|
"Time taken to respond to GET /validator/block"
|
|
|
|
);
|
|
|
|
pub static ref VALIDATOR_GET_ATTESTATION_REQUEST_RESPONSE_TIME: Result<Histogram> =
|
|
|
|
try_create_histogram(
|
|
|
|
"http_server_validator_attestation_get_request_duration_seconds",
|
|
|
|
"Time taken to respond to GET /validator/attestation"
|
|
|
|
);
|
|
|
|
pub static ref VALIDATOR_GET_DUTIES_REQUEST_RESPONSE_TIME: Result<Histogram> =
|
|
|
|
try_create_histogram(
|
|
|
|
"http_server_validator_duties_get_request_duration_seconds",
|
|
|
|
"Time taken to respond to GET /validator/duties"
|
|
|
|
);
|
2019-08-19 11:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the full set of Prometheus metrics for the Beacon Node application.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This is a HTTP handler method.
|
2019-11-25 04:48:24 +00:00
|
|
|
pub fn get_prometheus<T: BeaconChainTypes>(
|
|
|
|
req: Request<Body>,
|
|
|
|
beacon_chain: Arc<BeaconChain<T>>,
|
|
|
|
db_path: PathBuf,
|
2019-12-13 02:30:58 +00:00
|
|
|
freezer_db_path: PathBuf,
|
2019-11-25 04:48:24 +00:00
|
|
|
) -> ApiResult {
|
2019-08-19 11:02:34 +00:00
|
|
|
let mut buffer = vec![];
|
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
|
|
|
|
// There are two categories of metrics:
|
|
|
|
//
|
|
|
|
// - Dynamically updated: things like histograms and event counters that are updated on the
|
|
|
|
// fly.
|
|
|
|
// - Statically updated: things which are only updated at the time of the scrape (used where we
|
|
|
|
// can avoid cluttering up code with metrics calls).
|
|
|
|
//
|
|
|
|
// The `lighthouse_metrics` crate has a `DEFAULT_REGISTRY` global singleton (via `lazy_static`)
|
|
|
|
// which keeps the state of all the metrics. Dynamically updated things will already be
|
|
|
|
// up-to-date in the registry (because they update themselves) however statically updated
|
|
|
|
// things need to be "scraped".
|
|
|
|
//
|
|
|
|
// We proceed by, first updating all the static metrics using `scrape_for_metrics(..)`. Then,
|
|
|
|
// using `lighthouse_metrics::gather(..)` to collect the global `DEFAULT_REGISTRY` metrics into
|
|
|
|
// a string that can be returned via HTTP.
|
|
|
|
|
|
|
|
slot_clock::scrape_for_metrics::<T::EthSpec, T::SlotClock>(&beacon_chain.slot_clock);
|
2019-12-13 02:30:58 +00:00
|
|
|
store::scrape_for_metrics(&db_path, &freezer_db_path);
|
2019-08-19 11:02:34 +00:00
|
|
|
beacon_chain::scrape_for_metrics(&beacon_chain);
|
|
|
|
|
|
|
|
encoder
|
|
|
|
.encode(&lighthouse_metrics::gather(), &mut buffer)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
String::from_utf8(buffer)
|
2019-09-13 10:42:56 +00:00
|
|
|
.map(|string| ResponseBuilder::new(&req)?.body_text(string))
|
2019-09-13 09:38:40 +00:00
|
|
|
.map_err(|e| ApiError::ServerError(format!("Failed to encode prometheus info: {:?}", e)))?
|
2019-08-19 11:02:34 +00:00
|
|
|
}
|