1
0
mirror of https://github.com/actix/examples synced 2025-02-13 05:52:20 +01:00

47 lines
1.3 KiB
Rust
Raw Normal View History

use std::time::Instant;
use actix_web::HttpMessage as _;
use actix_web::{
body::MessageBody,
dev::{ServiceRequest, ServiceResponse},
http::header::{HeaderName, HeaderValue},
};
use actix_web_lab::middleware::Next;
use tracing_actix_web::RequestId;
use crate::metric_names::*;
pub(crate) async fn request_telemetry(
req: ServiceRequest,
next: Next<impl MessageBody>,
) -> actix_web::Result<ServiceResponse<impl MessageBody>> {
let now = Instant::now();
metrics::gauge!(GAUGE_HTTP_CONCURRENT_REQUESTS).increment(1);
let mut res = next.call(req).await?;
let req_id = res.request().extensions().get::<RequestId>().copied();
if let Some(req_id) = req_id {
res.headers_mut().insert(
HeaderName::from_static("request-id"),
// this unwrap never fails, since UUIDs are valid ASCII strings
HeaderValue::from_str(&req_id.to_string()).unwrap(),
);
};
2024-06-05 04:54:40 +01:00
let outcome = if res.status().is_success() || res.status().is_redirection() {
"success"
} else {
"failure"
};
let diff = now.elapsed();
2024-06-05 04:54:40 +01:00
metrics::histogram!(HISTOGRAM_HTTP_REQUEST_DURATION, "outcome" => outcome).record(diff);
metrics::gauge!(GAUGE_HTTP_CONCURRENT_REQUESTS).decrement(1);
Ok(res)
}