Q27: What is the instrumentation.js file used for in Next.js?

The instrumentation.js file allows executing server initialization logic, performance monitoring setup, or observability tools (like OpenTelemetry, Sentry, or Datadog) as soon as the Next.js Node.js server starts up.

Registering OpenTelemetry in instrumentation.js
// instrumentation.js (Root directory)
export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./telemetry-node');
  }
}
💡 register() executes once when the Next.js server instance launches.
Key Takeaways
  • Executes global server startup logic inside register() function.
  • Integrates APM observability tools (OpenTelemetry, Sentry) early.
Knowledge Check

Inside which exported function within instrumentation.js do you place server startup monitoring setup?