chore: utilize AsyncContext to ensure logs have all request context#1824
Open
paustint wants to merge 1 commit into
Open
chore: utilize AsyncContext to ensure logs have all request context#1824paustint wants to merge 1 commit into
paustint wants to merge 1 commit into
Conversation
Logs were sometimes hard to know which were associated to a give n transaction, this PR uses AsyncContext to store the logger so that the same logger instance with context is used across entire request lifecycle This will make debugging errors and performing research much easier
Contributor
There was a problem hiding this comment.
Pull request overview
This PR migrates request-scoped logging from req.log/res.log to an AsyncLocalStorage-backed request context so logs consistently include request identifiers and late-bound context (user/org/device) across the full request lifecycle.
Changes:
- Add
request-context(AsyncLocalStorage) withrequestContextMiddleware,getLogger(), andenrichRequestContext(). - Replace
req.log/res.logusage across API and geo-ip-api withgetLogger()and enrich context where discovered. - Update logging policy to emit 5xx access logs at
errorand adjust/add tests; add ESLint guard to preventreq.log/res.logregressions inapps/api.
Reviewed changes
Copilot reviewed 54 out of 54 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| libs/shared/node-utils/eslint.config.js | Disable module-boundary rule in config |
| libs/salesforce-api/eslint.config.js | Disable module-boundary rule in config |
| libs/email/eslint.config.js | Disable module-boundary rule in config |
| libs/auth/server/src/lib/auth.service.ts | Switch to getLogger(); enrich user/session |
| libs/auth/server/eslint.config.js | Disable module-boundary rule in config |
| libs/api-types/src/lib/api-route.types.ts | Remove log from API Request/Response types |
| libs/api-types/eslint.config.js | Disable module-boundary rule in config |
| libs/api-config/src/lib/request-context.ts | New AsyncLocalStorage request context + middleware |
| libs/api-config/src/lib/logging-policy.ts | Log 5xx access lines at error |
| libs/api-config/src/lib/tests/request-context.spec.ts | Add tests for request-context behavior |
| libs/api-config/src/lib/tests/logging-policy.spec.ts | Update tests for 5xx access logging |
| libs/api-config/src/lib/tests/db-cache-provider.config.spec.ts | Fix hoisted import ordering in test |
| libs/api-config/src/index.ts | Export request-context; adjust init import order |
| libs/api-config/eslint.config.js | Disable module-boundary rule in config |
| apps/jetstream-web-extension/eslint.config.js | Disable module-boundary rule in config |
| apps/jetstream-web-extension-e2e/eslint.config.js | Disable module-boundary rule in config |
| apps/jetstream-e2e/eslint.config.js | Disable module-boundary rule in config |
| apps/geo-ip-api/src/route.utils.ts | Use getLogger(); remove log types |
| apps/geo-ip-api/src/main.ts | Mount request context middleware; use getLogger() |
| apps/geo-ip-api/eslint.config.js | Disable module-boundary rule in config |
| apps/cron-tasks/src/tests/salesforce-org-expiration.spec.ts | Fix hoisted import ordering in test |
| apps/cron-tasks/eslint.config.js | Disable module-boundary rule in config |
| apps/api/src/main.ts | Mount request context middleware after httpLogger |
| apps/api/src/app/utils/route.utils.ts | Use request-scoped getLogger() in route wrapper |
| apps/api/src/app/utils/response.handlers.ts | Replace res.log fallbacks with getLogger() |
| apps/api/src/app/utils/deferred-response.middleware.ts | Replace res.log with getLogger() logging |
| apps/api/src/app/utils/tests/response.handlers.spec.ts | Adapt tests to mocked getLogger() |
| apps/api/src/app/utils/tests/deferred-response.middleware.spec.ts | Adapt tests to mocked getLogger() |
| apps/api/src/app/services/user-feedback.service.ts | Use getLogger() for service logging |
| apps/api/src/app/services/external-auth.service.ts | Use getLogger(); enrich user/device context |
| apps/api/src/app/services/tests/external-auth.service.spec.ts | Mock getLogger()/enrichRequestContext() |
| apps/api/src/app/routes/team.routes.ts | Replace res.log with getLogger() |
| apps/api/src/app/routes/route.middleware.ts | Replace log usage; enrich user/org context |
| apps/api/src/app/routes/redirect.routes.ts | Replace res.log with getLogger() |
| apps/api/src/app/routes/platform-event.routes.ts | Replace log usage with getLogger() |
| apps/api/src/app/controllers/web-extension.controller.ts | Replace res.log with getLogger() |
| apps/api/src/app/controllers/user.controller.ts | Replace log usage with getLogger() |
| apps/api/src/app/controllers/user-feedback.controller.ts | Replace log usage with getLogger() |
| apps/api/src/app/controllers/test.controller.ts | Replace log usage with getLogger() |
| apps/api/src/app/controllers/team.controller.ts | Replace log usage; update verification logging |
| apps/api/src/app/controllers/socket.controller.ts | Use per-socket child logger; getLogger() for errors |
| apps/api/src/app/controllers/sf-bulk-api.controller.ts | Replace res.log with getLogger() |
| apps/api/src/app/controllers/orgs.controller.ts | Replace res.log with getLogger() |
| apps/api/src/app/controllers/oauth.controller.ts | Replace log usage; pass getLogger() downstream |
| apps/api/src/app/controllers/desktop-app.controller.ts | Replace res.log with getLogger() |
| apps/api/src/app/controllers/canvas.controller.ts | Replace res.log with getLogger() |
| apps/api/src/app/controllers/billing.controller.ts | Replace log usage with getLogger() |
| apps/api/src/app/controllers/auth.controller.ts | Replace res.log with getLogger() |
| apps/api/src/app/controllers/tests/web-extension.controller.spec.ts | Mock getLogger() for controller tests |
| apps/api/src/app/controllers/tests/user.controller.spec.ts | Mock getLogger() for controller tests |
| apps/api/src/app/controllers/tests/team.controller.spec.ts | Mock getLogger(); adapt domain verification tests |
| apps/api/src/app/controllers/tests/desktop-app.controller.spec.ts | Mock getLogger() for controller tests |
| apps/api/src/app/controllers/tests/auth.controller.spec.ts | Mock getLogger() for controller tests |
| apps/api/eslint.config.js | Ban req.log/res.log (non-test) via restricted syntax |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
1
to
4
| // Sentry must initialize before any other module that should be auto-instrumented (db, http). | ||
| import './lib/api-sentry-config'; | ||
| import './lib/api-db-config'; | ||
| import './lib/api-sentry-config'; | ||
| import './lib/env-config'; |
| @@ -0,0 +1,100 @@ | |||
| import { Maybe } from '@jetstream/types'; | |||
Comment on lines
+11
to
+13
| // NOTE: `req.log` / `res.log` are intentionally NOT declared. Request-scoped logging now flows | ||
| // through `getLogger()` from `@jetstream/api-config` (AsyncLocalStorage). Reading `req.log` / | ||
| // `res.log` is disallowed — see the ESLint `no-restricted-syntax` guard in apps/api/.eslintrc. |
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.
Logs were sometimes hard to know which were associated to a give n transaction, this PR uses AsyncContext to store the logger so that the same logger instance with context is used across entire request lifecycle
This will make debugging errors and performing research much easier