Skip to content

feat: add structured correlation-id request logging middleware - #258

Open
marimuthusanthosh wants to merge 1 commit into
trustflow-protocol:mainfrom
marimuthusanthosh:feat/structured-request-logging
Open

feat: add structured correlation-id request logging middleware#258
marimuthusanthosh wants to merge 1 commit into
trustflow-protocol:mainfrom
marimuthusanthosh:feat/structured-request-logging

Conversation

@marimuthusanthosh

Copy link
Copy Markdown

Description

Implemented correlation-ID request logging middleware for the backend as requested in #247.

The implementation generates or propagates an X-Request-Id for every request, makes the ID available throughout asynchronous request handling, includes it in application logs, and attaches it to Sentry events for easier cross-referencing.

Closes #247

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📝 Documentation update
  • 🔧 Configuration change
  • ♻️ Code refactoring
  • ⚡ Performance improvement
  • ✅ Test update

Component

  • Backend (Node.js API)
  • Frontend (Next.js UI)
  • Smart Contract (Soroban/Rust)
  • SDK
  • Infrastructure/DevOps
  • Documentation

Changes Made

  • Added request ID middleware using AsyncLocalStorage.
  • Reuses the inbound X-Request-Id header or generates a new request ID when one is not provided.
  • Attaches the request ID to the request object and response headers.
  • Propagates the request ID through asynchronous request handling.
  • Updated the logger wrapper to include the request ID in log messages.
  • Updated SentryExceptionFilter to attach request_id and correlation_id tags to Sentry events.
  • Added regression test coverage to verify that the same request ID is propagated consistently through logging and Sentry capture.

Testing

Manual Testing

  • Tested locally
  • Tested in development environment
  • Tested edge cases

Automated Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • All tests passing locally

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Screenshots/Recordings (if applicable)

Not applicable. This is a backend middleware and logging change.

Additional Notes

The implementation is designed to allow application logs and Sentry events from the same request to be correlated using a shared request/correlation ID.

Example log output:

[requestId=req-123] ...

The same ID is attached to Sentry through the request_id and correlation_id tags.

Reviewer Notes

Please review the following areas in particular:

  • Request ID generation and X-Request-Id propagation.
  • AsyncLocalStorage context propagation across asynchronous request handling.
  • Logger integration and whether all request-scoped logs consistently include the ID.
  • Sentry tag integration and consistency between logs and Sentry events.
  • End-to-end test coverage for request ID propagation.

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.

Add structured, correlation-ID request logging middleware

1 participant