Skip to content

Commit 522aa56

Browse files
authored
lazy load public api + profiling + crashtracking + span stats (#5256)
1 parent 5f9757c commit 522aa56

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

packages/dd-trace/src/config.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,10 @@ class Config {
754754
this._setValue(env, 'baggageMaxItems', DD_TRACE_BAGGAGE_MAX_ITEMS)
755755
this._setBoolean(env, 'clientIpEnabled', DD_TRACE_CLIENT_IP_ENABLED)
756756
this._setString(env, 'clientIpHeader', DD_TRACE_CLIENT_IP_HEADER)
757-
this._setBoolean(env, 'crashtracking.enabled', DD_CRASHTRACKING_ENABLED)
757+
this._setBoolean(env, 'crashtracking.enabled', coalesce(
758+
DD_CRASHTRACKING_ENABLED,
759+
!this._isInServerlessEnvironment()
760+
))
758761
this._setBoolean(env, 'codeOriginForSpans.enabled', DD_CODE_ORIGIN_FOR_SPANS_ENABLED)
759762
this._setString(env, 'dbmPropagationMode', DD_DBM_PROPAGATION_MODE)
760763
this._setString(env, 'dogstatsd.hostname', DD_DOGSTATSD_HOST || DD_DOGSTATSD_HOSTNAME)
@@ -824,7 +827,11 @@ class Config {
824827
this._envUnprocessed.peerServiceMapping = DD_TRACE_PEER_SERVICE_MAPPING
825828
}
826829
this._setString(env, 'port', DD_TRACE_AGENT_PORT)
827-
const profilingEnabledEnv = coalesce(DD_EXPERIMENTAL_PROFILING_ENABLED, DD_PROFILING_ENABLED)
830+
const profilingEnabledEnv = coalesce(
831+
DD_EXPERIMENTAL_PROFILING_ENABLED,
832+
DD_PROFILING_ENABLED,
833+
this._isInServerlessEnvironment() ? 'false' : undefined
834+
)
828835
const profilingEnabled = isTrue(profilingEnabledEnv)
829836
? 'true'
830837
: isFalse(profilingEnabledEnv)

packages/dd-trace/src/proxy.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ const DynamicInstrumentation = require('./debugger')
99
const telemetry = require('./telemetry')
1010
const nomenclature = require('./service-naming')
1111
const PluginManager = require('./plugin_manager')
12-
const remoteConfig = require('./remote_config')
13-
const AppsecSdk = require('./appsec/sdk')
14-
const dogstatsd = require('./dogstatsd')
1512
const NoopDogStatsDClient = require('./noop/dogstatsd')
16-
const { SSIHeuristics } = require('./profiling/ssi-heuristics')
17-
const appsecStandalone = require('./appsec/standalone')
18-
const LLMObsSDK = require('./llmobs/sdk')
1913

2014
class LazyModule {
2115
constructor (provider) {
@@ -32,6 +26,35 @@ class LazyModule {
3226
}
3327
}
3428

29+
function lazyProxy (obj, property, config, getClass, ...args) {
30+
if (config?._isInServerlessEnvironment?.() === false) {
31+
defineEagerly(obj, property, getClass, ...args)
32+
} else {
33+
defineLazily(obj, property, getClass, ...args)
34+
}
35+
}
36+
37+
function defineEagerly (obj, property, getClass, ...args) {
38+
const RealClass = getClass()
39+
40+
obj[property] = new RealClass(...args)
41+
}
42+
43+
function defineLazily (obj, property, getClass, ...args) {
44+
Reflect.defineProperty(obj, property, {
45+
get () {
46+
const RealClass = getClass()
47+
const value = new RealClass(...args)
48+
49+
Reflect.defineProperty(obj, property, { value, configurable: true, enumerable: true })
50+
51+
return value
52+
},
53+
configurable: true,
54+
enumerable: true
55+
})
56+
}
57+
3558
class Tracer extends NoopProxy {
3659
constructor () {
3760
super()
@@ -66,7 +89,8 @@ class Tracer extends NoopProxy {
6689
telemetry.start(config, this._pluginManager)
6790

6891
if (config.dogstatsd) {
69-
this.dogstatsd = new dogstatsd.CustomMetrics(config)
92+
// Custom Metrics
93+
lazyProxy(this, 'dogstatsd', config, () => require('./dogstatsd').CustomMetrics, config)
7094
}
7195

7296
if (config.spanLeakDebug > 0) {
@@ -80,7 +104,7 @@ class Tracer extends NoopProxy {
80104
}
81105

82106
if (config.remoteConfig.enabled && !config.isCiVisibility) {
83-
const rc = remoteConfig.enable(config, this._modules.appsec)
107+
const rc = require('./remote_config').enable(config, this._modules.appsec)
84108

85109
rc.setProductHandler('APM_TRACING', (action, conf) => {
86110
if (action === 'unapply') {
@@ -120,6 +144,7 @@ class Tracer extends NoopProxy {
120144
}
121145

122146
if (config.profiling.enabled !== 'false') {
147+
const { SSIHeuristics } = require('./profiling/ssi-heuristics')
123148
const ssiHeuristics = new SSIHeuristics(config)
124149
ssiHeuristics.start()
125150
let mockProfiler = null
@@ -210,11 +235,11 @@ class Tracer extends NoopProxy {
210235
this._modules.llmobs.enable(config)
211236
}
212237
if (!this._tracingInitialized) {
213-
const prioritySampler = appsecStandalone.configure(config)
238+
const prioritySampler = config.appsec.standalone?.enabled && require('./appsec/standalone').configure(config)
214239
this._tracer = new DatadogTracer(config, prioritySampler)
215240
this.dataStreamsCheckpointer = this._tracer.dataStreamsCheckpointer
216-
this.appsec = new AppsecSdk(this._tracer, config)
217-
this.llmobs = new LLMObsSDK(this._tracer, this._modules.llmobs, config)
241+
lazyProxy(this, 'appsec', config, () => require('./appsec/sdk'), this._tracer, config)
242+
lazyProxy(this, 'llmobs', config, () => require('./llmobs/sdk'), this._tracer, this._modules.llmobs, config)
218243
this._tracingInitialized = true
219244
}
220245
if (config.iast.enabled) {

packages/dd-trace/src/span_processor.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const format = require('./format')
55
const SpanSampler = require('./span_sampler')
66
const GitMetadataTagger = require('./git_metadata_tagger')
77

8-
const { SpanStatsProcessor } = require('./span_stats')
9-
108
const startedSpans = new WeakSet()
119
const finishedSpans = new WeakSet()
1210

@@ -20,7 +18,12 @@ class SpanProcessor {
2018
this._config = config
2119
this._killAll = false
2220

23-
this._stats = new SpanStatsProcessor(config)
21+
// TODO: This should already have been calculated in `config.js`.
22+
if (config.stats?.enabled && !config.appsec?.standalone?.enabled) {
23+
const { SpanStatsProcessor } = require('./span_stats')
24+
this._stats = new SpanStatsProcessor(config)
25+
}
26+
2427
this._spanSampler = new SpanSampler(config.sampler)
2528
this._gitMetadataTagger = new GitMetadataTagger(config)
2629
}
@@ -46,7 +49,7 @@ class SpanProcessor {
4649
for (const span of started) {
4750
if (span._duration !== undefined) {
4851
const formattedSpan = format(span)
49-
this._stats.onSpanFinished(formattedSpan)
52+
this._stats?.onSpanFinished(formattedSpan)
5053
formatted.push(formattedSpan)
5154

5255
spanProcessCh.publish({ span })

packages/dd-trace/test/proxy.spec.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ describe('TracerProxy', () => {
127127
logger: 'logger',
128128
debug: true,
129129
profiling: {},
130-
appsec: {},
130+
appsec: {
131+
standalone: {
132+
enabled: true
133+
}
134+
},
131135
iast: {},
132136
crashtracking: {},
133137
dynamicInstrumentation: {},
@@ -329,6 +333,7 @@ describe('TracerProxy', () => {
329333

330334
const remoteConfigProxy = new RemoteConfigProxy()
331335
remoteConfigProxy.init()
336+
remoteConfigProxy.appsec // Eagerly trigger lazy loading.
332337
expect(DatadogTracer).to.have.been.calledOnce
333338
expect(AppsecSdk).to.have.been.calledOnce
334339
expect(appsec.enable).to.not.have.been.called
@@ -415,11 +420,11 @@ describe('TracerProxy', () => {
415420
}
416421

417422
proxy.init()
418-
419-
expect(dogStatsD._config().dogstatsd.hostname).to.equal('localhost')
420-
421423
proxy.dogstatsd.increment('foo', 10, { alpha: 'bravo' })
424+
422425
const incs = dogStatsD._increments()
426+
427+
expect(dogStatsD._config().dogstatsd.hostname).to.equal('localhost')
423428
expect(incs.length).to.equal(1)
424429
expect(incs[0][0]).to.equal('foo')
425430
expect(incs[0][1]).to.equal(10)
@@ -530,6 +535,7 @@ describe('TracerProxy', () => {
530535

531536
const proxy = new DatadogProxy()
532537
proxy.init(options)
538+
proxy.appsec // Eagerly trigger lazy loading.
533539

534540
const config = AppsecSdk.firstCall.args[1]
535541
expect(standalone.configure).to.have.been.calledOnceWithExactly(config)

0 commit comments

Comments
 (0)