Skip to content

Add context information to each log message in authenticated endpoints#535

Merged
GianlucaFicarelli merged 6 commits intomainfrom
log_sub_id
Feb 20, 2026
Merged

Add context information to each log message in authenticated endpoints#535
GianlucaFicarelli merged 6 commits intomainfrom
log_sub_id

Conversation

@GianlucaFicarelli
Copy link
Collaborator

@GianlucaFicarelli GianlucaFicarelli commented Feb 20, 2026

Add context information to each log message in authenticated endpoints.
It shold be used only in authenticated endpoints, since it depends on user_context.
These additional keys are added to the extra dict:

  • sub_id: the subject_id from Keycloak
  • request_id: id that can be used to correlate multiple logs in the same request
  • forwarded_for: the originating IP address of the client, from the X-Forwarded-For HTTP header

This PR also adds 2 headers to the response:

  • x-process-time: process time in seconds, formatted
  • x-request-id: the request id, server generated. If the header is present also in the request headers, its value is ignored to ensure that is unique on the server side, but this can be discussed.
  • We could decide to add also a header X-Correlation-ID to trace the transaction end-to-end across multiple services, but need more considerations and changes.

Example of the added response headers:

< x-request-id: 87746ace-9e21-4879-bfee-bbf4f7c7e300
< x-process-time: 0.868

Fix https://github.com/openbraininstitute/prod-platform-architecture/issues/187

@GianlucaFicarelli GianlucaFicarelli self-assigned this Feb 20, 2026
@codecov
Copy link

codecov bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
pytest 97.68% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
app/application.py 96.96% <100.00%> (+1.05%) ⬆️
app/dependencies/logger.py 100.00% <100.00%> (ø)
app/routers/__init__.py 100.00% <100.00%> (ø)
app/schemas/types.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@mgeplf mgeplf left a comment

Choose a reason for hiding this comment

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

that's so beautiful

@GianlucaFicarelli GianlucaFicarelli merged commit 3281799 into main Feb 20, 2026
2 checks passed
@GianlucaFicarelli GianlucaFicarelli deleted the log_sub_id branch February 20, 2026 12:28
GianlucaFicarelli added a commit that referenced this pull request Feb 24, 2026
* origin/main:
  Log request context in a middleware (#536)
  Add context information to each log message in authenticated endpoints (#535)
  Pin alembic<1.18 for compatibility with alembic-postgresql-enum (#533)
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.

3 participants