Skip to content

Codex [ FastAPI ] Add request ID middleware for log correlation#1151

Closed
cerredz wants to merge 1 commit into
UnsafeLabs:mainfrom
cerredz:fix/797-request-id-middleware
Closed

Codex [ FastAPI ] Add request ID middleware for log correlation#1151
cerredz wants to merge 1 commit into
UnsafeLabs:mainfrom
cerredz:fix/797-request-id-middleware

Conversation

@cerredz
Copy link
Copy Markdown

@cerredz cerredz commented May 16, 2026

/claim #797

Summary:

  • Adds RequestIDMiddleware as a pure ASGI middleware that preserves client-provided X-Request-ID values or generates UUID request IDs.
  • Stores the request ID on request.state.request_id and echoes it on the response header.
  • Adds FastAPI logging context via contextvars and a log record factory so records expose request_id during request handling without leaking between requests.
  • Adds focused tests for generated IDs, client-provided IDs, log-record metadata, concurrent isolation, and default logging behavior without middleware.

Validation:

  • uv run python -m pytest tests/test_request_id_middleware.py -q
  • uv run ruff check fastapi/middleware/request_id.py fastapi/logger.py tests/test_request_id_middleware.py
  • uv run ruff format --check fastapi/middleware/request_id.py fastapi/logger.py tests/test_request_id_middleware.py
  • python -m compileall -q fastapi/middleware/request_id.py fastapi/logger.py tests/test_request_id_middleware.py
  • git diff --check HEAD~1..HEAD

Note: I intentionally did not add _provenance.json because it asks for full prompt/runtime/session provenance rather than product code.

@github-actions
Copy link
Copy Markdown
Contributor

Unfortunately the changes in this PR didn't fully resolve the issue. Please rework your solution and submit a new pull request.

Make sure to review the acceptance criteria in the linked issue and verify all conditions are met before resubmitting. See CONTRIBUTING.md for guidelines.

@github-actions github-actions Bot closed this May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant