instrumentation.js|ts file integrates observability tools into your application, enabling performance tracking, behavior monitoring, and production debugging.
Place the file at the root of your project, or inside a src folder if you use one.
instrumentation.ts
Exports
register (optional)
Called once when a new Next.js server instance starts. Must complete before the server handles any requests. Can be async.
instrumentation.ts
onRequestError (optional)
Called whenever the Next.js server captures a request error. Use it to report errors to any custom observability provider.
instrumentation.ts
If you run async tasks in
onRequestError, make sure to await them. The function is invoked when Next.js captures the error, so unresolved promises may be lost.Parameters
The caught error. Always an
Error instance. The digest property is a unique ID for the error instance, useful for correlating with server logs.The error may not be the original thrown instance — React may process Server Component errors. Use digest to identify the original error type.Read-only information about the request that caused the error.
Context about where the error occurred.
Targeting a specific runtime
The file runs in both Node.js and Edge runtimes. Useprocess.env.NEXT_RUNTIME to conditionally load runtime-specific code:
instrumentation.js
Examples
OpenTelemetry setup
instrumentation.ts
Reporting errors to Sentry
instrumentation.ts
Custom error logging
instrumentation.ts
Version history
| Version | Changes |
|---|---|
v15.0.0 | onRequestError introduced; instrumentation stable |
v14.0.4 | Turbopack support for instrumentation |
v13.2.0 | instrumentation introduced as experimental |
