feat: PostHog sourcemaps + server logs (OpenTelemetry)#1282
Merged
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The OTel console bridge now ships all server console output to PostHog Logs, so drop email/name from the routinely-firing webhook logs and log only non-PII identifiers (segment ids, event type, bounce type). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds PostHog-based production observability by uploading Next.js sourcemaps at build time and exporting server-side console logs via an OpenTelemetry OTLP log pipeline, while centralizing PostHog host configuration and reducing PII in webhook logging.
Changes:
- Integrates
@posthog/nextjs-configintonext.config.mjsto upload sourcemaps in production. - Adds an OTLP log exporter + console bridge and wires
flushLogs()into request/route teardown paths. - Centralizes PostHog host constants and updates client/server PostHog configuration to reference them; trims webhook log PII.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/server/posthog.ts | Switches server PostHog client default host to centralized constant. |
| src/server/logging/otel-logs.ts | Adds OTLP logging pipeline and console bridge to emit server logs to PostHog. |
| src/server/logging/otel-logs.test.ts | Adds unit tests for severity mapping, console bridge behavior, and flush safety. |
| src/pages/api/trpc/[trpc].ts | Flushes pending logs after each tRPC API request via finally. |
| src/pages/api/inbound_webhooks/resend.ts | Reduces PII in webhook logs now that server logs are exported. |
| src/lib/posthog/hosts.ts | Introduces centralized PostHog host constants (API/assets/UI/proxy/logs endpoint). |
| src/lib/posthog/hosts.test.ts | Adds unit tests to enforce host constant values/derivations. |
| src/instrumentation.ts | Registers server logging via Next.js instrumentation hook. |
| src/app/posthog-provider.tsx | Updates client PostHog init to use centralized host constants. |
| src/app/llms.txt/route.ts | Schedules log flush after request completion using after(). |
| pnpm-workspace.yaml | Allows @posthog/cli to run build steps under pnpm policy. |
| pnpm-lock.yaml | Locks new PostHog config + OpenTelemetry logging dependencies. |
| package.json | Adds @posthog/nextjs-config and OpenTelemetry logging packages. |
| next.config.mjs | Adds PostHog sourcemap upload wrapper + rewrites updated to use centralized hosts. |
Files not reviewed (1)
- pnpm-lock.yaml: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Gate server-log export to VERCEL_ENV=production so preview/dev console output never ships to PostHog Logs (matches the sourcemap upload gate). - Always use the real PostHog API host for build-time sourcemap uploads, not NEXT_PUBLIC_POSTHOG_HOST (which may be the /ingest reverse proxy). - Wrap console-bridge tests in try/finally so a failing assertion cannot leak patched console methods into other tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Summary
Adds production-error observability for the site via PostHog:
next.config.mjsnow wraps the build with@posthog/nextjs-config'swithPostHogConfig(outermost wrapper), gated toVERCEL_ENV === 'production'withdeleteAfterUpload: true. Production JS errors will show unminified stack traces in PostHog Error Tracking. Because injection must run on the deployed build, this hooks into the Vercel build rather than a GitHub Action.src/instrumentation.tsstands up an OTLP log pipeline (src/server/logging/otel-logs.ts) that patchesconsole.*to also emit OTel log records to PostHog Logs (/i/v1/logs, authed with the public project token).flushLogs()is wired into the tRPC handler (try/finally) and thellms.txtroute (after()) so batches flush before serverless freeze.src/lib/posthog/hosts.ts; the client provider, server client, logs endpoint, and next.config rewrites + wrapper host all import from it. Resolved URLs are byte-identical (prod client still uses the/ingestreverse proxy).Commits
7eb2ed1sourcemap upload via@posthog/nextjs-configfab4957OTel server-log pipeline + console bridge2b09179flush wiring (tRPC + llms.txt)5293a16eslint fix (type-only import)c676a35centralize PostHog host configc371864trim subscriber PII from webhook logsManual setup required before this works in prod
POSTHOG_PERSONAL_API_KEYto Vercel (Production scope) — a personal API key with write access to Error Tracking.NEXT_PUBLIC_POSTHOG_HOSTis unset or points at a real PostHog host (us.i.posthog.com), not themikebifulco.com/ingestreverse proxy — the sourcemap upload API is not proxied.Reviewer notes (non-blocking)
console.*line now ships to PostHog Logs. Consider severity/source filtering or sampling if quota becomes a concern.stringifyArgrendersErrorasname: message(no stack) in log bodies — stacks are expected to come from Error Tracking + sourcemaps.Verification
pnpm typecheck,pnpm lint, andpnpm buildpass (aside from one pre-existingsrc/lib/unified-feed.test.tstype error unrelated to this branch). Unit tests: 218 passing. New unit coverage for the OTel logging module and the host constants.🤖 Generated with Claude Code