Files
Jiwon ChoiandGitHub fa71595a5e Fix early OTel proxy tracers in Cache Components prerenders (#95317)
> [!TIP]
> Best reviewed with hide whitespace.

### Why?

Cache Components prerenders can fail when an OpenTelemetry tracer is
acquired before provider registration and later resolves through proxy
tracer delegation:

```
Next.js encountered the unstable value `Math.random()` while prerendering
```

Current behavior:
https://github.com/vercel/next.js/actions/runs/28518518762/job/84536475151?pr=95317#step:32:343

### How?

Patch proxy tracer delegation to apply the same Cache Components span
instrumentation used for normal tracer lookup.

Closes https://github.com/vercel/next.js/issues/94753

<!-- NEXT_JS_LLM_PR -->
2026-07-01 18:14:06 +02:00

28 lines
965 B
TypeScript

import { trace, type Tracer } from '@opentelemetry/api'
import { NodeSDK } from '@opentelemetry/sdk-node'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
type TestGlobal = typeof globalThis & {
__nextTestEarlyTracer?: Tracer
}
// Acquire this before sdk.start() to reproduce how instrumentation libraries
// receive a ProxyTracer before the real provider is registered.
;(globalThis as TestGlobal).__nextTestEarlyTracer ??= trace.getTracer(
'next-test-early-tracer'
)
const sdk = new NodeSDK({
serviceName: 'nextjs-otel-app',
traceExporter: new OTLPTraceExporter({
url:
process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ||
'http://localhost:4318/v1/traces',
}),
instrumentations: [new HttpInstrumentation(), new ExpressInstrumentation()],
})
sdk.start()