Skip to content

fix(posthog): upgrade SDKs and fix serverless event flushing#3951

Merged
waleedlatif1 merged 2 commits intostagingfrom
waleedlatif1/posthog-node-fix
Apr 4, 2026
Merged

fix(posthog): upgrade SDKs and fix serverless event flushing#3951
waleedlatif1 merged 2 commits intostagingfrom
waleedlatif1/posthog-node-fix

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

@waleedlatif1 waleedlatif1 commented Apr 4, 2026

Summary

  • Upgrade posthog-js 1.334.1 → 1.364.4, posthog-node 5.9.2 → 5.28.9
  • Move PostHog shutdown handler out of OTel init so it runs regardless of telemetry config

Type of Change

  • Bug fix

Testing

  • Full tsc --noEmit passes with zero errors
  • Lint passes clean
  • Verified all PostHog APIs used (capture, shutdown, init, identify, reset, __loaded, PostHogProvider) still exist in new versions
  • Verified all client-side config options (person_profiles, autocapture, session_recording, persistence, etc.) still valid

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 4, 2026 11:07pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 4, 2026

PR Summary

Medium Risk
Upgrades posthog-js/posthog-node and adjusts SIGINT/SIGTERM shutdown behavior; the main risk is subtle runtime changes in analytics delivery/flushing and process shutdown ordering.

Overview
Upgrades PostHog SDK dependencies (posthog-js and posthog-node) and updates the lockfile to match.

Moves PostHog shutdown/flush handling out of OpenTelemetry initialization: OTel now only shuts down its own SDK, while register() always registers a separate SIGINT/SIGTERM handler to call getPostHogClient()?.shutdown() so events can flush even when OTel is disabled or not configured.

Reviewed by Cursor Bugbot for commit 6ab5fa5. Configure here.

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 4, 2026

Greptile Summary

This PR fixes a real bug where the PostHog shutdown() call was only registered when OpenTelemetry was also enabled. By moving the shutdownPostHog signal handler from inside initializeOpenTelemetry() to the top-level register() function, PostHog events will now be properly flushed on process exit regardless of telemetry configuration. The two PostHog SDK upgrades (posthog-js 1.334.1 → 1.364.4, posthog-node 5.9.2 → 5.28.9) are routine maintenance.

Key changes:

  • shutdownPostHog handler moved from initializeOpenTelemetry() (which exits early if OTel is disabled) to register(), guaranteeing SIGTERM/SIGINT coverage on all deployments
  • posthog-js bumped from 1.334.1 to 1.364.4
  • posthog-node bumped from 5.9.2 to 5.28.9
  • Lock file updated accordingly

Confidence Score: 5/5

Safe to merge — the change is a straightforward and correct refactor with no logic regressions.

The OTel/PostHog separation is exactly right: OTel shutdown belongs inside initializeOpenTelemetry() because it depends on the sdk instance, while PostHog shutdown belongs in register() since it must always run. No P0 or P1 issues found.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/instrumentation-node.ts Moves PostHog shutdown handler out of OTel init into register() so it always fires on SIGTERM/SIGINT regardless of telemetry config — clean and correct fix
apps/sim/package.json Upgrades posthog-js 1.334.1→1.364.4 and posthog-node 5.9.2→5.28.9; no other dependency changes
bun.lock Lock file updated to reflect posthog dependency upgrades

Sequence Diagram

sequenceDiagram
    participant Next as Next.js Runtime
    participant Reg as register()
    participant OTel as initializeOpenTelemetry()
    participant PH as PostHog Client
    participant OS as OS Signal

    Next->>Reg: call register()
    Reg->>OTel: await initializeOpenTelemetry()
    alt OTel enabled
        OTel->>OTel: start SDK
        OTel->>OS: register shutdownOtel on SIGTERM/SIGINT
    else OTel disabled
        OTel-->>Reg: return early (no OTel handlers)
    end
    Reg->>OS: register shutdownPostHog on SIGTERM/SIGINT
    Note over Reg,OS: PostHog handler always registered

    OS->>OTel: SIGTERM/SIGINT (if OTel enabled)
    OTel->>OTel: sdk.shutdown()
    OS->>PH: SIGTERM/SIGINT
    PH->>PH: flush & shutdown
Loading

Reviews (1): Last reviewed commit: "fix(posthog): revert flushAt to 20 for l..." | Re-trigger Greptile

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 6ab5fa5. Configure here.

@waleedlatif1 waleedlatif1 merged commit 0ba8ab1 into staging Apr 4, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/posthog-node-fix branch April 4, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant