Implement Request Correlation ID Middleware with AsyncLocalStorage - #152
Implement Request Correlation ID Middleware with AsyncLocalStorage#152Damilorlar wants to merge 6 commits into
Conversation
- Bind correlation ID into AsyncLocalStorage via TraceContext.run() - Middleware generates/reuses x-request-id and x-correlation-id headers - propagate correlation ID through entire request lifecycle - Unit tests verify persistence and propagation across nested calls
|
@Damilorlar 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! 🚀 |
|
Needs changes The PR implements the request ID middleware and unit tests, but misses the required integration into the uniform response envelope metadata and NestJS Logger outputs as specified in issue #120.
Reviewed commit: |
There was a problem hiding this comment.
Needs changes
The PR implements the request ID middleware and unit tests, but misses the required integration into the uniform response envelope metadata and NestJS Logger outputs as specified in issue #120.
src/middleware/request-id.middleware.ts: Issue #120 requires that the uniform response envelope includes the request ID in its metadata and that it is attached to NestJS Logger outputs for every request lifecycle phase, which is not addressed in this PR.
Reviewed commit: a159aee870620881ef917ac0aa11c8f862bf6000.
|
MergeKeeper review Scope: in scope for linked issue The pull request correctly implements and tests the request correlation ID middleware using AsyncLocalStorage according to the issue requirements. Reviewed commit: |
|
MergeKeeper merge status Status: blocked Reason: One or more required CI checks failed. Failing checks:
Next steps:
|
- Changed '../../common/context/trace.context' to '../common/context/trace.context' - One ../ from src/middleware/ resolves to src/, then common/context/ reaches the actual file location - Fixes TS2307: Cannot find module error
- Changed import from '../context/trace.context' to '../../common/context/trace.context' - Removed unused NextFunction import - Removed unused requestId variable assignment - Fixes TS2307 and lint errors
- Changed import from '../../common/context/trace.context' to '../common/context/trace.context' - One ../ from src/middleware/ resolves to src/, then common/context/ reaches trace.context.ts - Removed TraceContextData from import (was unused, causing lint error @typescript-eslint/no-unused-vars) - Now matches the exact import path in request-id.middleware.ts
- Line 10: added headers: {} to mock req (fixes TypeError: cannot read x-request-id of undefined)
- Lines 77-79: changed to expect(storedTrace).toBeDefined() then typeof storedTrace.traceId (fixes TS18048)
- All tests now exercise the middleware code paths with proper mock objects
##closed #120
Implement request correlation ID middleware for the NestJS modular monolith to provide consistent request tracing across asynchronous operations, database queries, background jobs, and service calls.
The middleware should extract the correlation ID from the incoming X-Request-ID header or generate a new UUID v4 when one is not provided. The ID should then be stored in the existing AsyncLocalStorage context and propagated throughout the request lifecycle.
The correlation ID should also be included in the standard response envelope as requestId and attached to NestJS Logger output, making it possible to trace logs and operations back to a specific HTTP request.
Acceptance Criteria
Implement NestJS middleware to capture X-Request-ID or generate a UUID v4.
Store the correlation ID in the existing AsyncLocalStorage execution context.
Include the correlation ID as requestId in the standard response metadata.
Ensure the correlation ID is available to NestJS Logger outputs throughout the request lifecycle.
Add unit tests verifying correlation ID persistence and propagation across nested service calls.
Verify that a new correlation ID is generated when X-Request-ID is not provided.