Skip to content

Next.js App Router OpenTelemetry Logging: Complete Setup Guide

Most Next.js OpenTelemetry guides configure traces and forget /v1/logs. Here is the clean App Router pattern that ships structured logs correlated with active trace IDs.

Exla Engineering··5 min read

OTLP/HTTP

Works on Vercel, Fly, ECS & Cloud Run

0 Edge bloat

Clean NEXT_RUNTIME === 'nodejs' split

60 sec

Automated setup via npx libra-logs

Key takeaway

Gate your OpenTelemetry LoggerProvider inside instrumentation.node.ts (NEXT_RUNTIME === 'nodejs') and export over OTLP/HTTP, or let npx libra-logs wire it on a clean git branch in 60 seconds.

Why @vercel/otel alone does not persist your application logs

@vercel/otel exports HTTP request spans, while console.error and Pino logs inside Server Actions still vanish after Vercel's short runtime retention unless a LoggerProvider ships /v1/logs.

Step 1: Gate instrumentation.ts for the Node.js runtime

Next.js bundles instrumentation.ts for both Node and Edge. Dynamic import keeps Node exporters out of Edge middleware builds.

src/instrumentation.ts
export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    await import("./instrumentation.node");
  }
}

Step 2: Export batched logs over OTLP/HTTP

OTLP/HTTP avoids native gRPC binary issues in serverless functions and flushes structured customer attributes asynchronously.

src/instrumentation.node.ts
import { logs, SeverityNumber } from "@opentelemetry/api-logs";
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { BatchLogRecordProcessor, LoggerProvider } from "@opentelemetry/sdk-logs";

const exporter = new OTLPLogExporter({
  url: process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT ?? "https://libra-logs-api.fly.dev/logs/api/v1/logs",
  headers: { Authorization: `Bearer ${process.env.LIBRA_LOGS_INGEST_KEY ?? ""}` },
});

const provider = new LoggerProvider({
  resource: resourceFromAttributes({
    "service.name": process.env.OTEL_SERVICE_NAME ?? "nextjs-web",
    "deployment.environment": process.env.NODE_ENV ?? "production",
  }),
  processors: [new BatchLogRecordProcessor(exporter, { scheduledDelayMillis: 1500 })],
});

logs.setGlobalLoggerProvider(provider);

Skip boilerplate: install in one prompt for $20/mo

Paste the Libra setup prompt into Claude Code, Cursor, or Codex (or run npx libra-logs). It wires your Next.js app on a branch, confirms your first log line arrived, and gives you 25 GB/mo on the $20/mo plan ($150/mo for 3-9 employees).

Simple pricing · 30-day free trial

Stop paying per indexed log event. Group bugs by customer instead.

$20/mo

1-2 employees · up to 25 GB

$150/mo

3-9 employees · up to 100 GB + Slack alerts

10+ staff

We beat your Datadog/Sentry bill

Talk to sales

FAQ

Common engineering questions

Does BatchLogRecordProcessor add latency to Next.js Server Actions?

No. Records buffer in memory and flush asynchronously over background HTTP batches.

Can I send traces and logs to the same vendor-neutral OTLP setup?

Yes. Standard OTEL_EXPORTER_OTLP_* environment variables let you switch or dual-ship endpoints anytime without changing app code.