@@ -13,7 +13,6 @@ const log = require('../log')
13
13
const { storage } = require ( '../../../datadog-core' )
14
14
const telemetryMetrics = require ( '../telemetry/metrics' )
15
15
const { channel } = require ( 'dc-polyfill' )
16
- const spanleak = require ( '../spanleak' )
17
16
const util = require ( 'util' )
18
17
19
18
const tracerMetrics = telemetryMetrics . manager . namespace ( 'tracers' )
@@ -99,7 +98,18 @@ class DatadogSpan {
99
98
100
99
unfinishedRegistry . register ( this , operationName , this )
101
100
}
102
- spanleak . addSpan ( this , operationName )
101
+
102
+ // Nullish operator is used here because both `tracer` and `tracer._config`
103
+ // can be null and there are tests passing invalid values to the `Span`
104
+ // constructor which still succeed today. Part of the problem is that `Span`
105
+ // stores only the tracer and not the config, so anything that needs the
106
+ // config has to read it from the tracer stored on the span, including
107
+ // even `Span` itself in this case.
108
+ //
109
+ // TODO: Refactor Tracer/Span + tests to avoid having to do nullish checks.
110
+ if ( tracer ?. _config ?. spanLeakDebug > 0 ) {
111
+ require ( '../spanleak' ) . addSpan ( this , operationName )
112
+ }
103
113
104
114
if ( startCh . hasSubscribers ) {
105
115
startCh . publish ( { span : this , fields } )
0 commit comments