惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
IT之家
IT之家
V
V2EX
Jina AI
Jina AI
V
Visual Studio Blog
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园 - 叶小钗
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
Google Online Security Blog
Google Online Security Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
News and Events Feed by Topic
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
SecWiki News
SecWiki News
博客园_首页
罗磊的独立博客
量子位
Latest news
Latest news
I
Intezer
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Last Week in AI
Last Week in AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News | PayPal Newsroom

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
OpenTelemetry in Next.js: traces that survive the edge/server boundary without losing context
Juan Torchia · 2026-06-17 · via DEV Community

OpenTelemetry in Next.js: traces that survive the edge/server boundary without losing context

Why is observability in Next.js still a half-solved problem in 2025? We've had OpenTelemetry as the de facto backend standard for years — well documented in Spring Boot, plain Node, Go — and yet the App Router has a boundary that silently breaks trace context without raising a single visible error. I've wondered for a while if the community underestimates this because the problem doesn't throw exceptions: it just disappears.

My take is blunt: OpenTelemetry in Next.js works, but it requires explicit propagator configuration. The default silently breaks the trace at the edge/node boundary. If you're coming from Spring Boot where context propagates almost automatically, this is going to catch you off guard.


The real problem: what actually happens at the edge/node boundary

The Next.js App Router runs in two distinct environments that share very little:

  • Edge Runtime: Middleware, some Route Handlers. A trimmed-down environment based on Web APIs, no full Node.js support. Runs in V8 isolates.
  • Node.js Runtime: Server Components, Server Actions, API Routes. Regular Node, with filesystem access, process, all of it.

When a request enters through Middleware (edge) and then hits a Server Component (node), there's an environment transition. If the OpenTelemetry propagator isn't explicitly configured to read and write traceparent and tracestate headers on both sides, the trace gets cut right there. The Middleware span closes with no children. The Server Component starts a brand new trace with no parent. In Jaeger or any collector, you see two orphaned traces where there should be one single chain.

What makes this hard to diagnose: there's no error. No warning. The code runs perfectly. You only notice something's wrong when you look at the collector and the trace IDs don't match.


How to configure OpenTelemetry in Next.js App Router: the instrumentation hook

Next.js exposes a specific entry point for this, documented in the official guide: the instrumentation.ts file at the project root (or inside src/ if that's your structure). This hook runs exactly once when the server starts.

// instrumentation.ts — runs once when the Node.js server starts
import { NodeSDK } from '@opentelemetry/sdk-node'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { W3CTraceContextPropagator } from '@opentelemetry/core'
import { CompositePropagator, W3CBaggagePropagator } from '@opentelemetry/core'
import { Resource } from '@opentelemetry/resources'
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'

export async function register() {
  // Dynamic import: we only initialize in the Node.js runtime.
  // The edge runtime doesn't support the full Node SDK.
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    const sdk = new NodeSDK({
      resource: new Resource({
        [SEMRESATTRS_SERVICE_NAME]: 'my-nextjs-app',
      }),
      traceExporter: new OTLPTraceExporter({
        // Point this at your local collector, Railway, Fly, whatever.
        url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? 'http://localhost:4318/v1/traces',
      }),
      // CRITICAL: the W3C TraceContext propagator is what reads/writes
      // the traceparent and tracestate headers between edge and node.
      // Without this, each environment starts a new trace with no parent.
      textMapPropagator: new CompositePropagator({
        propagators: [
          new W3CTraceContextPropagator(),
          new W3CBaggagePropagator(),
        ],
      }),
    })

    sdk.start()
  }
}

The process.env.NEXT_RUNTIME === 'nodejs' conditional is not optional. If you try to initialize the full NodeSDK in the edge runtime, the build breaks because that environment doesn't have access to the Node APIs the SDK needs. The official docs mention this, but they bury it a bit.


The edge side: propagating context without the full SDK

The edge runtime can't run the NodeSDK. What it can do is read and write headers using the W3C TraceContext primitives. If you're using Middleware for auth or routing, this is the pattern for propagating context to the server:

// middleware.ts — edge runtime, header propagation only
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  const response = NextResponse.next()

  // If there's an incoming traceparent (e.g., from an API gateway),
  // we forward it as-is to the server.
  // If there's none, the Node server will start a new trace — that's correct.
  const traceparent = request.headers.get('traceparent')
  if (traceparent) {
    response.headers.set('traceparent', traceparent)
  }

  const tracestate = request.headers.get('tracestate')
  if (tracestate) {
    response.headers.set('tracestate', tracestate)
  }

  return response
}

export const config = {
  // Apply only to routes that need propagation
  matcher: ['/api/:path*', '/((?!_next/static|_next/image|favicon.ico).*)'],
}

This doesn't generate spans on the edge (you'd need the full SDK for that, which isn't available), but it keeps the context chain alive so the Node.js Runtime can continue the trace from the same trace ID.


The gotchas nobody documents properly

1. instrumentation.ts needs experimental.instrumentationHook enabled in versions before Next.js 15.

In Next.js 15+ it's on by default. If you're on 14, you need this in next.config.ts:

// next.config.ts
const nextConfig = {
  experimental: {
    instrumentationHook: true, // required in Next.js 14 and earlier
  },
}

export default nextConfig

Without this, the instrumentation.ts file sits on disk and never runs. The server starts with no telemetry and no warning about it.

2. Server Actions don't propagate context automatically.

From OpenTelemetry's perspective, a Server Action is a new HTTP request. If you don't explicitly instrument the Action with a manual span, it'll show up as a separate trace in the collector.

// app/actions/create-resource.ts
'use server'

import { trace } from '@opentelemetry/api'

const tracer = trace.getTracer('my-nextjs-app')

export async function createResource(formData: FormData) {
  // We create an explicit child span for the Server Action
  return await tracer.startActiveSpan('server-action.createResource', async (span) => {
    try {
      const name = formData.get('name') as string
      span.setAttribute('resource.name', name)

      // your logic here
      const result = await saveToDb(name)

      span.setStatus({ code: 0 }) // SpanStatusCode.OK
      return result
    } catch (error) {
      span.recordException(error as Error)
      span.setStatus({ code: 2, message: (error as Error).message }) // SpanStatusCode.ERROR
      throw error
    } finally {
      span.end()
    }
  })
}

3. The exporter name matters for the collector.

OTLPTraceExporter over HTTP uses port 4318. If you're using gRPC (direct to Jaeger), you need @opentelemetry/exporter-trace-otlp-grpc and port 4317. Mixing exporters and ports is a classic source of "everything's configured and nothing's arriving at the collector."

4. sdk.start() doesn't wait for collector confirmation.

If the collector isn't available at startup, the SDK doesn't fail with an error — it just silently drops spans. There's an SDK shutdown you can hook into to flush before the process exits:

// Inside register(), after sdk.start()
process.on('SIGTERM', () => {
  sdk.shutdown().finally(() => process.exit(0))
})


Decision checklist: before you instrument your Next.js App Router

Before you start, these are the questions that determine how much work you're actually in for:

Question If the answer is... Implication
Do you have Middleware running on the edge? Yes You need manual header propagation in middleware.ts
Are you using Server Actions with business logic? Yes You need manual spans in each relevant Action
Are you on Next.js 14 or earlier? Yes You need to explicitly enable instrumentationHook
Are you using a collector on Railway/Fly/Docker? Yes Check the port: 4318 for HTTP, 4317 for gRPC
Do you need full end-to-end traces? Yes W3CTraceContextPropagator is mandatory, not optional
Do you only need server-side Node traces? Yes A basic instrumentation.ts is enough, no manual spans

The OpenTelemetry JavaScript SDK documents all available exporters and propagators. The thing the official Next.js docs don't emphasize enough is that the SDK's default propagator is not the W3C TraceContext — and that's exactly what breaks the chain at the edge/node boundary.


What you can't conclude without your own production data

Being honest about the limits of this guide:

  • Latency overhead: There are no verifiable numbers on how much the instrumentation adds to cold starts on Vercel or edge functions. It could be zero, it could be meaningful. You need to measure it in the environment where you deploy.
  • Span volume: On a high-traffic system, the number of spans the NodeSDK automatically generates (Next.js instruments its own internal operations) can be larger than you expect. Sampling is a whole separate topic.
  • Vercel Edge Network compatibility: Header propagation works in theory on any environment that respects the HTTP protocol. Whether it works exactly the same on Vercel Edge, Cloudflare Workers, or your own Node.js server on Railway depends on how each platform handles internal headers.

These are real limits. A guide that doesn't name them isn't being straight with you.


FAQ: OpenTelemetry in Next.js App Router

Can I use OpenTelemetry in the Next.js edge runtime?

Partially. The full NodeSDK doesn't run in the edge runtime because it depends on Node.js APIs that aren't available there. What you can do is manually propagate the traceparent and tracestate headers in Middleware so the Node.js Runtime can continue the trace with the same trace ID. To generate real spans on the edge, you'd need a telemetry library designed specifically for Node-less environments — something that, as of this writing, still doesn't have a mature, official solution in the OTel JS ecosystem.

What propagator do I need so traces survive between Middleware and Server Components?

W3CTraceContextPropagator. This is the propagator that reads and writes the traceparent and tracestate headers from the W3C TraceContext standard, which is the format the modern ecosystem uses to propagate context between services. Without explicitly configuring it in the NodeSDK, the SDK doesn't know how to read the context coming from Middleware and starts a new trace with no parent.

Is OpenTelemetry in Next.js compatible with Jaeger, Grafana Tempo, and similar tools?

Yes, as long as you use the right exporter and point to the right port on the collector. OTLPTraceExporter (HTTP, port 4318) or OTLPTraceExporter with gRPC (port 4317) works with any collector that implements the OTLP protocol: Jaeger, Grafana Tempo, Zipkin (with its specific exporter), Honeycomb, DataDog, etc. The exporter configuration is independent of which collector you use.

Do Server Actions generate spans automatically?

No. Next.js automatically instruments some internal operations (fetch, page rendering, some cache operations), but Server Actions are application code. If you want to trace the logic inside a Server Action, you need to create spans manually using tracer.startActiveSpan() from the OpenTelemetry API.

How do I know if trace context is propagating correctly?

The most direct way is to look at the collector. If you see two separate traces for a request that should be a single chain (for example, a request that goes through Middleware and hits a Server Component), context is breaking. In Jaeger you can search by traceparent in the attributes, or just verify that the trace ID is the same across all spans for a given request.

Do I need instrumentation.ts if my app doesn't use the edge runtime?

If everything runs on the Node.js runtime (no Middleware, no edge routes), instrumentation.ts is enough and the complexity drops significantly. The propagation problem is specific to the edge/node boundary. If you never cross that boundary, Next.js's automatic instrumentation together with the NodeSDK configured with W3CTraceContextPropagator should give you functional traces with minimal extra effort.


My position: instrument early, not when something breaks

When I worked on observability from the backend side in Java with Spring Boot, the advantage was that the framework gave you a lot for free. Next.js is different: the App Router architecture with its edge/node boundary creates a discontinuity that doesn't exist in a classic server, and OpenTelemetry doesn't resolve it automatically.

The uncomfortable part is that the system's silence when context breaks means a lot of people assume their instrumentation is working — until they need to debug something serious in production and discover the traces are fragmented.

My concrete recommendation: if you're using App Router with Middleware, configure W3CTraceContextPropagator from the start and verify in the collector that spans from a single request form a coherent chain before you actually need it. That verification is much cheaper to do in development than to reconstruct it under pressure.

The practical next step: spin up a local Jaeger with Docker (docker run -d --name jaeger -p 16686:16686 -p 4318:4318 jaegertracing/all-in-one:latest), configure the OTLPTraceExporter pointing to http://localhost:4318/v1/traces, and manually verify that a request passing through Middleware and hitting a Server Component shows up as a single trace with chained spans. If you see two traces, the propagator is misconfigured. If you see one, you're good.

That visual check is the proof of concept no guide can replace.


Original sources:


This article was originally published on juanchi.dev