Add lighthouse_metrics gather fn

This commit is contained in:
Paul Hauner 2019-08-12 18:19:50 +10:00
parent d7c546844c
commit 7165598b7f
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
2 changed files with 13 additions and 7 deletions

View File

@ -46,20 +46,22 @@ pub fn get_prometheus<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
// - 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 `prometheus` 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".
// 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 `prometheus::gather(..)` to collect the global `DEFAULT_REGISTRY` metrics into a
// string that can be returned via HTTP.
// 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);
store::scrape_for_metrics(&db_path);
beacon_chain::scrape_for_metrics(&beacon_chain);
encoder.encode(&prometheus::gather(), &mut buffer).unwrap();
encoder
.encode(&lighthouse_metrics::gather(), &mut buffer)
.unwrap();
String::from_utf8(buffer)
.map(|string| success_response(Body::from(string)))

View File

@ -2,6 +2,10 @@ use prometheus::{HistogramOpts, HistogramTimer, Opts};
pub use prometheus::{Histogram, IntCounter, IntGauge, Result};
pub fn gather() -> Vec<prometheus::proto::MetricFamily> {
prometheus::gather()
}
pub fn try_create_int_counter(name: &str, help: &str) -> Result<IntCounter> {
let opts = Opts::new(name, help);
let counter = IntCounter::with_opts(opts)?;