@@ -128,8 +128,12 @@ class Tracer implements TracerInterface {
128
128
private tracingEnabled : boolean = true ;
129
129
130
130
public constructor ( options : TracerOptions = { } ) {
131
- this . provider = new ProviderService ( ) ;
132
131
this . setOptions ( options ) ;
132
+ this . provider = new ProviderService ( ) ;
133
+ if ( this . isTracingEnabled ( ) === false ) {
134
+ // Tell x-ray-sdk to not throw an error if context is missing but tracing is disabled
135
+ this . provider . setContextMissingStrategy ( ( ) => ( { } ) ) ;
136
+ }
133
137
}
134
138
135
139
/**
@@ -140,7 +144,7 @@ class Tracer implements TracerInterface {
140
144
* @param error - Error to serialize as metadata
141
145
*/
142
146
public addErrorAsMetadata ( error : Error ) : void {
143
- if ( this . tracingEnabled === false ) {
147
+ if ( this . isTracingEnabled ( ) === false ) {
144
148
return ;
145
149
}
146
150
@@ -163,7 +167,7 @@ class Tracer implements TracerInterface {
163
167
* @param methodName - Name of the method that is being traced
164
168
*/
165
169
public addResponseAsMetadata ( data ?: unknown , methodName ?: string ) : void {
166
- if ( data === undefined || this . captureResponse === false || this . tracingEnabled === false ) {
170
+ if ( data === undefined || this . captureResponse === false || this . isTracingEnabled ( ) === false ) {
167
171
return ;
168
172
}
169
173
@@ -175,7 +179,7 @@ class Tracer implements TracerInterface {
175
179
*
176
180
*/
177
181
public addServiceNameAnnotation ( ) : void {
178
- if ( this . tracingEnabled === false || this . serviceName === undefined ) {
182
+ if ( this . isTracingEnabled ( ) === false || this . serviceName === undefined ) {
179
183
return ;
180
184
}
181
185
this . putAnnotation ( 'Service' , this . serviceName ) ;
@@ -191,7 +195,7 @@ class Tracer implements TracerInterface {
191
195
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
192
196
*/
193
197
public annotateColdStart ( ) : void {
194
- if ( this . tracingEnabled === true ) {
198
+ if ( this . isTracingEnabled ( ) === true ) {
195
199
this . putAnnotation ( 'ColdStart' , Tracer . coldStart ) ;
196
200
}
197
201
if ( Tracer . coldStart === true ) {
@@ -222,7 +226,7 @@ class Tracer implements TracerInterface {
222
226
* @returns AWS - Instrumented AWS SDK
223
227
*/
224
228
public captureAWS < T > ( aws : T ) : T {
225
- if ( this . tracingEnabled === false ) return aws ;
229
+ if ( this . isTracingEnabled ( ) === false ) return aws ;
226
230
227
231
return this . provider . captureAWS ( aws ) ;
228
232
}
@@ -251,7 +255,7 @@ class Tracer implements TracerInterface {
251
255
* @returns service - Instrumented AWS SDK v2 client
252
256
*/
253
257
public captureAWSClient < T > ( service : T ) : T {
254
- if ( this . tracingEnabled === false ) return service ;
258
+ if ( this . isTracingEnabled ( ) === false ) return service ;
255
259
256
260
return this . provider . captureAWSClient ( service ) ;
257
261
}
@@ -281,7 +285,7 @@ class Tracer implements TracerInterface {
281
285
* @returns service - Instrumented AWS SDK v3 client
282
286
*/
283
287
public captureAWSv3Client < T > ( service : T ) : T {
284
- if ( this . tracingEnabled === false ) return service ;
288
+ if ( this . isTracingEnabled ( ) === false ) return service ;
285
289
286
290
return this . provider . captureAWSv3Client ( service ) ;
287
291
}
@@ -322,7 +326,7 @@ class Tracer implements TracerInterface {
322
326
const originalMethod = descriptor . value ;
323
327
324
328
descriptor . value = ( ( event , context , callback ) => {
325
- if ( this . tracingEnabled === false ) {
329
+ if ( this . isTracingEnabled ( ) === false ) {
326
330
return originalMethod ?. apply ( target , [ event , context , callback ] ) ;
327
331
}
328
332
@@ -389,7 +393,7 @@ class Tracer implements TracerInterface {
389
393
const originalMethod = descriptor . value ;
390
394
391
395
descriptor . value = ( ...args : unknown [ ] ) => {
392
- if ( this . tracingEnabled === false ) {
396
+ if ( this . isTracingEnabled ( ) === false ) {
393
397
return originalMethod ?. apply ( target , [ ...args ] ) ;
394
398
}
395
399
@@ -458,14 +462,14 @@ class Tracer implements TracerInterface {
458
462
* @returns segment - The active segment or subsegment in the current scope.
459
463
*/
460
464
public getSegment ( ) : Segment | Subsegment {
461
- let segment = this . provider . getSegment ( ) ;
462
- if ( segment === undefined && this . isTracingEnabled ( ) === false ) {
463
- segment = new Subsegment ( '## Dummy segment' ) ;
465
+ if ( this . isTracingEnabled ( ) === false ) {
466
+ return new Subsegment ( '## Dummy segment' ) ;
464
467
}
468
+ const segment = this . provider . getSegment ( ) ;
465
469
if ( segment === undefined ) {
466
470
throw new Error ( 'Failed to get the current sub/segment from the context.' ) ;
467
471
}
468
-
472
+
469
473
return segment ;
470
474
}
471
475
@@ -501,7 +505,7 @@ class Tracer implements TracerInterface {
501
505
* @param value - Value for annotation
502
506
*/
503
507
public putAnnotation ( key : string , value : string | number | boolean ) : void {
504
- if ( this . tracingEnabled === false ) return ;
508
+ if ( this . isTracingEnabled ( ) === false ) return ;
505
509
506
510
const document = this . getSegment ( ) ;
507
511
if ( document instanceof Segment ) {
@@ -534,7 +538,7 @@ class Tracer implements TracerInterface {
534
538
* @param timestamp - Namespace that metadata will lie under, if none is passed it will use the serviceName
535
539
*/
536
540
public putMetadata ( key : string , value : unknown , namespace ?: string | undefined ) : void {
537
- if ( this . tracingEnabled === false ) return ;
541
+ if ( this . isTracingEnabled ( ) === false ) return ;
538
542
539
543
const document = this . getSegment ( ) ;
540
544
if ( document instanceof Segment ) {
@@ -735,30 +739,26 @@ class Tracer implements TracerInterface {
735
739
private setTracingEnabled ( enabled ?: boolean ) : void {
736
740
if ( enabled !== undefined && enabled === false ) {
737
741
this . tracingEnabled = enabled ;
738
- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
739
-
742
+
740
743
return ;
741
744
}
742
745
743
746
const customConfigValue = this . getCustomConfigService ( ) ?. getTracingEnabled ( ) ;
744
747
if ( customConfigValue !== undefined && customConfigValue . toLowerCase ( ) === 'false' ) {
745
748
this . tracingEnabled = false ;
746
- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
747
-
749
+
748
750
return ;
749
751
}
750
752
751
753
const envVarsValue = this . getEnvVarsService ( ) ?. getTracingEnabled ( ) ;
752
754
if ( envVarsValue . toLowerCase ( ) === 'false' ) {
753
755
this . tracingEnabled = false ;
754
- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
755
-
756
+
756
757
return ;
757
758
}
758
759
759
760
if ( this . isLambdaSamCli ( ) || this . isLambdaExecutionEnv ( ) === false ) {
760
761
this . tracingEnabled = false ;
761
- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
762
762
}
763
763
}
764
764
0 commit comments