Skip to content

feat: PostHog sourcemaps + server logs (OpenTelemetry)#1282

Merged
mbifulco merged 8 commits into
mainfrom
feat/sourcemap-uploads
Jul 8, 2026
Merged

feat: PostHog sourcemaps + server logs (OpenTelemetry)#1282
mbifulco merged 8 commits into
mainfrom
feat/sourcemap-uploads

Conversation

@mbifulco

@mbifulco mbifulco commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Summary

Adds production-error observability for the site via PostHog:

  1. Sourcemap uploadnext.config.mjs now wraps the build with @posthog/nextjs-config's withPostHogConfig (outermost wrapper), gated to VERCEL_ENV === 'production' with deleteAfterUpload: 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.
  2. Server logs via OpenTelemetrysrc/instrumentation.ts stands up an OTLP log pipeline (src/server/logging/otel-logs.ts) that patches console.* 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 the llms.txt route (after()) so batches flush before serverless freeze.
  3. Host consolidation — all PostHog host strings moved into 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 /ingest reverse proxy).
  4. PII hygiene — since the console bridge ships all server logs to PostHog Logs, trimmed the Resend webhook logs to non-PII identifiers (no subscriber email/name).

Commits

  • 7eb2ed1 sourcemap upload via @posthog/nextjs-config
  • fab4957 OTel server-log pipeline + console bridge
  • 2b09179 flush wiring (tRPC + llms.txt)
  • 5293a16 eslint fix (type-only import)
  • c676a35 centralize PostHog host config
  • c371864 trim subscriber PII from webhook logs

Manual setup required before this works in prod

  • Add POSTHOG_PERSONAL_API_KEY to Vercel (Production scope) — a personal API key with write access to Error Tracking.
  • Confirm NEXT_PUBLIC_POSTHOG_HOST is unset or points at a real PostHog host (us.i.posthog.com), not the mikebifulco.com/ingest reverse proxy — the sourcemap upload API is not proxied.
  • After deploy: verify a symbol set appears in PostHog → Error Tracking, and a line appears in PostHog → Logs.

Reviewer notes (non-blocking)

  • Log volume/cost: every server console.* line now ships to PostHog Logs. Consider severity/source filtering or sampling if quota becomes a concern.
  • Error stacks: stringifyArg renders Error as name: message (no stack) in log bodies — stacks are expected to come from Error Tracking + sourcemaps.

Verification

pnpm typecheck, pnpm lint, and pnpm build pass (aside from one pre-existing src/lib/unified-feed.test.ts type 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

mbifulco and others added 6 commits July 6, 2026 07:50
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>
Copilot AI review requested due to automatic review settings July 6, 2026 15:21
@vercel

vercel Bot commented Jul 6, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
mikebifulco-com-bnbu Ready Ready Preview, Comment Jul 8, 2026 11:36am

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-config into next.config.mjs to 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.

Comment thread src/server/logging/otel-logs.ts
Comment thread src/server/logging/otel-logs.ts
Comment thread next.config.mjs
Comment thread src/server/logging/otel-logs.test.ts
Comment thread src/server/logging/otel-logs.test.ts
- 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>
@mbifulco mbifulco enabled auto-merge July 8, 2026 11:32
@mbifulco mbifulco merged commit 23560c8 into main Jul 8, 2026
8 checks passed
@mbifulco mbifulco deleted the feat/sourcemap-uploads branch July 8, 2026 11:36
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.

2 participants