feat(instrumentation): warn when Next.js is loaded before dd-trace#8901
feat(instrumentation): warn when Next.js is loaded before dd-trace#8901BridgeAR wants to merge 2 commits into
Conversation
Initializing dd-trace after Next.js has already loaded — e.g. from Next's
instrumentation.ts register() hook, which runs once the server modules are
required — leaves the next integration silently disabled: the require hook
never sees next-server.js, the plugin never attaches, and tracer.use('next')
is a no-op with no diagnostic. ESM apps hit this more often, since a top-level
import does not guarantee dd-trace evaluates before the framework.
checkForRequiredModules already detects packages loaded before dd-trace, but
its call and its log sink were both DD_TRACE_DEBUG-only, so affected users saw
nothing. The same scan now also collects a curated set (Next.js, matched on its
server module) and surfaces it by default via the startup-log path (gated on
startupLogs); the broad list stays DD_TRACE_DEBUG-only.
Fixes: #5430
Overall package sizeSelf size: 6.2 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.2 | 85.93 kB | 825.11 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
This comment has been minimized.
This comment has been minimized.
BenchmarksBenchmark execution time: 2026-06-12 16:42:06 Comparing candidate commit 35b11c3 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 1448 metrics, 19 unstable metrics.
|
require.cache keys use the platform separator, so on Windows (...\node_modules\next\...) extractPackageAndModulePath never matched node_modules/ and the framework scan silently collected nothing. Normalize the key to forward slashes before parsing so the Next.js late-load warning fires on Windows too.
Summary
Initializing dd-trace after Next.js has already loaded — e.g. from Next's
instrumentation.tsregister()hook, which runs once the server modules arerequired — leaves the
nextintegration silently disabled: the require hooknever sees
next-server.js, the plugin never attaches, andtracer.use('next')is a no-op with no diagnostic. ESM apps hit this more often, since a top-level
importdoes not guarantee dd-trace evaluates before the framework.checkForRequiredModulesalready detects packages loaded before dd-trace, butits call and its log sink were both
DD_TRACE_DEBUG-only, so affected users sawnothing. The same scan now also collects a curated set (Next.js, matched on its
server module) and surfaces it by default via the startup-log path (gated on
startupLogs), naming the fix — load dd-trace first via--require dd-trace/initor
--import dd-trace/initialize.mjsand keep it inserverExternalPackages.The broad list stays
DD_TRACE_DEBUG-only.Test plan
DD_TRACE_DEBUG): warns whennext-server.jsis cached before init; silent when next loads after init or only a non-server
file is cached.
logLateLoadedFrameworksgating + drain idempotency; existingexpressdebug-order tests unchanged.
Fixes: #5430