nextjs - 💡(How to fix) Fix Opentelemetry warnings since v16.2.0 [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#91655Fetched 2026-04-08 01:02:43
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Author
Participants
Timeline (top)
subscribed ×3commented ×2issue_type_added ×1

Error Message

Cannot execute the operation on ended Span {traceId: 722ebe96aae7b52fa0afa2f06a5110c0, spanId: 4663e719a6a39b15} Error: Operation attempted on ended Span {traceId: 722ebe96aae7b52fa0afa2f06a5110c0, spanId: 4663e719a6a39b15}

Code Example

Cannot execute the operation on ended Span {traceId: 722ebe96aae7b52fa0afa2f06a5110c0, spanId: 4663e719a6a39b15} Error: Operation attempted on ended Span {traceId: 722ebe96aae7b52fa0afa2f06a5110c0, spanId: 4663e719a6a39b15}

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.3.0: Wed Jan 28 20:51:28 PST 2026; root:xnu-12377.91.3~2/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 24.11.0
  npm: 11.6.1
  Yarn: 1.22.22
  pnpm: 10.20.0
Relevant Packages:
  next: 16.2.0 // Latest available version is detected (16.2.0).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/tmair/otel-issue

To Reproduce

  1. Install the dependencies pnpm install
  2. Build the application
  3. Run pnpm run start
  4. Request the main page under http://localhost:3000/

Current vs. Expected behavior

I expect no warnings from opentelemetry in the console but I get the following warning on each request:

Cannot execute the operation on ended Span {traceId: 722ebe96aae7b52fa0afa2f06a5110c0, spanId: 4663e719a6a39b15} Error: Operation attempted on ended Span {traceId: 722ebe96aae7b52fa0afa2f06a5110c0, spanId: 4663e719a6a39b15}

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.3.0: Wed Jan 28 20:51:28 PST 2026; root:xnu-12377.91.3~2/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 24.11.0
  npm: 11.6.1
  Yarn: 1.22.22
  pnpm: 10.20.0
Relevant Packages:
  next: 16.2.0 // Latest available version is detected (16.2.0).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next start (local)

Additional context

This happens when using a custom server and opentelemetry instrumentation. It seems that the parent span is already closed when trying to add additional attributes onto it.

I suspect that the changes made in #90832 cause this issue. Downgrading to v16.1.7 fixes the issue.

extent analysis

Fix Plan

The fix involves ensuring that the parent span is not closed before adding attributes to it.

  • Update the custom server to properly handle the span lifecycle:
    • Ensure that the span is not ended prematurely.
    • Use tracer.startSpan to create a new span for each request.
    • Use span.setAttribute to add attributes to the span.
    • End the span after the request is processed.

Example code:

const { Tracer } = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');

const provider = new NodeTracerProvider();
const tracer = provider.getTracer('my-tracer');

// Create a new span for each request
const handleRequest = (req, res) => {
  const span = tracer.startSpan('handle-request');
  // Add attributes to the span
  span.setAttribute('request.method', req.method);
  span.setAttribute('request.path', req.path);

  // Process the request
  // ...

  // End the span after the request is processed
  span.end();
  res.send('Hello World!');
};

Verification

To verify that the fix worked, run the application and check the console for warnings. The warning about attempting to execute an operation on an ended span should be gone.

Extra Tips

  • Make sure to properly configure the OpenTelemetry provider and tracer.
  • Use the tracer.startSpan method to create a new span for each request.
  • Use the span.setAttribute method to add attributes to the span.
  • End the span after the request is processed using the span.end method.
  • Consider using a library like @opentelemetry/instrumentation-http to automatically instrument HTTP requests.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING