Getting started
The quickest way to add OpenTelemetry is with the@vercel/otel package.
Using @vercel/otel
1
Install packages
- npm
- pnpm
- yarn
2
Create instrumentation.ts
Create an
instrumentation.ts file in the root of your project (or in /src if you use that layout):The
instrumentation file must be in the project root, not inside app/ or pages/.Manual configuration
For full control over the OpenTelemetry SDK, configure it manually. BecauseNodeSDK is not compatible with the Edge runtime, conditionally import it:
Testing your instrumentation
Use the OpenTelemetry dev environment to test traces locally. When working correctly, you should see a root span labeledGET /requested/pathname with all other spans from that trace nested under it.
To see additional spans:
Custom spans
Add custom spans using the OpenTelemetry API:Default spans
Next.js automatically instruments the following spans:
Span attributes follow OpenTelemetry semantic conventions plus custom
next.* attributes:
next.span_name— Duplicates the span namenext.span_type— Unique identifier for the span typenext.route— Route pattern (e.g.,/[param]/user)next.rsc— Whether the request is an RSC prefetch requestnext.page— Internal path to a special file (e.g.,page.ts,layout.ts)
Set
NEXT_OTEL_FETCH_DISABLED=1 to disable automatic instrumentation of fetch calls if you use a custom fetch instrumentation library.Deployment
Vercel
OpenTelemetry works out of the box on Vercel. Follow the Vercel observability quickstart to connect to an observability provider.Self-hosted
Spin up an OpenTelemetry Collector to receive and process telemetry from your Next.js app, then configure@vercel/otel or a custom exporter to send data to the collector.