Skip to content

Implement Request Correlation ID Middleware with AsyncLocalStorage - #152

Open
Damilorlar wants to merge 6 commits into
ASTROIDX556:mainfrom
Damilorlar:feature/correlation-id-middleware
Open

Implement Request Correlation ID Middleware with AsyncLocalStorage#152
Damilorlar wants to merge 6 commits into
ASTROIDX556:mainfrom
Damilorlar:feature/correlation-id-middleware

Conversation

@Damilorlar

Copy link
Copy Markdown

##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.

- 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
@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@mergekeeper

mergekeeper Bot commented Aug 30, 2026

Copy link
Copy Markdown

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: a159aee870620881ef917ac0aa11c8f862bf6000.

@mergekeeper mergekeeper 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.

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

mergekeeper Bot commented Aug 30, 2026

Copy link
Copy Markdown

MergeKeeper review

Scope: in scope for linked issue #120.
Verdict: clean

The pull request correctly implements and tests the request correlation ID middleware using AsyncLocalStorage according to the issue requirements.

Reviewed commit: ec4240d3e759954c75acff27f59e770a4501bcf9.
CI and merge eligibility are checked separately.

@mergekeeper

mergekeeper Bot commented Aug 30, 2026

Copy link
Copy Markdown

MergeKeeper merge status

Status: blocked
PR state: open
Mergeability: mergeable
Checked commit: ec4240d3e759954c75acff27f59e770a4501bcf9.

Reason: One or more required CI checks failed.

Failing checks:

Next steps:

  1. Open the failing check details above and fix the reported error.
  2. Run the same checks locally where possible.
  3. Commit and push the fix.
  4. MergeKeeper will automatically re-review the updated PR.

- 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
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 request correlation ID propagation middleware and logging interceptor

1 participant