feat(observability): forward Winston error logs to PostHog Error Tracking (closes #3651)#3654
Conversation
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (7)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3654 +/- ##
==========================================
+ Coverage 89.15% 89.20% +0.05%
==========================================
Files 135 136 +1
Lines 4646 4668 +22
Branches 1437 1451 +14
==========================================
+ Hits 4142 4164 +22
Misses 392 392
Partials 112 112
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds opt-in forwarding of Winston error (and above) logs to PostHog Error Tracking, integrating with the existing errorTracker.captureException() path and introducing a dedup flag to avoid double-sending the same Error object.
Changes:
- Introduces
PostHogErrorTransport(custom Winston transport) to forward error logs to PostHog viaerrorTracker.captureException(). - Conditionally wires the transport into the global logger when
config.analytics.posthog.errorTracking === true. - Marks
err.posthogCaptured = truein the Express error middleware path and adds unit/wiring/integration tests around the new behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/services/logger.posthog.transport.js | New Winston transport that forwards error logs to PostHog and attempts dedup via err.posthogCaptured. |
| lib/services/logger.js | Conditionally registers the PostHog transport when error tracking is enabled. |
| lib/services/errorTracker.js | Marks errors with posthogCaptured in the Express error handler for deduplication. |
| lib/services/tests/logger.posthog.transport.unit.tests.js | Unit coverage for transport extraction/wrapping, dedup skip, and “never throw” behavior. |
| lib/services/tests/logger.posthog.transport.wiring.unit.tests.js | Verifies conditional transport registration based on config. |
| lib/services/tests/logger.posthog.transport.integration.tests.js | End-to-end verification that logger.error(...) produces $exception events and respects the dedup flag. |
| lib/services/tests/errorTracker.unit.tests.js | Adds a test asserting the dedup flag is set by the Express error handler. |
| captureException(err, { distinctId, requestId: req.id }); | ||
| if (err && typeof err === 'object') err.posthogCaptured = true; | ||
| next(err); |
| export class PostHogErrorTransport extends Transport { | ||
| constructor(opts = {}) { | ||
| super({ ...opts, level: opts.level ?? 'error' }); | ||
| } | ||
|
|
Summary
PostHogErrorTransport— a custom Winston transport that forwardserror-level (and above) logs to PostHog Error Tracking via the existingerrorTracker.captureException()service.logger.jsconditionally onconfig.analytics.posthog.errorTracking === true(opt-in, no change for unconfigured projects).err.posthogCaptured = trueinerrorTracker.setupExpressErrorHandlerafter Express captures an error, so the same exception is never double-sent to PostHog by the Winston transport.Closes #3651
Changes
lib/services/logger.posthog.transport.jsPostHogErrorTransportclasslib/services/logger.jserrorTracking=truelib/services/errorTracker.jserr.posthogCaptured = trueafter Express capturelib/services/tests/logger.posthog.transport.unit.tests.jslib/services/tests/logger.posthog.transport.wiring.unit.tests.jslib/services/tests/logger.posthog.transport.integration.tests.jslib/services/tests/errorTracker.unit.tests.jsDedup design
When an unhandled error bubbles through Express AND was previously logged via
logger.error, the middleware marks the flag so the transport skips it on the second pass.Notes from pre-push critical-review
logger.posthog.transport.jsis always imported at module-load time (ESM static imports) even whenerrorTracking=false. Functionally safe —errorTrackerguards its own config check. Can be lazy-loaded via dynamicimport()in a follow-up if module-load budget becomes a concern.log.fileLogger— added to preventgetLogOptions()crash in test isolation. Behavior under test is unchanged.Test plan
PostHogErrorTransport(level default, Error forwarding, info.error extraction, synthetic Error wrapping, dedup skip, posthogCaptured flag set, SDK throw safety)errorTracking=true, absent whenfalse)npm run lint— cleannpm run test:unit— 1379 passednpm run test:coverage— thresholds unchangedRelated
sourceparam merged at48766e5a)infra/docs/superpowers/plans/2026-05-11-posthog-observability-wave4.mdPhase 1