Getting started
The quickest way to add OpenTelemetry is with the@vercel/otel package.
Using @vercel/otel
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 | Type | Description |
|---|---|---|
[http.method] [next.route] | BaseServer.handleRequest | Root span for each incoming request |
render route (app) [next.route] | AppRender.getBodyResult | Route rendering in the App Router |
fetch [http.method] [http.url] | AppRender.fetch | Fetch request in your code |
executing api route (app) [next.route] | AppRouteRouteHandlers.runHandler | Route Handler execution |
generateMetadata [next.page] | ResolveMetadata.generateMetadata | Metadata generation for a page |
resolve page components | NextNodeServer.findPageComponents | Page component resolution |
resolve segment modules | NextNodeServer.getLayoutOrPageModule | Layout or page module loading |
start response | NextNodeServer.startResponse | Time when first byte is sent |
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.