Docs
DocumentationQuery ReferenceAPI Reference
Open Console→→
DocumentationQuery ReferenceAPI Reference

Platform overview

What is Axiom?QuickstartArchitectureFeatures
Fundamentals
Datasets
Edge deployments
Limits
Performance
Optimize usage
Requirements
Semantic conventions
Glossary
Tour
SecurityRoadmap

Send data

Reference architecturesMethods

Understand data

Console
Query
Builder
Editor
Query results
Visualize
Traces
Metrics
Correlations
Save queries
Stream
Dashboard
Create
Elements
Create
Configure
Element types
Gauge
Heatmap
Log stream
Monitor list
Note
Pie chart
Scatter plot
Statistic
Table
Time series
Sections
Configure
Filter
Annotate
Monitor
Overview
View status
Configure
Examples
Monitor types
Anomaly
Match
Threshold
Alerting
Overview
Configure
Notifier types
Custom Webhook
Discord
Email
Microsoft Teams
Opsgenie
PagerDuty
Slack
Manage
Datasets
Overview
Views
Virtual fields
Access
RBAC
Tokens
CLI
Organization
Audit log
Settings
Usage and billing
Profile
Extend
Overview
AWS Lambda
AWS PrivateLink
Cloudflare Workers
Cloudflare Logpush
Convex
Grafana
Hex
Netlify
Supabase
Tailscale
Terraform
Unkey
Vercel
Intelligence
Overview
Spotlight
AI agents
Overview
MCP Server
Overview
Tools
Query cost limits
Agent-created orgs
Skills
Overview
Axiom alerting
Build dashboards
Control costs
Query metrics
SRE
Translate SPL to APL
Splunk
Overview
Splunk app
Install and configure
Commands
Examples
Portal
How it works
Set up standard mode
Set up transparent mode
Observability Cloud
SPL command support
Examples
Monitor and troubleshoot

Use cases

ObservabilityProduct analytics
LLM observability
Overview
Use Axiom AI SDK
Manual instrumentation
GenAI attributes
Redaction policies

Miscellaneous

LLMs
Overview
List of docs pages
Full docs
Query reference
FAQs
Legal
Acceptable use policy
Cookies
Data processing
HIPAA
Partner agreement
Partner program guide
Privacy policy
SLA
Terms of service
Terms of use

OpenTelemetry using Next.js

This guide demonstrates how to configure OpenTelemetry in a Next.js app to send telemetry data to Axiom.

OpenTelemetry provides a standardized way to collect and export telemetry data from your Next.js apps. This guide walks you through the process of configuring OpenTelemetry in a Next.js app to send traces to Axiom using the OpenTelemetry SDK.

Prerequisites

  • Create an Axiom account.
  • Create a dataset in Axiom where you send your data.
  • Create an API token in Axiom with permissions to create, read, update, and delete datasets.
  • Install Node.js version 14 or newer.
  • An existing Next.js app. Alternatively, create a new app with the default settings using the Next.js documentation.

Send data from new project

Initial setup

  1. Create a new app with the default settings using the Next.js documentation.

  2. Run the following command to install the dependencies:

    shell
    npm install @opentelemetry/exporter-trace-otlp-http @opentelemetry/sdk-trace-node @opentelemetry/resources
  3. Create an instrumentation.ts file in the src folder of your project with the following content:

    typescript
    import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
    import { resourceFromAttributes } from "@opentelemetry/resources";
    import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
    import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
    import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
    
    export function register() {
      const provider = new NodeTracerProvider({
        resource: resourceFromAttributes({
          [ATTR_SERVICE_NAME]: "nextjs-otel-example",
        }, {
          // Use the latest schema version
          // Info: https://opentelemetry.io/docs/specs/semconv/
          schemaUrl: 'https://opentelemetry.io/schemas/1.37.0',
        }),
        spanProcessors: [new SimpleSpanProcessor(
          new OTLPTraceExporter({
            url: `https://${process.env.AXIOM_HOST}/v1/traces`,
            headers: {
              Authorization: `Bearer ${process.env.AXIOM_TOKEN}`,
              "X-Axiom-Dataset": `${process.env.AXIOM_DATASET}`,
            },
          })
        )],
      });
    
      provider.register();
    }
  4. Add the AXIOM_DOMAIN, API_TOKEN, and DATASET_NAME environment variables to your .env file. For example:

    shell
    AXIOM_HOST="AXIOM_DOMAIN"
    AXIOM_TOKEN="API_TOKEN"
    AXIOM_DATASET="DATASET_NAME"

Update root layout

In the /src/app/layout.tsx file, import and call the register function from the instrumentation module:

TSX
import { register } from '../instrumentation';

register();

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

This file sets up the root layout for your Next.js app and initializes the OpenTelemetry instrumentation by calling the register function.

Update compiler options

Add the following options to your tsconfig.json file to ensure compatibility with OpenTelemetry and Next.js:

JSON
{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

This file configures the TypeScript compiler options for your Next.js app.

Observe traces in Axiom

Use the following command to run your Next.js app with OpenTelemetry instrumentation in development mode:

shell
npm run dev

This command starts the Next.js development server, and the OpenTelemetry instrumentation automatically collects traces. As you interact with your app, traces are sent to Axiom where you can monitor and analyze your app’s performance and behavior.

In Axiom, go to the Stream tab and click your dataset. This page displays the traces sent to Axiom and lets you monitor and analyze your app’s performance and behavior.

Go to the Dashboards tab and click OpenTelemetry Traces. This pre-built traces dashboard provides further insights into the performance and behavior of your app.

Send data from existing project

Manual instrumentation

Manual instrumentation allows you to create, configure, and manage spans and traces, providing detailed control over telemetry data collection at specific points within the app.

  1. Set up and retrieve a tracer from the OpenTelemetry API. This tracer starts and manages spans within your app components or API routes.
javascript
import { trace } from '@opentelemetry/api';
const tracer = trace.getTracer('nextjs-app');
  1. Manually start a span at the beginning of significant operations or transactions within your Next.js app and ensure you end it appropriately. This approach is for tracing specific custom events or operations not automatically captured by instrumentations.
javascript
const span = tracer.startSpan('operationName');
try {
  // Perform your operation here
} finally {
  span.end();
}
  1. Enhance the span with additional information such as user details or operation outcomes, which can provide deeper insights when analyzing telemetry data.
javascript
span.setAttribute('user_id', userId);
span.setAttribute('operation_status', 'success');

Automatic instrumentation

Automatic instrumentation uses the capabilities of OpenTelemetry to automatically capture telemetry data for standard operations such as HTTP requests and responses.

  1. Use the OpenTelemetry Node SDK to configure your app to automatically instrument supported libraries and frameworks. Set up NodeSDK in an instrumentation.ts file in your project.
typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';

export function register() {
  const sdk = new NodeSDK({
    resource: new Resource({ [SEM_RESOURCE_ATTRIBUTES.SERVICE_NAME]: 'nextjs-app' }),
    spanProcessor: new BatchSpanProcessor(
      new OTLPTraceExporter({
        url: `https://${process.env.AXIOM_HOST}/v1/traces`,
        headers: {
          Authorization: `Bearer ${process.env.AXIOM_TOKEN}`,
          'X-Axiom-Dataset': `${process.env.AXIOM_DATASET}`,
        },
      })
    ),
  });

  sdk.start();
}
  1. Include necessary OpenTelemetry instrumentation packages to automatically capture telemetry from Node.js libraries like HTTP and any other middlewares used by Next.js.

  2. Call the register function from the instrumentation.ts within your app startup file or before your app starts handling traffic to initialize the OpenTelemetry instrumentation.

javascript
// In pages/_app.js or an equivalent entry point
import { register } from '../instrumentation';
register();

Reference

List of OpenTelemetry trace fields

Field CategoryField NameDescription
General Trace Information
_rowIdUnique identifier for each row in the trace data.
_sysTimeSystem timestamp when the trace data was recorded.
_timeTimestamp when the actual event being traced occurred.
trace_idUnique identifier for the entire trace.
span_idUnique identifier for the span within the trace.
parent_span_idUnique identifier for the parent span within the trace.
HTTP Attributes
attributes.http.methodHTTP method used for the request.
attributes.http.status_codeHTTP status code returned in response.
attributes.http.routeRoute accessed during the HTTP request.
attributes.http.targetSpecific target of the HTTP request.
Custom Attributes
attributes.custom["next.route"]Custom attribute defining the Next.js route.
attributes.custom["next.rsc"]Indicates if React Server Components are used.
attributes.custom["next.span_name"]Custom name of the span within Next.js context.
attributes.custom["next.span_type"]Type of the Next.js span, describing the operation context.
Resource Process Attributes
resource.process.pidProcess ID of the Node.js app.
resource.process.runtime.descriptionDescription of the runtime environment. For example, Node.js.
resource.process.runtime.nameName of the runtime environment. For example, nodejs.
resource.process.runtime.versionVersion of the runtime environment For example, 18.17.0.
resource.process.executable.nameExecutable name running the process. For example, next-server.
Resource Host Attributes
resource.host.archArchitecture of the host machine. For example, arm64.
resource.host.nameName of the host machine. For example, MacBook-Pro.local.
Operational Details
durationTime taken for the operation.
kindType of span (for example, server, internal).
nameName of the span, often a high-level title for the operation.
Scope Attributes
scope.nameName of the scope for the operation. For example, next.js.
scope.versionVersion of the scope. For example, 0.0.1.
Service Attributes
service.nameName of the service generating the trace. For example, nextjs-app.
Telemetry SDK Attributes
telemetry.sdk.languageLanguage of the telemetry SDK. For example, nodejs.
telemetry.sdk.nameName of the telemetry SDK. For example, opentelemetry.
telemetry.sdk.versionVersion of the telemetry SDK. For example, 1.23.0.

List of imported libraries

@opentelemetry/api The core API for OpenTelemetry in JavaScript, providing the necessary interfaces and utilities for tracing, metrics, and context propagation. In the context of Next.js, it allows developers to manually instrument custom spans, manipulate context, and access the active span if needed.

@opentelemetry/exporter-trace-otlp-http This exporter enables your Next.js app to send trace data over HTTP to any backend that supports the OTLP (OpenTelemetry Protocol), such as Axiom. Using OTLP ensures compatibility with a wide range of observability tools and standardizes the data export process.

@opentelemetry/resources This defines the Resource which represents the entity producing telemetry. In Next.js, Resources can be used to describe the app (for example, service name, version) and are attached to all exported telemetry, aiding in identifying data in backend systems.

@opentelemetry/sdk-node

The OpenTelemetry SDK for Node.js which provides a comprehensive set of tools for instrumenting Node.js apps. It includes automatic instrumentation for popular libraries and frameworks, as well as APIs for manual instrumentation. In the Next.js setup, it’s used to configure and initialize the OpenTelemetry SDK.

@opentelemetry/sdk-trace-node This package provides the Node.js-specific implementation of the OpenTelemetry Tracing SDK. It includes the core components needed to create and manage spans, as well as utilities for automatic and manual instrumentation in Node.js environments. In a Next.js app, it works alongside @opentelemetry/sdk-node to give finer control over the tracing pipeline. For example, configuring span processors, sampling strategies, or custom span exporters directly for trace data. It’s useful when you need more granular control over tracing behavior beyond the default SDK configuration.

@opentelemetry/semantic-conventions A set of standard attributes and conventions for describing resources, spans, and metrics in OpenTelemetry. By adhering to these conventions, your Next.js app’s telemetry data becomes more consistent and interoperable with other OpenTelemetry-compatible tools and systems.

Was this page helpful?
Suggest edits on GitHub
On this page
PrerequisitesSend data from new projectInitial setupUpdate root layoutUpdate compiler optionsObserve traces in AxiomSend data from existing projectManual instrumentationAutomatic instrumentationReferenceList of OpenTelemetry trace fieldsList of imported libraries