Skip to content

Add structured, correlation-ID request logging middleware #247

Description

@meshackyaro

Description

Every service across the backend logs via NestJS's built-in Logger with free-form string messages (e.g. this.logger.log(\Migration ${name} (${run.runId}) completed`)insrc/migration/migration-runner.service.ts, similar patterns throughout src/gig/, src/dispute/, src/event-ingestion/) — there is no request-scoped correlation/trace ID generated anywhere, and no structured (JSON) log output. src/common/filters/sentry-exception.filter.ts` captures unhandled 5xx errors to Sentry, but there's no way to correlate a given Sentry event with the specific application log lines emitted while handling that same request, since neither carries a shared identifier. In production, tracing a single request's full lifecycle (auth → rate limit → controller → service → webhook dispatch) across logs currently requires guessing based on timestamps and content alone.

Component

Backend

Difficulty

🔴 Hard

Tasks

  • Add middleware that generates (or propagates an inbound X-Request-Id header as) a correlation ID for every request, attaching it to the request object
  • Thread the correlation ID into the Logger context (e.g. via NestJS's AsyncLocalStorage or a request-scoped logger) so every log line emitted while handling a request includes it
  • Attach the same ID as a Sentry tag in SentryExceptionFilter (src/common/filters/sentry-exception.filter.ts) so Sentry events can be cross-referenced with application logs
  • Add a test asserting the correlation ID appears consistently across a sample request's log output and any resulting Sentry capture

Acceptance Criteria

  • Every log line emitted during a single request's handling shares a common correlation/request ID
  • The same ID is attached to any Sentry event captured during that request
  • A test covers ID propagation end to end

Estimated Time

1-2 days

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions