Skip to content

fix(next): bundle the instrumentation factory and stop capturing evlog's own output - #816

Merged
HugoRCD merged 1 commit into
mainfrom
fix/next-instrumentation-bundling
Oct 10, 2026
Merged

HugoRCD merged 1 commit into
mainfrom
fix/next-instrumentation-bundling

Conversation

@HugoRCD

@HugoRCD HugoRCD commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

🔗 Linked issue

No issue. Reported by a user deploying defineNodeInstrumentation to Vercel.

📚 Description

Two bugs in evlog/next/instrumentation, both hit with defineNodeInstrumentation({ ..., captureOutput: true }).

Every route returns 500 once deployed. The gate loaded evlog/next/instrumentation/create through a computed import(/* webpackIgnore: true */ ...). Turbopack keeps it as a bare runtime import and the output file trace does not include evlog, so register() throws Cannot find package 'evlog' on Vercel or a copied standalone build, and Next.js fails every request. With webpack the trace includes evlog, but the factory loads unbundled from node_modules, where NEXT_RUNTIME is not inlined, so its NEXT_RUNTIME === 'nodejs' check sees undefined and captureOutput never applies.

The gate now runs if (process.env.NEXT_RUNTIME === 'nodejs') await import('./instrumentation-create'). Both bundlers inline NEXT_RUNTIME: the Node.js build bundles the factory and the logger into instrumentation.js, and the Edge build drops the branch. No change to exports.

Drains receive every event twice. In JSON mode, outputWideEvent writes through console.log / console.error, and the patched process.stdout.write / process.stderr.write captured that line again as log.info / log.error. Pretty output already bypassed the patch through a global slot holding the native write. That slot is replaced by a __evlogOwnOutput flag set around every write evlog makes itself, and both patches pass flagged writes through.

Verification

Next.js 16.4.0, output: 'standalone', with the standalone output copied to a directory that has no parent node_modules, as on Vercel. Routes: POST /api/webhook (withEvlog), GET /api/hello (plain console.log), GET /api/edge (Edge runtime).

Build webhook hello edge Route console.log captured
2.30.1, Turbopack 500 500 500 n/a, register() throws
2.30.1, webpack 202 200 200 no
this branch, Turbopack 202 200 200 yes
this branch, webpack 202 200 200 yes

On this branch, evlog is absent from the deployed node_modules and no Edge output file contains the logger, on either bundler. Server output after the fix (Turbopack), with the wide event printed once and the route's console.log captured:

{"method":"POST","path":"/api/webhook","requestId":"76317051-fca4-4e22-83bd-cea2377798be","outcome":"ignored","status":202,"durationMs":1,"duration":"1ms","timestamp":"2026-10-10T17:47:25.756Z","level":"info","service":"verify","environment":"production"}
{"timestamp":"2026-10-10T17:47:25.774Z","level":"info","service":"verify","environment":"production","source":"stdout","message":"plain line from a route"}

Tests:

  • test/next/instrumentation-bundle.test.ts bundles a root instrumentation.ts from dist with esbuild (NEXT_RUNTIME defined, next external), runs register() in a temp directory with no node_modules, and checks the Edge bundle leaves the logger out. The Node.js case failed on main with ERR_MODULE_NOT_FOUND.
  • Two captureOutput tests in test/next/instrumentation.test.ts assert the drain receives one event for JSON output and nothing extra for pretty and tagged output. On main the drain received 4 events.

pnpm run lint, pnpm run typecheck (including evlog-telemetry), pnpm run test, pnpm test:coverage and pnpm api:snapshot (no diff) pass locally.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Summary by CodeRabbit

  • Bug Fixes
    • Evlog’s own console output is no longer captured again as structured log events, preventing duplicate logging.
    • Next.js instrumentation now bundles correctly for Node.js deployments without node_modules and works with both Turbopack and webpack.
    • Captured output is displayed once as structured terminal output when silent is disabled; with silent enabled, original output remains available alongside drain delivery.

@vercel

vercel Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated
evi Ready Ready Preview Oct 10, 2026 6:00pm UTC
evlog-docs Ready Ready Preview, v0 Oct 10, 2026 6:00pm UTC
evlog-render-lab Ready Ready Preview Oct 10, 2026 6:00pm UTC
evlog-telemetry Ready Ready Preview Oct 10, 2026 6:00pm UTC
just-use-evlog Ready Ready Preview Oct 10, 2026 6:00pm UTC

Request Review

@github-actions github-actions Bot added the bug Something isn't working label Oct 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@coderabbitai

coderabbitai Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0636d524-aee5-4c39-a0a7-f51e36593ea2

📥 Commits

Reviewing files that changed from the base of the PR and between 49af75e and a5b0112.


📒 Files selected for processing (8)
  • .changeset/capture-output-own-events.md
  • .changeset/next-instrumentation-bundling.md
  • apps/docs/content/4.integrate/frameworks/02.nextjs.md
  • packages/evlog/src/logger.ts
  • packages/evlog/src/next/instrumentation-create.ts
  • packages/evlog/src/next/instrumentation-gate.ts
  • packages/evlog/test/next/instrumentation-bundle.test.ts
  • packages/evlog/test/next/instrumentation.test.ts

 ________________________
< I am the one who bugs. >
 ------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR

🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 10, 2026

Copy link
Copy Markdown
@evlog/cli

npm i https://pkg.pr.new/@evlog/cli@816

evlog

npm i https://pkg.pr.new/evlog@816

@evlog/nuxthub

npm i https://pkg.pr.new/@evlog/nuxthub@816

@evlog/signals

npm i https://pkg.pr.new/@evlog/signals@816

@evlog/telemetry

npm i https://pkg.pr.new/@evlog/telemetry@816

commit: a5b0112

This branch was successfully deployed

5 active deployments
Preview – evlog-docs — a5b0112e Deployed Oct 10, 2026 by vercel[bot]
Preview – evlog-telemetry — a5b0112e Deployed Oct 10, 2026 by vercel[bot]
Preview – evi — a5b0112e Deployed Oct 10, 2026 by vercel[bot]
Preview – evlog-render-lab — a5b0112e Deployed Oct 10, 2026 by vercel[bot]
Preview – just-use-evlog — a5b0112e Deployed Oct 10, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant