From 9f485e4dbbbe96f798ca51b97bdba232a62c3071 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Sun, 17 Aug 2025 22:03:35 -0300 Subject: [PATCH] x-routstr-request-id header --- routstr/core/main.py | 1 + routstr/core/middleware.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/routstr/core/main.py b/routstr/core/main.py index caef08c5..d5fba2d8 100644 --- a/routstr/core/main.py +++ b/routstr/core/main.py @@ -95,6 +95,7 @@ app.add_middleware( allow_credentials=True, allow_methods=["*"], allow_headers=["*"], + expose_headers=["x-routstr-request-id"], ) # Add logging middleware diff --git a/routstr/core/middleware.py b/routstr/core/middleware.py index 3feada7e..4307cb9d 100644 --- a/routstr/core/middleware.py +++ b/routstr/core/middleware.py @@ -11,7 +11,7 @@ from .logging import get_logger logger = get_logger(__name__) # Context variable to store request ID across async context -request_id_context: ContextVar[str | None] = ContextVar("request_id", default=None) +request_id_context: ContextVar[str | None] = ContextVar("request_id") class LoggingMiddleware(BaseHTTPMiddleware): @@ -62,8 +62,8 @@ class LoggingMiddleware(BaseHTTPMiddleware): ) # Log at TRACE level for full body (security filter will redact sensitive data) - if request_body and hasattr(logger, "trace"): - logger.trace( + if request_body and hasattr(logger, "exception"): + logger.exception( "Request body", extra={ "request_id": request_id, @@ -95,7 +95,7 @@ class LoggingMiddleware(BaseHTTPMiddleware): }, ) if hasattr(response, "headers"): - response.headers["X-Routstr-Request-Id"] = request_id + response.headers["x-routstr-request-id"] = request_id return response