From ea1ed6fd7053a354e8559fbdc72aa91158be28b2 Mon Sep 17 00:00:00 2001 From: Michael Pfaff Date: Wed, 20 Jan 2021 22:56:09 -0500 Subject: [PATCH] Fix concurrent request spans interfering --- src/lib.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6dfcf17..37b78c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,25 +7,27 @@ use nrid::Nrid; use std::time::Instant; use tide::Next; use tide::Request; +use tracing::Instrument; use tracing::Level; -/// Middleware for the [`req_access_token!`] macro. pub fn log_middleware<'a, State: 'static + Clone + Send + Sync>( req: Request, next: Next<'a, State>, ) -> Pin + Send + 'a>> { - Box::pin(async { + Box::pin(async move { let now = Instant::now(); let id = Nrid::now(); - let uri = req.url(); + let uri = req.url().clone(); // doesn't take forwarding into account and includes useless socket port information // let ip = req.peer_addr().unwrap_or("unknown").to_string(); let ip = req.remote().unwrap_or("unknown").to_string(); let span = span!(Level::ERROR, "Request", id = %id); - let _enter = span.enter(); - debug!(req.method = %req.method(), req.uri.path = uri.path(), req.uri.query = uri.query().unwrap_or(""), client_ip = %ip, received = ?now); - let res = next.run(req).await; - debug!(status = %res.status(), elapsed = ?now.elapsed(), "Response"); + let res = async { + debug!(req.method = %req.method(), req.uri.path = uri.path(), req.uri.query = uri.query().unwrap_or(""), client_ip = %ip, received = ?now); + let res = next.run(req).await; + debug!(status = %res.status(), elapsed = ?now.elapsed(), "Response"); + res + }.instrument(span).await; Ok(res) }) }