feat: add correlation ID middleware for request tracing#170
feat: add correlation ID middleware for request tracing#170noevidence1017 wants to merge 3 commits into
Conversation
|
@noevidence1017 is attempting to deploy a commit to the Abdulazeem's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@noevidence1017 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
Could you click the Update branch button on your end, or run git pull origin main locally and push? That will make the tests pass so I can merge this!" Fix conflict |
|
Fix backend build test error |
|
Fix conflicts and backend build test error |
|
Check your backend build details to know what is causing the error and resolve it. |
closes #31
What
Adds a correlation ID middleware that gives every incoming request a unique tracking ID, making it possible to trace all log lines belonging to a single API call across middleware layers and async tasks.
Changes
New middleware/correlation.js: checks for an inbound X-Correlation-ID header and reuses it when present; otherwise generates a uuidv4. The value is exposed on req.correlationId, set as the outbound X-Correlation-ID response header, and used to prefix a per-request log line emitted on response finish.
Mounted the middleware globally at the top of the Express stack in server.js, ahead of CORS and body parsing, so the ID is available to every downstream handler and error path.
Updated the global error handler so 500 logs are prefixed with the correlation ID, and the ID is returned in the error response body alongside the existing reference ID.
Added the uuid dependency (pinned to ^9.0.1 to remain CommonJS-compatible with the existing Jest setup).
Testing
Existing Jest suite passes (28 passed).
Verified that requests without the header receive a generated ID and that an inbound X-Correlation-ID is preserved and echoed back.