Skip to content

feat: add request correlation IDs - #298

Open
Heet27 wants to merge 5 commits into
mainfrom
feature/26t2-be-hg-001-request-correlation-id
Open

feat: add request correlation IDs#298
Heet27 wants to merge 5 commits into
mainfrom
feature/26t2-be-hg-001-request-correlation-id

Conversation

@Heet27

@Heet27 Heet27 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

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-ID header. 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

  • Bug fix
  • New feature
  • Breaking change
  • Refactor / code cleanup
  • Documentation
  • CI/CD / infrastructure
  • Security

Affected Components

  • /backend-api
  • /frontend
  • /engine (collectors / policies)
  • /security
  • /infrastructure
  • /.github/workflows
  • /docs

Motivation

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

  • Unit tests pass locally
  • Tested manually — describe how:
  • No tests required — explain why:

Manual testing completed by running the backend locally with Uvicorn.

Tests performed:

  • Accessed GET /docs without an X-Request-ID header and confirmed that a UUID was generated and returned in the response.
  • Sent a request with X-Request-ID: HG-TEST-001 using curl and confirmed the same value was returned in the response header.
  • Verified that multiple requests generated different IDs when no request ID was supplied.

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

  • No breaking changes
  • Yes — describe below:

The existing API behaviour remains unchanged. The only addition is the X-Request-ID response header.


Rollback Plan

  • Revert commit is sufficient
  • Requires additional steps — describe below:

The feature is isolated to the request logging middleware and can be removed by reverting this commit.


Checklist

  • Code follows project conventions
  • No secrets, credentials, or tokens committed
  • Relevant documentation updated (if applicable)
  • CI/CD workflows pass on this branch
  • PR is focused on one thing

Screenshots

  1. Response showing an automatically generated X-Request-ID.
  2. Response showing the supplied X-Request-ID: HG-TEST-001 being returned.
  3. Backend terminal demonstrating successful requests while the middleware is running.
Auto-generated Request ID Backend Log Custom Request ID

@github-actions

Copy link
Copy Markdown
Contributor

Preview Environment

A preview environment can be spun up on demand for this PR.

Action Label Includes
Spin up preview deploy-preview Frontend, backend, database, Redis, OPA, worker
Spin up preview with M365 deploy-preview-m365 Everything above + PowerShell service for Exchange/Teams scan testing
Tear down preview teardown-preview Stops the environment early

The environment will also be torn down automatically when the PR is closed or merged.
Preview URLs will appear in a follow-up comment once the deploy completes (~5–8 min).
M365 scans require real tenant credentials added through the frontend UI.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

CI: Backend API

Job Result
Security analysis (CodeQL + Bandit) success
Lint failure

One or more checks failed. View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant