all working
This commit is contained in:
@@ -4,6 +4,7 @@ Request/Response Logging Middleware
|
||||
import time
|
||||
import json
|
||||
from typing import Callable
|
||||
from uuid import uuid4
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.responses import StreamingResponse
|
||||
@@ -21,10 +22,19 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
self.log_responses = log_responses
|
||||
|
||||
async def dispatch(self, request: Request, call_next: Callable) -> Response:
|
||||
# Skip logging for static files and health checks
|
||||
# Correlation ID: use incoming header or generate a new one
|
||||
correlation_id = request.headers.get("x-correlation-id") or request.headers.get("x-request-id") or str(uuid4())
|
||||
request.state.correlation_id = correlation_id
|
||||
|
||||
# Skip logging for static files and health checks (still attach correlation id)
|
||||
skip_paths = ["/static/", "/uploads/", "/health", "/favicon.ico"]
|
||||
if any(request.url.path.startswith(path) for path in skip_paths):
|
||||
return await call_next(request)
|
||||
response = await call_next(request)
|
||||
try:
|
||||
response.headers["X-Correlation-ID"] = correlation_id
|
||||
except Exception:
|
||||
pass
|
||||
return response
|
||||
|
||||
# Record start time
|
||||
start_time = time.time()
|
||||
@@ -41,7 +51,8 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
path=request.url.path,
|
||||
query_params=str(request.query_params) if request.query_params else None,
|
||||
client_ip=client_ip,
|
||||
user_agent=user_agent
|
||||
user_agent=user_agent,
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
# Process request
|
||||
@@ -56,7 +67,8 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
path=request.url.path,
|
||||
duration_ms=duration_ms,
|
||||
error=str(e),
|
||||
client_ip=client_ip
|
||||
client_ip=client_ip,
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
raise
|
||||
|
||||
@@ -74,7 +86,8 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
path=request.url.path,
|
||||
status_code=response.status_code,
|
||||
duration_ms=duration_ms,
|
||||
user_id=user_id
|
||||
user_id=user_id,
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
# Log response details if enabled
|
||||
@@ -84,7 +97,8 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
status_code=response.status_code,
|
||||
headers=dict(response.headers),
|
||||
size_bytes=response.headers.get("content-length"),
|
||||
content_type=response.headers.get("content-type")
|
||||
content_type=response.headers.get("content-type"),
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
# Log slow requests as warnings
|
||||
@@ -94,7 +108,8 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
method=request.method,
|
||||
path=request.url.path,
|
||||
duration_ms=duration_ms,
|
||||
status_code=response.status_code
|
||||
status_code=response.status_code,
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
# Log authentication-related requests to auth log
|
||||
@@ -106,9 +121,16 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
status_code=response.status_code,
|
||||
duration_ms=duration_ms,
|
||||
client_ip=client_ip,
|
||||
user_agent=user_agent
|
||||
user_agent=user_agent,
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
|
||||
# Attach correlation id header to all responses
|
||||
try:
|
||||
response.headers["X-Correlation-ID"] = correlation_id
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return response
|
||||
|
||||
def get_client_ip(self, request: Request) -> str:
|
||||
|
||||
Reference in New Issue
Block a user