Add a metrics counter measuring in-flight requests

This commit is contained in:
Armaël Guéneau 2024-04-25 22:03:07 +02:00
parent e9f42cac57
commit 60e8aacfd7

View file

@ -41,9 +41,21 @@ pub struct HttpsConfig {
struct HttpsMetrics { struct HttpsMetrics {
requests_received: metrics::Counter<u64>, requests_received: metrics::Counter<u64>,
requests_served: metrics::Counter<u64>, requests_served: metrics::Counter<u64>,
requests_in_flight: metrics::UpDownCounter<i64>,
request_proxy_duration: metrics::Histogram<f64>, request_proxy_duration: metrics::Histogram<f64>,
} }
struct InFlightGuard<'a> {
metrics: Arc<HttpsMetrics>,
tags: &'a [KeyValue],
}
impl<'a> Drop for InFlightGuard<'a> {
fn drop(&mut self) {
self.metrics.requests_in_flight.add(-1, self.tags)
}
}
pub async fn serve_https( pub async fn serve_https(
config: HttpsConfig, config: HttpsConfig,
cert_store: Arc<CertStore>, cert_store: Arc<CertStore>,
@ -62,6 +74,10 @@ pub async fn serve_https(
.u64_counter("https_requests_served") .u64_counter("https_requests_served")
.with_description("Total number of requests served over HTTPS") .with_description("Total number of requests served over HTTPS")
.init(), .init(),
requests_in_flight: meter
.i64_up_down_counter("https_requests_in_flight")
.with_description("Current number of requests handled over HTTPS")
.init(),
request_proxy_duration: meter request_proxy_duration: meter
.f64_histogram("https_request_proxy_duration") .f64_histogram("https_request_proxy_duration")
.with_description("Duration between time when request was received, and time when backend returned status code and headers") .with_description("Duration between time when request was received, and time when backend returned status code and headers")
@ -163,8 +179,8 @@ async fn handle_request(
) -> Result<Response<Body>, Infallible> { ) -> Result<Response<Body>, Infallible> {
let method_tag = KeyValue::new("method", req.method().to_string()); let method_tag = KeyValue::new("method", req.method().to_string());
// The host tag is only included in the requests_received metric, // The host tag is only included in the requests_received and requests_in_flight
// as for other metrics it can easily lead to cardinality explosions. // metrics, as for other metrics it can easily lead to cardinality explosions.
let host_tag = KeyValue::new( let host_tag = KeyValue::new(
"host", "host",
req.uri() req.uri()
@ -178,9 +194,13 @@ async fn handle_request(
.unwrap_or_default(), .unwrap_or_default(),
); );
metrics let tags = [host_tag, method_tag.clone()];
.requests_received metrics.requests_received.add(1, &tags);
.add(1, &[host_tag, method_tag.clone()]); metrics.requests_in_flight.add(1, &tags);
let _guard = InFlightGuard {
metrics: Arc::clone(&metrics),
tags: &tags,
};
let mut tags = vec![method_tag]; let mut tags = vec![method_tag];
let resp = select_target_and_proxy( let resp = select_target_and_proxy(