Skip to content

Instantly share code, notes, and snippets.

@kaenova
Last active November 21, 2025 04:18
Show Gist options
  • Select an option

  • Save kaenova/e8447317d65df3ac5ae1a348ebf1b65a to your computer and use it in GitHub Desktop.

Select an option

Save kaenova/e8447317d65df3ac5ae1a348ebf1b65a to your computer and use it in GitHub Desktop.
Application Insight Next.js

How to install Open Telemetry

bun install @vercel/otel @opentelemetry/api @azure/monitor-opentelemetry-exporter
// (src/)instrumentation.ts
import type { SpanExporter } from '@opentelemetry/sdk-trace-base';
import { registerOTel } from '@vercel/otel';

export async function register() {
  let traceExporter: SpanExporter | undefined;

  if (!process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
    console.warn('APPLICATION_INSIGHTS_CONNECTION_STRING is not set. Skipping OpenTelemetry registration.');
    return
  }

  if (!process.env.APPLICATIONINSIGHTS_SERVICE_NAME) {
    console.warn('APPLICATION_INSIGHTS_SERVICE_NAME is not set. Skipping OpenTelemetry registration.');
    return
  }

  if (process.env.NEXT_RUNTIME === 'nodejs') {
    const { AzureMonitorTraceExporter } = await import('@azure/monitor-opentelemetry-exporter');
    traceExporter = new AzureMonitorTraceExporter({
      connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
      // you can read from ENV if you prefer to
      // connectionString: process.env.APP_INSIGHTS_CONNECTION_STRING,
    });
  }

  registerOTel({ serviceName: process.env.APPLICATIONINSIGHTS_SERVICE_NAME, traceExporter });
}

For relevancies, usually the APPLICATIONINSIGHTS_SERVICE_NAME is the Azure Resource Name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment