diff --git a/backend/main.py b/backend/main.py index 0c10759..2844f3e 100644 --- a/backend/main.py +++ b/backend/main.py @@ -109,8 +109,12 @@ class CorrelationIDMiddleware: method=scope.get("method", ""), ) + _response_status: int = 0 + async def send_with_header(message: dict) -> None: + nonlocal _response_status if message["type"] == "http.response.start": + _response_status = message.get("status", 0) headers = list(message.get("headers", [])) headers.append((b"x-correlation-id", correlation_id.encode())) message = {**message, "headers": headers} @@ -118,9 +122,12 @@ class CorrelationIDMiddleware: await self.app(scope, receive, send_with_header) - # Bind duration after the response so downstream loggers can emit it. duration_ms = (time.perf_counter_ns() - start_ns) / 1_000_000 structlog.contextvars.bind_contextvars(duration_ms=round(duration_ms, 2)) + structlog.get_logger("docuvault.access").info( + "request_complete", + status_code=_response_status, + ) # ── Lifespan ──────────────────────────────────────────────────────────────────