feat: structured logging with correlation_id propagation - #10
Merged
Conversation
lmoraesdev
force-pushed
the
feat/structured-logging
branch
from
May 15, 2026 17:43
143dcbe to
1a1b863
Compare
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.
What
Adds structured logging to the application and propagates a
correlation_idthrough every layer of a single request — automatically, with no parameter threading.Every log line is a single JSON object with a fixed shape:
{ "when": "2026-05-14T20:32:00.123Z", "level": "info", "correlation_id": "req_8h2k3pX...", "where": "CreateChargeService", "what": "charge_created", "why": "user_request", "how": "POST /charges", "charge_id": "ch_..." }Logs emitted in this PR:
CreateChargeServicecharge_created,idempotency_cache_hit,idempotency_conflictProcessWebhookServicewebhook_received,webhook_already_processed,charge_state_transitionedWhy
When a payment incident hits at 2am, the on-call doesn't have time to grep across services to assemble what happened. Structured logs with a shared
correlation_idgive them every request's story in one query, with the same field names every time.The
correlation_id:X-Correlation-Idrequest header if present (clients can pass their own to trace cross-service)req_<uuid>AsyncLocalStoragefor the duration of the request, so every service that logs picks it up automatically — no parameter threadingApproach
Five commits:
feat: add StructuredLogger with 5W1H formatfeat: add correlation id middleware with AsyncLocalStoragetest: add unit tests for StructuredLogger and CorrelationIdMiddleware(including request isolation test)feat: emit structured logs from CreateChargeService and ProcessWebhookServicedocs: document logging format in READMErefactor: improve structured logger and add testsNotes
AsyncLocalStorageis the Node equivalent of Go'scontext.Contextor Java'sThreadLocal— it propagates values down the call stack without polluting function signatures.storage.run(value, callback), notenterWith(), which is what guarantees per-request isolation under concurrency.correlation_id.