Observability startup
Initialize tracing or metrics providers before requests are served.
Next.js 16+ · Directive
Next.js instrumentation runs server-startup code for observability and logging. Export register from a root-level instrumentation.ts/js file, or from src when using that layout.
Next.js discovers an instrumentation.ts or instrumentation.js file at the project root, or alongside app and pages in src. It invokes the exported register function once when each server instance starts and waits for it to finish before accepting requests. Use this hook to initialize monitoring, logging, or other server-startup integrations; inspect NEXT_RUNTIME when setup differs between Node.js and Edge.
export async function register(): Promise<void> { /* initialization */ }export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./instrumentation.node')
}
}
Create this file at the project root, or in src when that folder is used.
Initialize tracing or metrics providers before requests are served.
Configure structured server logging during application boot.
Load different integrations for Node.js and Edge deployments.
Create instrumentation.ts or instrumentation.js in the project root, or in src when using that folder. Export register; Next.js calls it once as each server instance starts and waits for completion before serving requests.
Follow-up: How can startup code select a runtime? Check process.env.NEXT_RUNTIME and conditionally initialize Node.js- or Edge-specific integrations.
The register hook is intended for integrating monitoring and logging tools. Initialization belongs in the hook so it runs during server startup rather than per request.
Follow-up: How would you add OpenTelemetry? Initialize the provider or SDK from instrumentation.ts, using the dedicated OpenTelemetry guidance for the integration details.
The convention is instrumentation.ts or instrumentation.js at the root of the application, with src as the alternative location when that directory is used. It must not be nested under app or pages.
Follow-up: What should be checked if the hook is not discovered? Verify the filename, directory, and that register is exported from the file.
registeronRequestErrorprocess.env.NEXT_RUNTIMEOpenTelemetryinstrumentation-client.js|ts