AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

AppRender.Fetch Spans Never Set Span Status To ERROR For Failed (4Xx/5Xx) Responses

Next.js native fetch instrumentation creates AppRender.fetch spans but does not mark them as ERROR when the upstream response status is 4xx/5xx. This causes OTel-compatible APM backends to record failed backend dependencies as successful, misleading monitoring and alerting.

highConfidence 95%Next.jsAffected V16.3.0-Canary.53

Origin Analysis

The root cause is a missing call to span.setStatus({ code: SpanStatusCode.ERROR }) in packages/next/src/server/lib/patch-fetch.ts after the response status code attribute is set; the code only records the status as an attribute but never propagates it to the span status.
1. git clone https://github.com/anilparlak/otel-repro && cd otel-repro 2. npm install 3. npm run dev 4. In another terminal: curl http://localhost:3000/ 5. In the dev-server console, observe the 'fetch GET .../api/fail' span: it has status code 0 (UNSET) despite upstream HTTP 503.

Fixing Code Block

Edge Case Audit

Setting all 4xx responses as ERROR may increase error rates in APM dashboards, which could be noisy if 4xx codes are used for expected business logic (e.g., 404 for not-found routes). This change is global and applies to next start and next dev. Rollback: if error rates become too noisy, revert the change or adjust the threshold to only status >= 500. Also ensure the @opentelemetry/api package version supports SpanStatusCode (available since 1.0.0). No concurrency or multi-thread issues are expected as spans are per-request, but be cautious with custom span processors that may override status.

Ecosystem Topology