Under The Hood
frontend nextjs Next.js 14+ / 15

instrumentation.ts: Startup Hooks & OpenTelemetry Tracing Spans

Last updated
Prerequisites:
Distributed tracing basics
OpenTelemetry (OTel) core concepts
  • nextjs
  • opentelemetry
  • instrumentation
  • tracing
  • monitoring
  • apm

Read at your depth

The practical view

`instrumentation.ts` (placed in the root of your project) exports a `register()` function that Next.js runs once whenever a new server instance boots up. It is the official place to initialize monitoring SDKs (like Sentry, Datadog, or OpenTelemetry), connect to database pools, or set up tracing spans without polluting your UI code.

The same idea in other frameworks

react equivalent
Custom APM init scripts executed before `ReactDOM.hydrateRoot()`.

Legacy vs modern

Ad-hoc Global Logging vs Centralized OpenTelemetry Registration

Importing telemetry in root layouts causes multiple initializations; `instrumentation.ts` guarantees clean single-pass server boot execution.

before → after
Layout-level Initialization
if (!global.tracer) initTracer(); // ❌ OLD: Pollutes render path and runs per-request
instrumentation.ts Lifecycle Hook
export async function register() { await initOpenTelemetry(); } // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of multi-runtime execution (Edge vs Node.js).

The mechanical answer

`instrumentation.ts` is compiled for *all* runtimes used by Next.js, including the Edge Runtime. Because heavy monitoring packages (like `@opentelemetry/sdk-node`) rely on Node.js core modules (`fs`, `net`, `async_hooks`), static imports would fail in the Edge environment. Using dynamic `import()` inside a `process.env.NEXT_RUNTIME === 'nodejs'` conditional ensures Node packages are only loaded when running on the Node.js server.

Trap

Top-level static imports of Node.js modules in `instrumentation.ts` will crash your Edge Runtime routes during build.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.