@@ -9,13 +9,7 @@ const DynamicInstrumentation = require('./debugger')
9
9
const telemetry = require ( './telemetry' )
10
10
const nomenclature = require ( './service-naming' )
11
11
const PluginManager = require ( './plugin_manager' )
12
- const remoteConfig = require ( './remote_config' )
13
- const AppsecSdk = require ( './appsec/sdk' )
14
- const dogstatsd = require ( './dogstatsd' )
15
12
const NoopDogStatsDClient = require ( './noop/dogstatsd' )
16
- const { SSIHeuristics } = require ( './profiling/ssi-heuristics' )
17
- const appsecStandalone = require ( './appsec/standalone' )
18
- const LLMObsSDK = require ( './llmobs/sdk' )
19
13
20
14
class LazyModule {
21
15
constructor ( provider ) {
@@ -32,6 +26,35 @@ class LazyModule {
32
26
}
33
27
}
34
28
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
+
35
58
class Tracer extends NoopProxy {
36
59
constructor ( ) {
37
60
super ( )
@@ -66,7 +89,8 @@ class Tracer extends NoopProxy {
66
89
telemetry . start ( config , this . _pluginManager )
67
90
68
91
if ( config . dogstatsd ) {
69
- this . dogstatsd = new dogstatsd . CustomMetrics ( config )
92
+ // Custom Metrics
93
+ lazyProxy ( this , 'dogstatsd' , config , ( ) => require ( './dogstatsd' ) . CustomMetrics , config )
70
94
}
71
95
72
96
if ( config . spanLeakDebug > 0 ) {
@@ -80,7 +104,7 @@ class Tracer extends NoopProxy {
80
104
}
81
105
82
106
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 )
84
108
85
109
rc . setProductHandler ( 'APM_TRACING' , ( action , conf ) => {
86
110
if ( action === 'unapply' ) {
@@ -120,6 +144,7 @@ class Tracer extends NoopProxy {
120
144
}
121
145
122
146
if ( config . profiling . enabled !== 'false' ) {
147
+ const { SSIHeuristics } = require ( './profiling/ssi-heuristics' )
123
148
const ssiHeuristics = new SSIHeuristics ( config )
124
149
ssiHeuristics . start ( )
125
150
let mockProfiler = null
@@ -210,11 +235,11 @@ class Tracer extends NoopProxy {
210
235
this . _modules . llmobs . enable ( config )
211
236
}
212
237
if ( ! this . _tracingInitialized ) {
213
- const prioritySampler = appsecStandalone . configure ( config )
238
+ const prioritySampler = config . appsec . standalone ?. enabled && require ( './appsec/standalone' ) . configure ( config )
214
239
this . _tracer = new DatadogTracer ( config , prioritySampler )
215
240
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 )
218
243
this . _tracingInitialized = true
219
244
}
220
245
if ( config . iast . enabled ) {
0 commit comments