Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metrics for http requests #876

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion server/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ use warp::{
Filter,
};

use crate::{consts, error_util, metrics};
use crate::{
consts, error_util,
metrics::{self, HTTP_HANDLER_DURATION_HISTOGRAM_VEC},
};

#[derive(Debug, Snafu)]
pub enum Error {
Expand Down Expand Up @@ -193,6 +196,18 @@ impl<Q: QueryExecutor + 'static> Service<Q> {
.or(self.cpu_profile())
.or(self.server_config())
.or(self.stats())
.with(warp::log("http_requests"))
.with(warp::log::custom(|info| {
let path = info.path();
// Don't record /debug API
if path.starts_with("/debug") {
return;
}

HTTP_HANDLER_DURATION_HISTOGRAM_VEC
.with_label_values(&[path, info.status().as_str()])
.observe(info.elapsed().as_secs_f64())
}))
}

/// Expose `/prom/v1/read` and `/prom/v1/write` to serve Prometheus remote
Expand Down
14 changes: 13 additions & 1 deletion server/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

//! Metrics util for server.

use lazy_static::lazy_static;
use log::warn;
use prometheus::{Encoder, TextEncoder};
use prometheus::{exponential_buckets, register_histogram_vec, Encoder, HistogramVec, TextEncoder};

lazy_static! {
pub static ref HTTP_HANDLER_DURATION_HISTOGRAM_VEC: HistogramVec = register_histogram_vec!(
"http_handler_duration",
"Bucketed histogram of http server handler",
&["path", "code"],
// 0.01s, 0.02s, ... 163.84s
exponential_buckets(0.01, 2.0, 15).unwrap()
)
.unwrap();
}

/// Gather and dump prometheus to string.
pub fn dump() -> String {
Expand Down