fix(middleware): add support for logging request-id sent in request (#3225)

This commit is contained in:
Nishant Joshi
2024-01-03 14:53:10 +05:30
committed by GitHub
parent e06ba148b6
commit 0f72b5527a

View File

@ -43,16 +43,21 @@ where
actix_web::dev::forward_ready!(service);
fn call(&self, req: actix_web::dev::ServiceRequest) -> Self::Future {
let old_x_request_id = req.headers().get("x-request-id").cloned();
let mut req = req;
let request_id_fut = req.extract::<router_env::tracing_actix_web::RequestId>();
let response_fut = self.service.call(req);
Box::pin(async move {
let request_id = request_id_fut.await?;
let request_id = request_id.as_hyphenated().to_string();
if let Some(upstream_request_id) = old_x_request_id {
router_env::logger::info!(?request_id, ?upstream_request_id);
}
let mut response = response_fut.await?;
response.headers_mut().append(
http::header::HeaderName::from_static("x-request-id"),
http::HeaderValue::from_str(&request_id.as_hyphenated().to_string())?,
http::HeaderValue::from_str(&request_id)?,
);
Ok(response)