A Practical Guide to OpenTelemetry Instrumentation in Next.js

Learn where Next.js initializes OpenTelemetry, how to separate runtime-specific setup, and what it takes to export useful traces.

Company & Product Updates•~7 min read•November 8, 2026•By qz-l team
Looking for related guides? Start with the qz-l Learning Center and explore more tutorials in this topic cluster.

A Practical Guide to OpenTelemetry Instrumentation in Next.js

When a web request is slow, application logs often tell you what happened inside one function, while infrastructure metrics tell you that something is under pressure. Neither necessarily explains how a single request traveled through the application and its dependencies.

OpenTelemetry gives teams a vendor-neutral way to create and export observability data. In a Next.js application, however, adding a package is only one part of the work. Initialization has a prescribed place, the application may run in more than one runtime, and generated telemetry needs somewhere to go.

This guide covers a server-side starting point: where to register instrumentation, how to keep runtime-specific code separate, and how to think about export and data quality. The examples follow the current Next.js and OpenTelemetry documentation; they are not a report of a QZ-L production deployment.

What instrumentation gives you

Instrumentation is code that emits telemetry about an application. A trace represents the path of work associated with an operation; metrics summarize measurements over time; logs record discrete events. Together, these signals can help answer different questions: which dependency added latency, whether the issue affects many requests, and what an individual request reported.

OpenTelemetry's JavaScript documentation currently lists traces and metrics as stable, while logs are in development. It also warns that browser instrumentation is experimental and mostly unspecified. That makes a server-side Node.js setup a more straightforward starting point than assuming a single configuration will work identically in browsers, Node.js, and Edge runtimes.

Instrumentation can be automatic or manual. Automatic instrumentation can capture supported library operations without adding code around every call. Manual instrumentation lets an application add context around domain-specific work that a generic HTTP or database span cannot describe. A useful approach is to begin with the integrations you need, inspect the traces they produce, and add custom context only where it answers a real operational question.

Read a trace as a request tree

A trace is made up of spans. Each span describes a unit of work, with a name, timing, and context. A root span marks the overall operation; child spans represent work nested beneath it. Related spans share a trace ID, and a child refers to its parent span. This hierarchy is what lets an engineer inspect one request as a path rather than as unrelated log lines.

For example, a request might appear as a top-level server operation with child spans for an outbound HTTP call and a database query—if those libraries are instrumented and their context is connected. If the top-level span is present but a dependency operation is missing, that does not prove the dependency was idle. The library may not be instrumented, the runtime may not support the instrumentation, or context may not be propagated across that boundary. Inspect what is actually emitted before drawing conclusions.

Use the Next.js startup hook

For an App Router application, Next.js looks for an instrumentation.ts or instrumentation.js file at the project root. If the project uses a src directory, the file belongs there alongside app and other top-level source directories—not inside app.

The file exports a register function. Next.js calls it once when a new server instance starts and waits for it to finish before serving requests. The Next.js guide demonstrates this minimal registration using @vercel/otel:

npm install @vercel/otel
import { registerOTel } from '@vercel/otel'

export function register() {
  registerOTel('qzl-web')
}

The string is a service name, which helps distinguish this application from other services in telemetry. Choose a stable, meaningful value for your own app. This small hook is an initialization example, not a complete backend configuration: the destination, credentials, and deployment-specific exporter behavior still need to be set up and verified.

Before adding the file to an existing app, check the framework version's instrumentation documentation and the chosen integration package's setup instructions. A copied snippet can initialize the SDK without making any data visible if no usable export path is configured.

Respect runtime boundaries

Next.js may call register in multiple execution environments. The framework documentation recommends importing runtime-specific setup from inside register, guarded by NEXT_RUNTIME. This avoids loading a Node-only module in an Edge environment, or the reverse.

For example, split the implementation into runtime-specific modules:

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./instrumentation-node')
  }

  if (process.env.NEXT_RUNTIME === 'edge') {
    await import('./instrumentation-edge')
  }
}

Only add a branch for a runtime the application actually uses, and verify that the selected instrumentation packages support it. If the application runs only on Node.js, keep the setup focused on Node.js rather than adding an unused Edge path. The important point is that one startup hook does not imply one universal runtime environment.

Instrument the part that matters

Once the SDK is initialized and supported automatic instrumentation is enabled, make a representative request and inspect the resulting trace. Check whether it shows the route, outbound requests, and relevant database work. If an important operation is missing, manual instrumentation can add a span around that operation.

Do not create a span for every helper function. Excessive spans increase volume and make traces harder to interpret. Prefer boundaries that answer a specific question—for example, the time spent resolving a short code or calling a metadata provider. Name spans consistently and use attributes that help diagnose behavior without copying the entire request into telemetry.

That last constraint matters for link-oriented applications. A URL may contain a private token in its path or query string, and application inputs may contain personal or confidential content. Avoid placing raw destinations, query values, credentials, or user-submitted text into span names or attributes. Use bounded, non-sensitive identifiers when correlation is necessary, and decide deliberately which attributes are allowed to leave the application.

Connect instrumentation to a destination

Creating telemetry and collecting it are separate jobs. The OpenTelemetry JavaScript Node.js getting-started guide demonstrates a local example that exports output to the console. That is useful for confirming that instrumentation is producing data, but production systems normally need a configured export path and a backend where engineers can query or visualize the result.

The OpenTelemetry exporter guide recommends sending telemetry through an OpenTelemetry Collector in production. A Collector can receive and process telemetry before forwarding it to a backend. The exact exporter, endpoint, authentication, and processing configuration depends on the chosen service and deployment platform; a framework's registration example cannot fill in those environment-specific details.

Verify the whole path

Before collecting broadly, use a controlled request in a non-production environment and follow it through each boundary:

  1. Initialization: Confirm that the server starts successfully and the registration hook runs in the intended runtime.
  2. Instrumentation: Make one request and inspect whether the expected root span appears. Check the service identity and timestamps.
  3. Relationships: Look for child spans around the library operations you expected to observe. Confirm that they share the request's trace and have sensible parent relationships.
  4. Export: Confirm that the collector or backend receives the trace. Console output alone is only evidence that the local example emitted data, not proof that the production export path works.
  5. Data review: Inspect span names and attributes for credentials, raw URLs, query strings, or personal content. Also look for attributes whose values vary for nearly every request; those can create hard-to-query, high-cardinality data.

If a span is missing, debug from the boundary inward: is the relevant instrumentation package installed and initialized? Does it support the runtime? Does the operation use a library that the instrumentation recognizes? Is telemetry reaching the collector? These checks are more useful than adding more instrumentation at random.

Keep the first rollout deliberately narrow. Instrument one representative route, confirm the exported trace is useful, and establish an acceptable volume and retention policy before expanding. Sampling and filtering decisions should be made with the backend and operational requirements in mind; they should not silently discard the very error or latency cases the team needs to investigate.

Start small, then expand

OpenTelemetry provides shared APIs and conventions, but a successful integration still depends on runtime compatibility, the right instrumentation libraries, and a working exporter. Start with one server runtime and one representative operation. Verify the complete path from startup to backend. Then add manual spans or additional signals only to close gaps that you have actually observed.

That approach makes instrumentation easier to reason about: you know what the app emits, where it sends the data, and what sensitive information it might expose. A small, verified trace is more useful than a large configuration that has never been checked end to end.

References

Related Posts

Agent Host Protocol: Keeping Coding-Agent Sessions in Sync Across Devices

A practical guide to AHP's action, reducer, replay, and channel model for reconnectable cross-device coding-agent sessions.

Database Backup and Recovery: A Practical Pattern for Reliable PostgreSQL Operations

Learn how to design reliable PostgreSQL backups with remote storage, automation, retention, monitoring, and tested database recovery.

GitHub Actions Lockfiles: Keep CI Running the Code You Reviewed

Learn how GitHub Actions workflow lockfiles pin direct and transitive actions, how to adopt them safely, and what they do not cover.