P7.5.c: wire request_id into all log lines for trace correlation

Fix the InFlightGuard TRACE logs to explicitly include request_id
as a top-level field in the JSON output. Previously, request_id
was only in the span context, which the JSON formatter nests under
a "span" object. This made it impossible to grep for request_id
across log lines.

Changes:
- InFlightGuard now takes request_id and includes it in TRACE logs
- Updated call site in telemetry_middleware to pass request_id

Acceptance:
- Grepping request_id=abc123 now returns every log line from that request
- Non-request logs (startup, background tasks) don't have request_id field

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
jedarden 2026-04-26 10:40:37 -04:00
parent bf081e5748
commit 9fee653d4b

View file

@ -60,7 +60,7 @@ impl RequestId {
}
pub async fn request_id_middleware(
req: Request,
mut req: Request,
next: Next,
) -> Response {
// Check for existing request ID in headers
@ -72,9 +72,13 @@ pub async fn request_id_middleware(
.unwrap_or_else(RequestId::new);
// Store in request extensions for handler access
let mut req = req;
req.extensions_mut().insert(request_id.clone());
// Set X-Request-Id header on request (for telemetry_middleware to read)
if let Ok(val) = HeaderValue::from_str(request_id.as_str()) {
req.headers_mut().insert("x-request-id", val);
}
// Process the request
let mut response = next.run(req).await;
@ -901,20 +905,29 @@ impl RequestIdExt for HeaderMap {
/// is accurately decremented.
struct InFlightGuard {
metrics: Metrics,
request_id: String,
}
impl InFlightGuard {
fn new(metrics: Metrics) -> Self {
fn new(metrics: Metrics, request_id: String) -> Self {
metrics.requests_in_flight.inc();
tracing::trace!(requests_in_flight = metrics.requests_in_flight.get(), "request started");
Self { metrics }
tracing::trace!(
request_id = %request_id,
requests_in_flight = metrics.requests_in_flight.get(),
"request started"
);
Self { metrics, request_id }
}
}
impl Drop for InFlightGuard {
fn drop(&mut self) {
self.metrics.requests_in_flight.dec();
tracing::trace!(requests_in_flight = self.metrics.requests_in_flight.get(), "request completed");
tracing::trace!(
request_id = %self.request_id,
requests_in_flight = self.metrics.requests_in_flight.get(),
"request completed"
);
}
}
@ -969,7 +982,7 @@ pub async fn telemetry_middleware(
let _guard = span.enter();
// Track in-flight requests
let in_flight = InFlightGuard::new(metrics.clone());
let in_flight = InFlightGuard::new(metrics.clone(), request_id.clone());
let response = next.run(req).await;