rembrembdocs

instrumentation.js

Last updated April 23, 2026

The instrumentation.js|ts file is used to integrate observability tools into your application, allowing you to track the performance and behavior, and to debug issues in production.

To use it, place the file in the root of your application or inside a src folder if using one.

Exports

register (optional)

The file exports a register function that is called once when a new Next.js server instance is initiated, and must complete before the server is ready to handle requests. register can be an async function.

instrumentation.ts

JavaScriptTypeScript

import { registerOTel } from '@vercel/otel'
 
export function register() {
  registerOTel('next-app')
}

onRequestError (optional)

You can optionally export an onRequestError function to track server errors to any custom observability provider.

instrumentation.ts

JavaScriptTypeScript

import { type Instrumentation } from 'next'
 
export const onRequestError: Instrumentation.onRequestError = async (
  err,
  request,
  context
) => {
  await fetch('https://.../report-error', {
    method: 'POST',
    body: JSON.stringify({
      message: err.message,
      request,
      context,
    }),
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

Parameters

The function accepts three parameters: error, request, and context.

Types

export function onRequestError(
  error: { digest: string } & Error,
  request: {
    path: string // resource path, e.g. /blog?name=foo
    method: string // request method. e.g. GET, POST, etc
    headers: { [key: string]: string | string[] }
  },
  context: {
    routerKind: 'Pages Router' | 'App Router' // the router type
    routePath: string // the route file path, e.g. /app/blog/[dynamic]
    routeType: 'render' | 'route' | 'action' | 'proxy' // the context in which the error occurred
    renderSource:
      | 'react-server-components'
      | 'react-server-components-payload'
      | 'server-rendering'
    revalidateReason: 'on-demand' | 'stale' | undefined // undefined is a normal request without revalidation
    renderType: 'dynamic' | 'dynamic-resume' // 'dynamic-resume' for PPR
  }
): void | Promise<void>

Specifying the runtime

The instrumentation.js file works in both the Node.js and Edge runtime, however, you can use process.env.NEXT_RUNTIME to target a specific runtime.

instrumentation.js

export function register() {
  if (process.env.NEXT_RUNTIME === 'edge') {
    return require('./register.edge')
  } else {
    return require('./register.node')
  }
}
 
export function onRequestError() {
  if (process.env.NEXT_RUNTIME === 'edge') {
    return require('./on-request-error.edge')
  } else {
    return require('./on-request-error.node')
  }
}

Version History

VersionChanges
v15.0.0onRequestError introduced, instrumentation stable
v14.0.4Turbopack support for instrumentation
v13.2.0instrumentation introduced as an experimental feature

Learn more about Instrumentation

[

Instrumentation

Learn how to use instrumentation to run code at server startup in your Next.js app

](../../../guides/instrumentation/index.md)

Was this helpful?