@@ -13,7 +13,6 @@ const log = require('../log')
1313const { storage } = require ( '../../../datadog-core' )
1414const telemetryMetrics = require ( '../telemetry/metrics' )
1515const { channel } = require ( 'dc-polyfill' )
16- const spanleak = require ( '../spanleak' )
1716const util = require ( 'util' )
1817
1918const tracerMetrics = telemetryMetrics . manager . namespace ( 'tracers' )
@@ -99,7 +98,18 @@ class DatadogSpan {
9998
10099 unfinishedRegistry . register ( this , operationName , this )
101100 }
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+ }
103113
104114 if ( startCh . hasSubscribers ) {
105115 startCh . publish ( { span : this , fields } )
0 commit comments