diff --git a/packages/dd-trace/src/opentracing/span.js b/packages/dd-trace/src/opentracing/span.js index a0762e877f..1d55bab2ae 100644 --- a/packages/dd-trace/src/opentracing/span.js +++ b/packages/dd-trace/src/opentracing/span.js @@ -13,7 +13,6 @@ const log = require('../log') const { storage } = require('../../../datadog-core') const telemetryMetrics = require('../telemetry/metrics') const { channel } = require('dc-polyfill') -const spanleak = require('../spanleak') const util = require('util') const tracerMetrics = telemetryMetrics.manager.namespace('tracers') @@ -99,7 +98,18 @@ class DatadogSpan { unfinishedRegistry.register(this, operationName, this) } - spanleak.addSpan(this, operationName) + + // Nullish operator is used here because both `tracer` and `tracer._config` + // can be null and there are tests passing invalid values to the `Span` + // constructor which still succeed today. Part of the problem is that `Span` + // stores only the tracer and not the config, so anything that needs the + // config has to read it from the tracer stored on the span, including + // even `Span` itself in this case. + // + // TODO: Refactor Tracer/Span + tests to avoid having to do nullish checks. + if (tracer?._config?.spanLeakDebug > 0) { + require('../spanleak').addSpan(this, operationName) + } if (startCh.hasSubscribers) { startCh.publish({ span: this, fields }) diff --git a/packages/dd-trace/src/proxy.js b/packages/dd-trace/src/proxy.js index ce679173b9..b2fdf7fe28 100644 --- a/packages/dd-trace/src/proxy.js +++ b/packages/dd-trace/src/proxy.js @@ -13,7 +13,6 @@ const remoteConfig = require('./appsec/remote_config') const AppsecSdk = require('./appsec/sdk') const dogstatsd = require('./dogstatsd') const NoopDogStatsDClient = require('./noop/dogstatsd') -const spanleak = require('./spanleak') const { SSIHeuristics } = require('./profiling/ssi-heuristics') const appsecStandalone = require('./appsec/standalone') const LLMObsSDK = require('./llmobs/sdk') @@ -80,6 +79,7 @@ class Tracer extends NoopProxy { } if (config.spanLeakDebug > 0) { + const spanleak = require('./spanleak') if (config.spanLeakDebug === spanleak.MODES.LOG) { spanleak.enableLogging() } else if (config.spanLeakDebug === spanleak.MODES.GC_AND_LOG) {