Next.js Tracing
If you’re using Next.js, you have two options for setting up OpenTelemetry instrumentation:
Option 1: Using Vercel’s OTEL Setup
If you’re deploying to Vercel, you can use their built-in OpenTelemetry setup:
- Install the required dependencies:
npm install @opentelemetry/api @vercel/otel
- Create an instrumentation file at the root of your project (or in the src folder if using one):
instrumentation.ts
import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel({ serviceName: 'your-project-name' })
}
Option 2: Using Custom Exporters
If you’re using other observability tools (like Langfuse), you can configure a custom exporter:
- Install the required dependencies (example using Langfuse):
npm install @opentelemetry/api langfuse-vercel
- Create an instrumentation file:
instrumentation.ts
import {
NodeSDK,
ATTR_SERVICE_NAME,
Resource,
} from '@mastra/core/telemetry/otel-vendor';
import { LangfuseExporter } from 'langfuse-vercel';
export function register() {
const exporter = new LangfuseExporter({
// ... Langfuse config
})
const sdk = new NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: 'ai',
}),
traceExporter: exporter,
});
sdk.start();
}
Next.js Configuration
For either option, enable the instrumentation hook in your Next.js config:
next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
experimental: {
instrumentationHook: true // Not required in Next.js 15+
}
};
export default nextConfig;
Mastra Configuration
Configure your Mastra instance:
mastra.config.ts
import { Mastra } from "@mastra/core";
export const mastra = new Mastra({
// ... other config
telemetry: {
serviceName: "your-project-name",
enabled: true
}
});
This setup will enable OpenTelemetry tracing for your Next.js application and Mastra operations.
For more details, see the documentation for: