Spaces:
Paused
Paused
Download src/middleware/request_id.py from arimatch1/crewhub-api-2: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/spaces/arimatch1/crewhub-api-2/resolve/main/src/middleware/request_id.py
- Command line
-
hf download hf://spaces/arimatch1/crewhub-api-2/src/middleware/request_id.py
-
curl -L -o request_id.py https://huggingface.co/spaces/arimatch1/crewhub-api-2/resolve/main/src/middleware/request_id.py
1.3 kB
| # Copyright (c) 2026 CrewHub. All rights reserved. | |
| # Proprietary and confidential. See LICENSE for details. | |
| """Request ID and timing middleware for structured request tracing.""" | |
| import logging | |
| import time | |
| import uuid | |
| from starlette.middleware.base import BaseHTTPMiddleware | |
| from starlette.requests import Request | |
| logger = logging.getLogger(__name__) | |
| class RequestTracingMiddleware(BaseHTTPMiddleware): | |
| """Attach a unique request ID and log request timing.""" | |
| async def dispatch(self, request: Request, call_next): | |
| request_id = request.headers.get("X-Request-ID") or str(uuid.uuid4()) | |
| request.state.request_id = request_id | |
| start = time.monotonic() | |
| response = await call_next(request) | |
| duration_ms = int((time.monotonic() - start) * 1000) | |
| response.headers["X-Request-ID"] = request_id | |
| logger.info( | |
| "%s %s %s %dms", | |
| request.method, | |
| request.url.path, | |
| response.status_code, | |
| duration_ms, | |
| extra={ | |
| "request_id": request_id, | |
| "method": request.method, | |
| "path": request.url.path, | |
| "status": response.status_code, | |
| "duration_ms": duration_ms, | |
| }, | |
| ) | |
| return response | |