Skip to content

Add Sentry error tracking and performance tracing #205

@jakebromberg

Description

@jakebromberg

Context

After reverting Sentry from dj-site (Cloudflare Workers incompatibility), neither repo has error tracking. Backend-Service runs on EC2 with no compatibility issues, so we use @sentry/node for server-side error capture and performance tracing.

Changes

Sentry Initialization

  • apps/backend/instrument.ts and apps/auth/instrument.ts -- Sentry.init() with DSN, release, environment, and tracesSampleRate: 1.0. Auto-detected integrations cover Express, HTTP, and PostgreSQL.

Express Integration

  • apps/backend/app.ts -- import './instrument.js' as first line, Sentry.setupExpressErrorHandler(app) after routes before errorHandler, requestIdMiddleware after express.json().
  • apps/auth/app.ts -- import './instrument.js' as first line, Sentry.setupExpressErrorHandler(app) after routes, new fallback error handler (auth had none).

Cross-Service Correlation

  • apps/backend/middleware/requestId.ts -- Reads X-Request-Id from incoming request (sent by dj-site) or generates one via crypto.randomUUID(). Sets on response header and tags Sentry scope with request_id.
  • CORS updated -- X-Request-Id added to allowedHeaders and exposedHeaders.

Build Configuration

  • apps/backend/tsup.config.ts -- external: ['@sentry/node'], sourcemap: true (Sentry monkey-patches require unbundled modules).
  • apps/auth/tsup.config.ts -- '@sentry/node' added to existing external array.

Deploy

  • .github/actions/deploy-service/action.yml -- -e SENTRY_RELEASE=$TARGET_APP@$DEPLOY_TAG added to docker run.

Tests

  • tests/unit/middleware/requestId.test.ts -- 5 tests: UUID generation, pass-through of existing header, response header setting, Sentry scope tagging.

Environment Variables

Add to .env on EC2:

  • SENTRY_DSN -- Sentry project DSN

Verification

  1. npm run build succeeds for both workspaces
  2. npm run test:unit passes (125 tests, including 5 new requestId tests)
  3. Deploy to EC2, hit /healthcheck -- confirm X-Request-Id response header
  4. Trigger an error -- confirm it appears in Sentry with request_id tag
  5. Check Sentry Performance tab for Express spans

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions