feat: add request correlation IDs - #298
Conversation
Preview EnvironmentA preview environment can be spun up on demand for this PR.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 745d018865
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| request.state.request_id = request_id | ||
|
|
||
| response = await call_next(request) |
There was a problem hiding this comment.
Preserve correlation IDs on unhandled failures
When an endpoint or dependency raises an unhandled exception, call_next propagates it before the response header and request log are written. Starlette's outer ServerErrorMiddleware then creates the 500 response after this middleware has exited, so that response has no X-Request-ID and no correlation log—the failure scenario where the ID is most needed. Handle this path so the generated or supplied ID is logged and included on the resulting error response.
Useful? React with 👍 / 👎.
CI: Backend API
One or more checks failed. View logs |
Summary
This PR adds request correlation ID support to the backend request logging middleware. The middleware now checks whether an incoming request contains an
X-Request-IDheader. If the header is present and valid, that value is reused. Otherwise, a new UUID is generated.The request ID is stored in
request.state, returned in the response header, and included in the request log entry. This makes it easier to trace and identify individual requests during debugging and troubleshooting while keeping the change isolated to the middleware.Type of Change
Affected Components
/backend-api/frontend/engine(collectors / policies)/security/infrastructure/.github/workflows/docsMotivation
Planner Task: 26T2-BE-HG-001 – Add Backend Request Correlation IDs
Previously, requests did not have a consistent identifier that could be used to trace them through the application logs. This change adds support for request correlation IDs so that each request can be identified more easily. If a client already provides an
X-Request-ID, it is preserved; otherwise, the middleware generates a new UUID.Testing Done
Manual testing completed by running the backend locally with Uvicorn.
Tests performed:
GET /docswithout anX-Request-IDheader and confirmed that a UUID was generated and returned in the response.X-Request-ID: HG-TEST-001usingcurland confirmed the same value was returned in the response header.Security Considerations
This change does not modify authentication, authorization, permissions, secrets, or API access. It only adds request identification information to improve request tracing and logging.
Breaking Changes
The existing API behaviour remains unchanged. The only addition is the
X-Request-IDresponse header.Rollback Plan
The feature is isolated to the request logging middleware and can be removed by reverting this commit.
Checklist
Screenshots
X-Request-ID.X-Request-ID: HG-TEST-001being returned.