@@ -130,6 +130,10 @@ class Tracer implements TracerInterface {
130
130
public constructor ( options : TracerOptions = { } ) {
131
131
this . setOptions ( options ) ;
132
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,11 +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
- const segment = this . provider . getSegment ( ) ;
465
+ if ( this . isTracingEnabled ( ) === false ) {
466
+ return new Subsegment ( '## Dummy segment' ) ;
467
+ }
468
+ const segment = this . provider . getSegment ( ) ;
462
469
if ( segment === undefined ) {
463
470
throw new Error ( 'Failed to get the current sub/segment from the context.' ) ;
464
471
}
465
-
472
+
466
473
return segment ;
467
474
}
468
475
@@ -498,7 +505,7 @@ class Tracer implements TracerInterface {
498
505
* @param value - Value for annotation
499
506
*/
500
507
public putAnnotation ( key : string , value : string | number | boolean ) : void {
501
- if ( this . tracingEnabled === false ) return ;
508
+ if ( this . isTracingEnabled ( ) === false ) return ;
502
509
503
510
const document = this . getSegment ( ) ;
504
511
if ( document instanceof Segment ) {
@@ -531,7 +538,7 @@ class Tracer implements TracerInterface {
531
538
* @param timestamp - Namespace that metadata will lie under, if none is passed it will use the serviceName
532
539
*/
533
540
public putMetadata ( key : string , value : unknown , namespace ?: string | undefined ) : void {
534
- if ( this . tracingEnabled === false ) return ;
541
+ if ( this . isTracingEnabled ( ) === false ) return ;
535
542
536
543
const document = this . getSegment ( ) ;
537
544
if ( document instanceof Segment ) {
@@ -567,6 +574,8 @@ class Tracer implements TracerInterface {
567
574
* @param segment - Subsegment to set as the current segment
568
575
*/
569
576
public setSegment ( segment : Segment | Subsegment ) : void {
577
+ if ( this . isTracingEnabled ( ) === false ) return ;
578
+
570
579
return this . provider . setSegment ( segment ) ;
571
580
}
572
581
@@ -730,21 +739,21 @@ class Tracer implements TracerInterface {
730
739
private setTracingEnabled ( enabled ?: boolean ) : void {
731
740
if ( enabled !== undefined && enabled === false ) {
732
741
this . tracingEnabled = enabled ;
733
-
742
+
734
743
return ;
735
744
}
736
745
737
746
const customConfigValue = this . getCustomConfigService ( ) ?. getTracingEnabled ( ) ;
738
747
if ( customConfigValue !== undefined && customConfigValue . toLowerCase ( ) === 'false' ) {
739
748
this . tracingEnabled = false ;
740
-
749
+
741
750
return ;
742
751
}
743
752
744
753
const envVarsValue = this . getEnvVarsService ( ) ?. getTracingEnabled ( ) ;
745
754
if ( envVarsValue . toLowerCase ( ) === 'false' ) {
746
755
this . tracingEnabled = false ;
747
-
756
+
748
757
return ;
749
758
}
750
759
0 commit comments