@@ -9,13 +9,7 @@ const DynamicInstrumentation = require('./debugger')
99const telemetry = require ( './telemetry' )
1010const nomenclature = require ( './service-naming' )
1111const PluginManager = require ( './plugin_manager' )
12- const remoteConfig = require ( './remote_config' )
13- const AppsecSdk = require ( './appsec/sdk' )
14- const dogstatsd = require ( './dogstatsd' )
1512const NoopDogStatsDClient = require ( './noop/dogstatsd' )
16- const { SSIHeuristics } = require ( './profiling/ssi-heuristics' )
17- const appsecStandalone = require ( './appsec/standalone' )
18- const LLMObsSDK = require ( './llmobs/sdk' )
1913
2014class 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+
3558class 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 ) {
0 commit comments