Skip to content

Commit

Permalink
Added latency metrics for REST endpoint.
Browse files Browse the repository at this point in the history
This PR also makes it possible to set specific buckets for
histograms.

Cloes #4932
  • Loading branch information
fulmicoton committed May 13, 2024
1 parent 770d8a7 commit 141bc73
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions quickwit/quickwit-cli/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Default for CliMetrics {
"cli",
&[],
[],
quickwit_common::metrics::exponential_buckets(5.0, 5.0, 5).unwrap(),
),
}
}
Expand Down
16 changes: 10 additions & 6 deletions quickwit/quickwit-common/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ use std::collections::{BTreeMap, HashMap};
use std::sync::OnceLock;

use once_cell::sync::Lazy;
use prometheus::{Encoder, HistogramOpts, Opts, TextEncoder};
pub use prometheus::{
Histogram, HistogramTimer, HistogramVec as PrometheusHistogramVec, IntCounter,
IntCounterVec as PrometheusIntCounterVec, IntGauge, IntGaugeVec as PrometheusIntGaugeVec,
exponential_buckets, Histogram, HistogramTimer, HistogramVec as PrometheusHistogramVec,
IntCounter, IntCounterVec as PrometheusIntCounterVec, IntGauge,
IntGaugeVec as PrometheusIntGaugeVec,
};
use prometheus::{Encoder, HistogramOpts, Opts, TextEncoder};

#[derive(Clone)]
pub struct HistogramVec<const N: usize> {
Expand Down Expand Up @@ -146,10 +147,11 @@ pub fn new_gauge_vec<const N: usize>(
IntGaugeVec { underlying }
}

pub fn new_histogram(name: &str, help: &str, subsystem: &str) -> Histogram {
pub fn new_histogram(name: &str, help: &str, subsystem: &str, buckets: Vec<f64>) -> Histogram {
let histogram_opts = HistogramOpts::new(name, help)
.namespace("quickwit")
.subsystem(subsystem);
.subsystem(subsystem)
.buckets(buckets);
let histogram = Histogram::with_opts(histogram_opts).expect("failed to create histogram");
prometheus::register(Box::new(histogram.clone())).expect("failed to register histogram");
histogram
Expand All @@ -161,6 +163,7 @@ pub fn new_histogram_vec<const N: usize>(
subsystem: &str,
const_labels: &[(&str, &str)],
label_names: [&str; N],
buckets: Vec<f64>,
) -> HistogramVec<N> {
let owned_const_labels: HashMap<String, String> = const_labels
.iter()
Expand All @@ -169,7 +172,8 @@ pub fn new_histogram_vec<const N: usize>(
let histogram_opts = HistogramOpts::new(name, help)
.namespace("quickwit")
.subsystem(subsystem)
.const_labels(owned_const_labels);
.const_labels(owned_const_labels)
.buckets(buckets);
let underlying = PrometheusHistogramVec::new(histogram_opts, &label_names)
.expect("failed to create histogram vec");

Expand Down
2 changes: 2 additions & 0 deletions quickwit/quickwit-common/src/tower/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::time::Instant;

use futures::{ready, Future};
use pin_project::{pin_project, pinned_drop};
use prometheus::exponential_buckets;
use tower::{Layer, Service};

use crate::metrics::{
Expand Down Expand Up @@ -103,6 +104,7 @@ impl GrpcMetricsLayer {
subsystem,
&[("kind", kind)],
["rpc", "status"],
exponential_buckets(0.001, 2.0, 12).unwrap(),
),
}
}
Expand Down
5 changes: 3 additions & 2 deletions quickwit/quickwit-ingest/src/ingest_v2/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use mrecordlog::ResourceUsage;
use once_cell::sync::Lazy;
use quickwit_common::metrics::{
new_counter_vec, new_gauge, new_gauge_vec, new_histogram_vec, HistogramVec, IntCounterVec,
IntGauge, IntGaugeVec,
exponential_buckets, new_counter_vec, new_gauge, new_gauge_vec, new_histogram_vec,
HistogramVec, IntCounterVec, IntGauge, IntGaugeVec,
};

pub(super) struct IngestV2Metrics {
Expand Down Expand Up @@ -69,6 +69,7 @@ impl Default for IngestV2Metrics {
"ingest",
&[],
["operation", "type"],
exponential_buckets(0.001, 2.0, 12).unwrap(),
),
wal_disk_used_bytes: new_gauge(
"wal_disk_used_bytes",
Expand Down
3 changes: 2 additions & 1 deletion quickwit/quickwit-jaeger/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use once_cell::sync::Lazy;
use quickwit_common::metrics::{new_counter_vec, new_histogram_vec, HistogramVec, IntCounterVec};
use quickwit_common::metrics::{exponential_buckets, new_counter_vec, new_histogram_vec, HistogramVec, IntCounterVec};

pub struct JaegerServiceMetrics {
pub requests_total: IntCounterVec<2>,
Expand Down Expand Up @@ -52,6 +52,7 @@ impl Default for JaegerServiceMetrics {
"jaeger",
&[],
["operation", "index", "error"],
exponential_buckets(0.02, 2.0, 8).unwrap(),
),
fetched_traces_total: new_counter_vec(
"fetched_traces_total",
Expand Down
5 changes: 4 additions & 1 deletion quickwit/quickwit-opentelemetry/src/otlp/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use once_cell::sync::Lazy;
use quickwit_common::metrics::{new_counter_vec, new_histogram_vec, HistogramVec, IntCounterVec};
use quickwit_common::metrics::{
exponential_buckets, new_counter_vec, new_histogram_vec, HistogramVec, IntCounterVec,
};

pub struct OtlpServiceMetrics {
pub requests_total: IntCounterVec<4>,
Expand Down Expand Up @@ -52,6 +54,7 @@ impl Default for OtlpServiceMetrics {
"otlp",
&[],
["service", "index", "transport", "format", "error"],
exponential_buckets(0.02, 2.0, 8).unwrap(),
),
ingested_log_records_total: new_counter_vec(
"ingested_log_records_total",
Expand Down
4 changes: 4 additions & 0 deletions quickwit/quickwit-search/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl Default for SearchMetrics {
"Number of seconds required to run a leaf search over a single split. The timer \
starts after the semaphore is obtained.",
"search",
vec![
0.002, 0.005, 0.01, 0.015, 0.030, 0.040, 0.050, 0.075, 0.100, 0.200, 0.500,
1.000,
],
),
}
}
Expand Down
11 changes: 10 additions & 1 deletion quickwit/quickwit-serve/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use once_cell::sync::Lazy;
use quickwit_common::metrics::{new_counter_vec, IntCounterVec};
use quickwit_common::metrics::{new_counter_vec, new_histogram_vec, HistogramVec, IntCounterVec};

pub struct RestMetrics {
pub http_requests_total: IntCounterVec<2>,
pub request_duration_seconds: HistogramVec<2>,
}

impl Default for RestMetrics {
Expand All @@ -34,6 +35,14 @@ impl Default for RestMetrics {
&[],
["method", "status_code"],
),
request_duration_seconds: new_histogram_vec(
"request_duration_seconds",
"Response time in millisecs",
"",
&[],
["method", "status_code"],
quickwit_common::metrics::exponential_buckets(0.002, 2.0, 11).unwrap(),
),
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions quickwit/quickwit-serve/src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use tower_http::compression::predicate::{DefaultPredicate, Predicate, SizeAbove}
use tower_http::compression::CompressionLayer;
use tower_http::cors::CorsLayer;
use tracing::{error, info};
use warp::filters::log::Info;
use warp::{redirect, Filter, Rejection, Reply};

use crate::cluster_api::cluster_handler;
Expand Down Expand Up @@ -71,10 +72,17 @@ pub(crate) async fn start_rest_server(
readiness_trigger: BoxFutureInfaillible<()>,
shutdown_signal: BoxFutureInfaillible<()>,
) -> anyhow::Result<()> {
let request_counter = warp::log::custom(|info| {
let request_counter = warp::log::custom(|info: Info| {
let elapsed = info.elapsed();
let status = info.status();
let label_values: [&str; 2] = [info.method().as_str(), status.as_str()];
crate::SERVE_METRICS
.request_duration_seconds
.with_label_values(label_values)
.observe(elapsed.as_secs_f64());
crate::SERVE_METRICS
.http_requests_total
.with_label_values([info.method().as_str(), info.status().as_str()])
.with_label_values(label_values)
.inc();
});
// Docs routes
Expand Down

0 comments on commit 141bc73

Please sign in to comment.