This is step is required in order to enable instrumentation. Right now, instrumentationHook and serverComponentsExternalPackages are expermiental. Please follow the Official Next.js Documentation for instrumentationHook if this document is outdated.
If you are using a src folder in your Next.js application, create an instrumentation.ts file there. If not, create the file in the root of your project.
instrumentation.ts
import { Infrastack, Protocol } from "@infrastack/otel";export async function register() { Infrastack.init({ serviceName: "YOUR_SERVICE_NAME", protocol: Protocol.HTTP, // Recommended for Vercel });}
If you don’t provide a service name, we will create a random name for you.
5
Run your application
Run your Next.js application with the default command.
npm run dev
You can check your data from the infrastack.ai dashboard.
This is step is required in order to enable instrumentation. Right now, instrumentationHook and serverComponentsExternalPackages are experimental. Please follow the Official Next.js Documentation for instrumentationHook if this document is outdated.
next.config.mjs
const nextConfig = { experimental: { instrumentationHook: true, // To enable instrumentation serverComponentsExternalPackages: [ // To utilize node.js specific features in a next.js environment "@opentelemetry/auto-instrumentations-node", "@opentelemetry/sdk-node", ], },};export default nextConfig;
4
Create instrumentation-node.ts
Here, you will create a file that will be used to instrument your application. You will have lower level control over the instrumentation process.
instrumentation-node.ts
'use strict'import { NodeSDK } from '@opentelemetry/sdk-node';import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';import { Resource } from '@opentelemetry/resources';const traceExporter = new OTLPTraceExporter();const sdk = new NodeSDK({ resource: new Resource({ [ATTR_SERVICE_NAME]: "{{YOUR_SERVICE_NAME}}", [ATTR_SERVICE_VERSION]: "{{YOUR_SERVICE_VERSION}}", }), traceExporter, instrumentations: [ getNodeAutoInstrumentations({ '@opentelemetry/instrumentation-fs': { requireParentSpan: true, }, }) ],});sdk.start();
5
Create instrumentation.ts
If you are using a src folder in your Next.js application, create an instrumentation.ts file there. If not, create the file in the root of your project.
instrumentation.ts
export async function register() { if (process.env.NEXT_RUNTIME === "nodejs") { await import('./instrumentation-node'); }}
6
Run your application
Run your Next.js application with the default command.
npm run dev
You can check your data from the infrastack.ai dashboard.