feature request: improve error in case instrumented modules are loaded before dd-trace-js using ESM
Tracer Version(s)
5.67.0
Node.js Version(s)
24.8.0
Bug Report
After bumping to the latest version, I am seeing this on our logs:
TypeError: Cannot read properties of undefined (reading 'finish')
at WSReceiverPlugin.end (src/receiver.js:65:14)
at plugins/tracing.js:77:22
at wrappedHandler (plugins/plugin.js:81:24)
at Subscription._handler (plugins/plugin.js:15:9)
at Channel.publish (node:diagnostics_channel:143:9)
at node:diagnostics_channel:327:13
at run (node:diagnostics_channel:155:14)
at DatadogStorage.run (src/storage.js:79:22)
at node:diagnostics_channel:92:18
at Channel.runStores (node:diagnostics_channel:164:12)
at TracingChannel.traceSync (node:diagnostics_channel:317:18)
at WebSocket.wrappedMessageHandler (src/ws.js:75:23)
at WebSocket.emit (node:events:518:28)
at Receiver.receiverOnMessage (ws/lib/websocket.js:1220:20)
at Receiver.emit (node:events:518:28)
at Receiver.dataMessage (ws/lib/receiver.js:596:14)
at Receiver.getData (ws/lib/receiver.js:496:10)
at Receiver.startLoop (ws/lib/receiver.js:167:16)
at Receiver._write (ws/lib/receiver.js:94:10)
at writeOrBuffer (streams/writable:572:12)
at _write (streams/writable:501:10)
at Writable.write (streams/writable:510:10)
This is followed by the following error:
Error: Error in plugin handler:
at wrappedHandler (plugins/plugin.js:83:16)
at Subscription._handler (plugins/plugin.js:15:9)
at Channel.publish (node:diagnostics_channel:143:9)
at node:diagnostics_channel:327:13
at run (node:diagnostics_channel:155:14)
at DatadogStorage.run (src/storage.js:79:22)
at node:diagnostics_channel:92:18
at Channel.runStores (node:diagnostics_channel:164:12)
at TracingChannel.traceSync (node:diagnostics_channel:317:18)
at WebSocket.wrappedMessageHandler (src/ws.js:75:23)
at WebSocket.emit (node:events:518:28)
at Receiver.receiverOnMessage (ws/lib/websocket.js:1220:20)
at Receiver.emit (node:events:518:28)
at Receiver.dataMessage (ws/lib/receiver.js:596:14)
at Receiver.getData (ws/lib/receiver.js:496:10)
at Receiver.startLoop (ws/lib/receiver.js:167:16)
at Receiver._write (ws/lib/receiver.js:94:10)
at writeOrBuffer (streams/writable:572:12)
at _write (streams/writable:501:10)
at Writable.write (streams/writable:510:10)
at TLSSocket.socketOnData (ws/lib/websocket.js:1355:35)
at TLSSocket.emit (node:events:518:28)
My configuration is just calling tracer.init(). I do have opentelemetry loaded and configured before dd
Reproduction Code
literally just tracer.init()
profiling is disabled runtime metrics are enabled
Error Logs
No response
Tracer Config
No response
Operating System
No response
Bundling
No Bundling
FYI it looks like this was due to initializing the library after we were already making some WS requests (we initialize a ws connection on container start).
After moving the Datadog initialization to happen before the WS connections were opened, the issue was gone. With that said, having the lib error like this seems weird.
On a related note, the Plugins interface definition is missing ws, which prevents using tracer.use('ws', false); in TypeScript to programmatically disable the plugin
@mtowers-backbone could you please open that as a separate issue?
@JCMais if I am not mistaken, you're using ESM? We are checking for modules being loaded before the tracer starts in CJS.
I am also running into this issue. The problem is that ctx.span is undefined here. I see this PR that adds a check for ctx.span in the end() method - maybe that needs to be extended to all of the methods?
We are using ESM to import, but we're initializing dd-trace on import - theoretically before anything else happens.