Allow OpenTelemetry span completion during prerendering (#102188)
## Summary
OpenTelemetry SDK processors can read the clock when `span.end()`
records metrics. With Cache Components, those telemetry reads currently
run in the render's work-unit scope and abort background prerendering.
Run span completion outside that scope, extending the existing exemption
for span creation while preserving validation inside application
callbacks.
- Instrument spans returned by `startSpan` and passed to
`startActiveSpan`, including spans created outside a work unit and ended
later during rendering. Preserve the original receiver and end
timestamp, and wrap each span once.
- Add an SDK processor regression covering synchronous and asynchronous
telemetry reads, SDK-on/off controls, both Partial Prefetching settings,
cached-content reuse/revalidation, both span APIs, and a genuine
application-clock guard. Keep deployment coverage through runtime log
support.
- Update the tracing-context comment. Audit the remaining completion
sites in `tracer.ts` and `local-span-recorder.ts`, instrumentation
guides/API references, upgrade guidance, clock diagnostics, and
`with-opentelemetry`; no further changes are needed.
Finding: https://next-maintainer-agent.vercel.tools/agent-feedback/8689
<details>
<summary>Version investigation and related work</summary>
The unchanged Azure Monitor 1.21.0 reproduction logs two span-end
`Date.now()` diagnostics on both 16.4.5 and 16.4.6 after repeated
requests. The 16.4.6 SDK-disabled control logs none. This predates
16.4.6; the first affected version and a working older baseline remain
unknown.
Compared stable `v16.4.5` (`4e5335a0031`) with `v16.4.6`
(`54b876b6b16`): release notes and commits contain no tracing/prerender
changes. The route-handler compression and Turbopack dev-GC backports do
not match the page-render/span-end stack. Compared `v16.5.0-canary.58`
(`495cbfa1c2d`) with `.59` (`5e1f6e34ff5`): the tracing files are
unchanged; `.58` was not runtime-tested.
The original #82350 exemption covers creation. #95317 covers early proxy
tracers; #101597 covers render-error reporters and is included in tested
canary. Open #99521 covers tracers retained during registration. None
covers span completion. The new regression fails on unmodified canary
`794836c3cbb`.
</details>
## Verification
- New production regression: both SDK-enabled cases fail on unmodified
canary; all 8 cases pass with the fix in Turbopack development,
Turbopack production, and Webpack production.
- Pinned Azure reproduction with the fixed local package: repeated
built-route requests and an unbuilt route return HTTP 200 with cached
and request-time content, with Azure registered and zero unstable-clock
diagnostics. No application/configuration source changes or Azure
account are required.
- `pnpm test-start-turbo
test/e2e/app-dir/cache-components-allow-otel-spans` — 4 passed.
- 42 focused unit tests passed, including the existing tracer and
instrumentation-lifecycle suites. The new context-isolation cases fail
against the unmodified extension and pass with the fix.
- `pnpm typescript`, focused Prettier and ESLint checks passed.
- Hosted deployment validation not run.
<!-- NEXT_JS_LLM -->