feat(next): support instrumentation.ts hooks#188
Open
mnismt wants to merge 2 commits intoHugoRCD:mainfrom
Open
feat(next): support instrumentation.ts hooks#188mnismt wants to merge 2 commits intoHugoRCD:mainfrom
mnismt wants to merge 2 commits intoHugoRCD:mainfrom
Conversation
|
@mnismt is attempting to deploy a commit to the HRCD Projects Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
|
Thank you for following the naming conventions! 🙏 |
Merging this PR will degrade performance by 50.36%
Performance Changes
Comparing Footnotes |
Rename underscore-prefixed function to satisfy camelCase naming convention rule. Also fix space-before-function-paren in instrumentation.ts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
@HugoRCD fixed all CI failures (except CodSpeed) |
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.
🔗 Linked issue
Resolves #187
📚 Description
Following up on #187, I went ahead and dug into the codebase to add support for instrumentation.ts: https://nextjs.org/docs/app/guides/instrumentation
A note on
captureOutput: In dev mode, evlog pretty-prints wide events to stdout. WhencaptureOutput: true, those prints get re-captured as new stdout events, so you see everything twice. This is expected but noisy. Here's how I'd recommend using it:createEvlog()only. SkipcaptureOutput- pretty tree output is already readablecaptureOutput: true+silent: true. evlog stays quiet, capture only catches framework/third-party outputcreateInstrumentation()withoutcaptureOutput. JustonRequestError()for unhandled errorsFor dev, keep using
onRequestError()only - catching unhandled errors from SSR/RSC/middleware thatwithEvlog()can't reach.What this adds:
createInstrumentation()factory atevlog/next/instrumentationthat returns{ register, onRequestError }- matching the Next.js instrumentation signature exactly:register(): initializes the logger with drain at startup.onRequestError(): emits structured error events with digest, stack trace, request path/method, and routing context through the global drain (fire-and-forget)Coexistence with
createEvlog():Both can be used independently or together in the same
lib/evlog.ts.register()callsinitLogger()then_lockLogger(). WhencreateEvlog()later triggersconfigureHandler(), it checksisLoggerLocked()and skips re-initialization.Each can have its own drain. instrumentation handles
onRequestErrorevents,createEvloghandles per-request wide events fromwithEvlog().Edge Runtime:
This was a bit tricky.
Next.js evaluates
instrumentation.tsin both Node.js and Edge runtimes. Two things I had to work around:patchOutput()useglobalThis.processinstead of bareprocess.stdout/process.stderrso the bundler doesn't flag it as Node.js-onlyinstrumentation.tsre-export file to useawait import()gated behindNEXT_RUNTIME === 'nodejs'instead of static re-exports. This prevents Edge from bundling Node.js-only modules (node:async_hooks,node:fs/promises, ...) that come in through the drain and adaptersWithout this, you get
Module not found: Can't resolve 'node:async_hooks'at build time in Edge routes.Tests:
12 tests covering: config, stdout/stderr patching, onRequestError fields, drain flow, re-entrancy guard, Edge safety, idempotency,
enabled: false, defaults, undefined digest.Ran the full suite: 694/694 passing, zero regressions.
Docs:
I added a new Instrumentation section to the Next.js docs:
Feel free to tweak the wording or restructure it, happy to adjust.
Examples
The nextjs playground doesn't have any route so I tested with a few examples below:
1. A not found page
2. RSC crashes
Here's a RSC that crashes during SSR - no withEvlog() wrapper, no route handler:
Without instrumentation, this error only shows up as raw stderr. With
onRequestError()evlog captures it as a structured event with route context and digest:📝 Checklist