Skip to content

Commit

Permalink
Add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 15, 2020
1 parent 6a14342 commit 61f9d6c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions beacon_node/http_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ eth1 = { path = "../eth1" }
fork_choice = { path = "../../consensus/fork_choice" }
state_processing = { path = "../../consensus/state_processing" }
lighthouse_version = { path = "../../common/lighthouse_version" }
lighthouse_metrics = { path = "../../common/lighthouse_metrics" }
lazy_static = "1.4.0"

[dev-dependencies]
store = { path = "../store" }
Expand Down
58 changes: 57 additions & 1 deletion beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod beacon_proposer_cache;
mod block_id;
mod metrics;
mod reject;
mod state_id;

Expand Down Expand Up @@ -109,6 +110,60 @@ pub fn slog_logging(
})
}

pub fn prometheus_metrics() -> warp::filters::log::Log<impl Fn(warp::filters::log::Info) + Clone> {
warp::log::custom(move |info| {
// Here we restrict the `info.path()` value to some predefined values. Without this, we end
// up with a new metric type each time someone includes something unique in the path (e.g.,
// a block hash).
let path = {
let equals = |s: &'static str| -> Option<&'static str> {
if info.path() == &format!("{}/{}/{}", API_PREFIX, API_VERSION, s) {
Some(s)
} else {
None
}
};

let starts_with = |s: &'static str| -> Option<&'static str> {
if info
.path()
.starts_with(&format!("{}/{}/{}", API_PREFIX, API_VERSION, s))
{
Some(s)
} else {
None
}
};

equals("beacon/blocks")
.or(starts_with("validator/duties/attester"))
.or(starts_with("validator/duties/proposer"))
.or(starts_with("validator/attestation_data"))
.or(starts_with("validator/aggregate_attestation"))
.or(starts_with("validator/aggregate_and_proofs"))
.or(starts_with("validator/beacon_committee_subscriptions"))
.or(starts_with("beacon/"))
.or(starts_with("config/"))
.or(starts_with("debug/"))
.or(starts_with("events/"))
.or(starts_with("node/"))
.or(starts_with("validator/"))
.unwrap_or("other")
};

metrics::inc_counter_vec(&metrics::HTTP_API_PATHS_TOTAL, &[path]);
metrics::inc_counter_vec(
&metrics::HTTP_API_STATUS_CODES_TOTAL,
&[&info.status().to_string()],
);
metrics::observe_timer_vec(
&metrics::HTTP_API_PATHS_TIMES_TOTAL,
&[info.path()],
info.elapsed(),
);
})
}

pub fn serve<T: BeaconChainTypes>(
ctx: Arc<Context<T>>,
shutdown: impl Future<Output = ()> + Send + Sync + 'static,
Expand Down Expand Up @@ -1291,7 +1346,8 @@ pub fn serve<T: BeaconChainTypes>(
))
.boxed()
.recover(crate::reject::handle_rejection)
.with(slog_logging(log.clone()));
.with(slog_logging(log.clone()))
.with(prometheus_metrics());

let (listening_socket, server) = warp::serve(routes).try_bind_with_graceful_shutdown(
SocketAddrV4::new(config.listen_addr, config.listen_port),
Expand Down
19 changes: 19 additions & 0 deletions beacon_node/http_api/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub use lighthouse_metrics::*;

lazy_static::lazy_static! {
pub static ref HTTP_API_PATHS_TOTAL: Result<IntCounterVec> = try_create_int_counter_vec(
"http_api_paths_total",
"Count of HTTP requests received",
&["path"]
);
pub static ref HTTP_API_STATUS_CODES_TOTAL: Result<IntCounterVec> = try_create_int_counter_vec(
"http_api_status_codes_total",
"Count of HTTP status codes returned",
&["status"]
);
pub static ref HTTP_API_PATHS_TIMES_TOTAL: Result<HistogramVec> = try_create_histogram_vec(
"http_api_paths_times_total",
"Duration to process HTTP requests per path",
&["path"]
);
}
14 changes: 14 additions & 0 deletions common/lighthouse_metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
//! ```
use prometheus::{HistogramOpts, HistogramTimer, Opts};
use std::time::Duration;

pub use prometheus::{
Encoder, Gauge, GaugeVec, Histogram, HistogramVec, IntCounter, IntCounterVec, IntGauge,
Expand Down Expand Up @@ -221,6 +222,19 @@ pub fn start_timer(histogram: &Result<Histogram>) -> Option<HistogramTimer> {
}
}

/// Starts a timer on `vec` with the given `name`.
pub fn observe_timer_vec(vec: &Result<HistogramVec>, name: &[&str], duration: Duration) {
// This conversion was taken from here:
//
// https://docs.rs/prometheus/0.5.0/src/prometheus/histogram.rs.html#550-555
let nanos = f64::from(duration.subsec_nanos()) / 1e9;
let secs = duration.as_secs() as f64 + nanos;

if let Some(h) = get_histogram(vec, name) {
h.observe(secs)
}
}

/// Stops a timer created with `start_timer(..)`.
pub fn stop_timer(timer: Option<HistogramTimer>) {
if let Some(t) = timer {
Expand Down

0 comments on commit 61f9d6c

Please sign in to comment.