Skip to content

Commit a1cd027

Browse files
authored
lazy load span leak detection only when used (#5259)
1 parent bcf7e61 commit a1cd027

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/dd-trace/src/opentracing/span.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const log = require('../log')
1313
const { storage } = require('../../../datadog-core')
1414
const telemetryMetrics = require('../telemetry/metrics')
1515
const { channel } = require('dc-polyfill')
16-
const spanleak = require('../spanleak')
1716
const util = require('util')
1817

1918
const 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 })

packages/dd-trace/src/proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const remoteConfig = require('./remote_config')
1313
const AppsecSdk = require('./appsec/sdk')
1414
const dogstatsd = require('./dogstatsd')
1515
const NoopDogStatsDClient = require('./noop/dogstatsd')
16-
const spanleak = require('./spanleak')
1716
const { SSIHeuristics } = require('./profiling/ssi-heuristics')
1817
const appsecStandalone = require('./appsec/standalone')
1918
const LLMObsSDK = require('./llmobs/sdk')
@@ -71,6 +70,7 @@ class Tracer extends NoopProxy {
7170
}
7271

7372
if (config.spanLeakDebug > 0) {
73+
const spanleak = require('./spanleak')
7474
if (config.spanLeakDebug === spanleak.MODES.LOG) {
7575
spanleak.enableLogging()
7676
} else if (config.spanLeakDebug === spanleak.MODES.GC_AND_LOG) {

0 commit comments

Comments
 (0)