Hard15 minNext.js Fundamentals
UpdatedAug 3, 2026
Edit

Instrumentation & Monitoring (OpenTelemetry)

CONCEPTS:Instrumentation and Monitoring

Question Variations

  • "What is the purpose of the `instrumentation.ts` file in Next.js?"
  • "How would you integrate a monitoring tool like Sentry or New Relic into a modern Next.js app?"
  • "How does Next.js support OpenTelemetry?"
  • "Why would you use the `register` function instead of just initializing tools in a layout?"

Why This Is Asked

At scale, knowing “it works” isn’t enough. You need to know how well it works. Next.js’s support for instrumentation.ts and OpenTelemetry is a powerful feature for enterprise-level applications. Interviewers want to see if you can think beyond the code and into the operations and observability of the system.

Key Concepts

  • The register function: How it runs on server startup.
  • OpenTelemetry (OTEL): What it is and how Next.js supports it.
  • Tracing: Tracking a request across multiple services.
  • next.config.js integration: Enabling the instrumentationHook.

Question Variations

  • “What is the purpose of the instrumentation.ts file in Next.js?”
  • “How would you integrate a monitoring tool like Sentry or New Relic into a modern Next.js app?”
  • “How does Next.js support OpenTelemetry?”
  • “Why would you use the register function instead of just initializing tools in a layout?”

Answers by Technology

+ Add Variant
Next.jsImprove this answer ✏️

Expected Answer

Next.js provides an instrumentation.ts (or .js) file that allows you to hook into the Next.js server lifecycle.

Key Features:

  • The register() function: You export a function named register from this file. It is called exactly once when the server starts.
  • Environment Awareness: The file can be used to set up different monitoring for different runtimes (Node.js vs. Edge).
  • OpenTelemetry: Next.js has first-class support for OTEL. You can enable it in next.config.js (experimental.instrumentationHook: true) and use the register function to initialize an OTEL SDK.

Why It Matters

Using instrumentation.ts is the cleanest way to set up global monitoring. Before this feature, developers often had to “hack” layout files or use custom servers, which was brittle and didn’t work well with the App Router’s execution model.

Example Code

Enabling in Config

// next.config.js
module.exports = {
  experimental: {
    instrumentationHook: true,
  },
}

The Instrumentation File

// instrumentation.ts
export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    const { NodeSDK } = await import('@opentelemetry/sdk-node');
    // Initialize your monitoring tool here
  }
}

Common Mistakes

  • Forgetting the config flag: instrumentation.ts will not run unless experimental.instrumentationHook is true.
  • Putting too much logic in register: This function blocks the server from starting until it completes. It should only be used for initialization.
  • Confusing it with Middleware: Middleware runs on every request; instrumentation runs once on startup.

Follow-up Questions

  • Can you use instrumentation.ts in the Edge runtime? (Answer: Yes, you can check process.env.NEXT_RUNTIME to run runtime-specific code).
  • What is a “Span” in OpenTelemetry? (Answer: A span represents a single operation within a trace, like a database query or an HTTP request).

References