Add request ID middleware and correlate logs with request context#58
Merged
prodbycorne merged 1 commit intoJun 27, 2026
Merged
Conversation
Generate a UUID per request, expose it via X-Request-ID, and propagate it to all log lines through AsyncLocalStorage so concurrent requests can be traced in production.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds request ID middleware and AsyncLocalStorage-backed log correlation so concurrent request logs can be traced through the system. Every incoming request gets a UUID, returned via
X-Request-ID, and all log lines within that request automatically include the matchingrequestId.Related Issue
Closes #34
Changes
🔗 Request ID Middleware
src/middleware/requestId.jscrypto.randomUUID().req.idand sets theX-Request-IDresponse header.AsyncLocalStorageso child log calls inherit request context without passingreqmanually.📝 Logger Correlation
src/logger.jsrequestIdfrom AsyncLocalStorage on every log line.requestId: 'system'(background jobs, startup, shutdown).🚀 App Wiring
src/index.jsrequestIdMiddlewareearly in the Express middleware chain.serveris defined when the app is imported in tests.🧪 Tests
test/requestId.test.jsX-Request-IDis present on responses.req.idmatches the response header.requestIdfor a given request.requestId: 'system'./healthreturnsX-Request-ID.🔧 Configuration
package.jsonnpm installfrom parsing dependencies.Verification Results
X-Request-IDheader on every responserequestId: 'system'requestIdfor a given requestreqpassed manually through every function)