Capítulo 371 de 456
instrumentation.js|ts at the project (or src) root integrates observability tooling: run setup code once at server start, and capture server errors centrally.
register() (optional): async function called once when a new Next.js server instance starts, must complete before requests are handled.onRequestError() (optional): tracks server errors to a custom observability provider; receives (error, request, context).process.env.NEXT_RUNTIME: use to branch logic between 'edge' and Node.js runtimes, since the file runs in both.import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel('next-app')
}
export function onRequestError(
error: unknown,
request: {
path: string
method: string
headers: { [key: string]: string | string[] }
},
context: {
routerKind: 'Pages Router' | 'App Router'
routePath: string
routeType: 'render' | 'route' | 'action' | 'proxy'
renderSource: 'react-server-components' | 'react-server-components-payload' | 'server-rendering'
revalidateReason: 'on-demand' | 'stale' | undefined
renderType: 'dynamic' | 'dynamic-resume'
}
): void | Promise<void>
onRequestError's parameters.None beyond the type signature above.
onRequestError: unawaited tasks may be dropped since Next.js doesn't wait for them.error is always an Error instance: it's typed unknown and may be processed by React; check digest for the real error type.src/ alongside pages/app.register must fully complete before the server accepts requests.NEXT_RUNTIME to load runtime-specific implementations (edge vs Node).context.routeType can be 'proxy', tying error tracking to proxy-originated errors.