@@ -128,8 +128,12 @@ class Tracer implements TracerInterface {
128128 private tracingEnabled : boolean = true ;
129129
130130 public constructor ( options : TracerOptions = { } ) {
131- this . provider = new ProviderService ( ) ;
132131 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+ }
133137 }
134138
135139 /**
@@ -140,7 +144,7 @@ class Tracer implements TracerInterface {
140144 * @param error - Error to serialize as metadata
141145 */
142146 public addErrorAsMetadata ( error : Error ) : void {
143- if ( this . tracingEnabled === false ) {
147+ if ( this . isTracingEnabled ( ) === false ) {
144148 return ;
145149 }
146150
@@ -163,7 +167,7 @@ class Tracer implements TracerInterface {
163167 * @param methodName - Name of the method that is being traced
164168 */
165169 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 ) {
167171 return ;
168172 }
169173
@@ -175,7 +179,7 @@ class Tracer implements TracerInterface {
175179 *
176180 */
177181 public addServiceNameAnnotation ( ) : void {
178- if ( this . tracingEnabled === false || this . serviceName === undefined ) {
182+ if ( this . isTracingEnabled ( ) === false || this . serviceName === undefined ) {
179183 return ;
180184 }
181185 this . putAnnotation ( 'Service' , this . serviceName ) ;
@@ -191,7 +195,7 @@ class Tracer implements TracerInterface {
191195 * @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
192196 */
193197 public annotateColdStart ( ) : void {
194- if ( this . tracingEnabled === true ) {
198+ if ( this . isTracingEnabled ( ) === true ) {
195199 this . putAnnotation ( 'ColdStart' , Tracer . coldStart ) ;
196200 }
197201 if ( Tracer . coldStart === true ) {
@@ -222,7 +226,7 @@ class Tracer implements TracerInterface {
222226 * @returns AWS - Instrumented AWS SDK
223227 */
224228 public captureAWS < T > ( aws : T ) : T {
225- if ( this . tracingEnabled === false ) return aws ;
229+ if ( this . isTracingEnabled ( ) === false ) return aws ;
226230
227231 return this . provider . captureAWS ( aws ) ;
228232 }
@@ -251,7 +255,7 @@ class Tracer implements TracerInterface {
251255 * @returns service - Instrumented AWS SDK v2 client
252256 */
253257 public captureAWSClient < T > ( service : T ) : T {
254- if ( this . tracingEnabled === false ) return service ;
258+ if ( this . isTracingEnabled ( ) === false ) return service ;
255259
256260 return this . provider . captureAWSClient ( service ) ;
257261 }
@@ -281,7 +285,7 @@ class Tracer implements TracerInterface {
281285 * @returns service - Instrumented AWS SDK v3 client
282286 */
283287 public captureAWSv3Client < T > ( service : T ) : T {
284- if ( this . tracingEnabled === false ) return service ;
288+ if ( this . isTracingEnabled ( ) === false ) return service ;
285289
286290 return this . provider . captureAWSv3Client ( service ) ;
287291 }
@@ -322,7 +326,7 @@ class Tracer implements TracerInterface {
322326 const originalMethod = descriptor . value ;
323327
324328 descriptor . value = ( ( event , context , callback ) => {
325- if ( this . tracingEnabled === false ) {
329+ if ( this . isTracingEnabled ( ) === false ) {
326330 return originalMethod ?. apply ( target , [ event , context , callback ] ) ;
327331 }
328332
@@ -389,7 +393,7 @@ class Tracer implements TracerInterface {
389393 const originalMethod = descriptor . value ;
390394
391395 descriptor . value = ( ...args : unknown [ ] ) => {
392- if ( this . tracingEnabled === false ) {
396+ if ( this . isTracingEnabled ( ) === false ) {
393397 return originalMethod ?. apply ( target , [ ...args ] ) ;
394398 }
395399
@@ -458,14 +462,14 @@ class Tracer implements TracerInterface {
458462 * @returns segment - The active segment or subsegment in the current scope.
459463 */
460464 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' ) ;
464467 }
468+ const segment = this . provider . getSegment ( ) ;
465469 if ( segment === undefined ) {
466470 throw new Error ( 'Failed to get the current sub/segment from the context.' ) ;
467471 }
468-
472+
469473 return segment ;
470474 }
471475
@@ -501,7 +505,7 @@ class Tracer implements TracerInterface {
501505 * @param value - Value for annotation
502506 */
503507 public putAnnotation ( key : string , value : string | number | boolean ) : void {
504- if ( this . tracingEnabled === false ) return ;
508+ if ( this . isTracingEnabled ( ) === false ) return ;
505509
506510 const document = this . getSegment ( ) ;
507511 if ( document instanceof Segment ) {
@@ -534,7 +538,7 @@ class Tracer implements TracerInterface {
534538 * @param timestamp - Namespace that metadata will lie under, if none is passed it will use the serviceName
535539 */
536540 public putMetadata ( key : string , value : unknown , namespace ?: string | undefined ) : void {
537- if ( this . tracingEnabled === false ) return ;
541+ if ( this . isTracingEnabled ( ) === false ) return ;
538542
539543 const document = this . getSegment ( ) ;
540544 if ( document instanceof Segment ) {
@@ -735,30 +739,26 @@ class Tracer implements TracerInterface {
735739 private setTracingEnabled ( enabled ?: boolean ) : void {
736740 if ( enabled !== undefined && enabled === false ) {
737741 this . tracingEnabled = enabled ;
738- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
739-
742+
740743 return ;
741744 }
742745
743746 const customConfigValue = this . getCustomConfigService ( ) ?. getTracingEnabled ( ) ;
744747 if ( customConfigValue !== undefined && customConfigValue . toLowerCase ( ) === 'false' ) {
745748 this . tracingEnabled = false ;
746- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
747-
749+
748750 return ;
749751 }
750752
751753 const envVarsValue = this . getEnvVarsService ( ) ?. getTracingEnabled ( ) ;
752754 if ( envVarsValue . toLowerCase ( ) === 'false' ) {
753755 this . tracingEnabled = false ;
754- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
755-
756+
756757 return ;
757758 }
758759
759760 if ( this . isLambdaSamCli ( ) || this . isLambdaExecutionEnv ( ) === false ) {
760761 this . tracingEnabled = false ;
761- this . provider . setContextMissingStrategy ( ( ) => undefined ) ;
762762 }
763763 }
764764
0 commit comments