@@ -17,13 +17,15 @@ import type {
1717 HandlerMethodDecorator ,
1818 SyncHandler ,
1919} from '@aws-lambda-powertools/commons/types' ;
20+ import {
21+ getServiceName ,
22+ getStringFromEnv ,
23+ getXRayTraceIdFromEnv ,
24+ isRequestXRaySampled ,
25+ } from '@aws-lambda-powertools/commons/utils/env' ;
2026import type { Handler } from 'aws-lambda' ;
2127import type { Segment , Subsegment } from 'aws-xray-sdk-core' ;
2228import xraySdk from 'aws-xray-sdk-core' ;
23- import {
24- type EnvironmentVariablesService ,
25- environmentVariablesService ,
26- } from './config/EnvironmentVariablesService.js' ;
2729import { ProviderService } from './provider/ProviderService.js' ;
2830import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js' ;
2931import type { ProviderServiceInterface } from './types/ProviderService.js' ;
@@ -174,11 +176,18 @@ class Tracer extends Utility implements TracerInterface {
174176 private customConfigService ?: ConfigServiceInterface ;
175177
176178 /**
177- * The environment variables service used by the Tracer, is always initialized in the constructor in setOptions() .
179+ * Cache environment variables once at init time .
178180 */
179- private envVarsService ! : EnvironmentVariablesService ;
181+ readonly #envConfig = {
182+ awsExecutionEnv : '' ,
183+ samLocal : '' ,
184+ captureError : '' ,
185+ captureHTTPsRequests : '' ,
186+ captureResponse : '' ,
187+ tracingEnabled : '' ,
188+ serviceName : '' ,
189+ } ;
180190
181- // serviceName is always initialized in the constructor in setOptions()
182191 /**
183192 * The name of the service, is always initialized in the constructor in setOptions().
184193 */
@@ -192,6 +201,7 @@ class Tracer extends Utility implements TracerInterface {
192201 public constructor ( options : TracerOptions = { } ) {
193202 super ( ) ;
194203
204+ this . #setEnvConfig( ) ;
195205 this . setOptions ( options ) ;
196206 this . provider = new ProviderService ( ) ;
197207 if ( this . isTracingEnabled ( ) && this . captureHTTPsRequests ) {
@@ -277,6 +287,8 @@ class Tracer extends Utility implements TracerInterface {
277287 }
278288
279289 /**
290+ * @deprecated Use {@link captureAWSv3Client | `captureAWSv3Client()`} instead.
291+ *
280292 * Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
281293 *
282294 * If you want to patch a specific client use {@link captureAWSClient} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead.
@@ -295,16 +307,17 @@ class Tracer extends Utility implements TracerInterface {
295307 * }
296308 * ```
297309 *
298- * @deprecated Use {@link captureAWSv3Client} instead.
299310 * @param aws - AWS SDK v2 import
300311 */
301- public captureAWS < T > ( aws : T ) : T {
312+ /* v8 ignore start */ public captureAWS < T > ( aws : T ) : T {
302313 if ( ! this . isTracingEnabled ( ) ) return aws ;
303314
304315 return this . provider . captureAWS ( aws ) ;
305- }
316+ } /* v8 ignore stop */
306317
307318 /**
319+ * @deprecated Use {@link captureAWSv3Client | `captureAWSv3Client()`} instead.
320+ *
308321 * Patch a specific AWS SDK v2 client and create traces when your application makes calls to that AWS service.
309322 *
310323 * If you want to patch all clients use {@link captureAWS} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead.
@@ -323,7 +336,7 @@ class Tracer extends Utility implements TracerInterface {
323336 * ...
324337 * }
325338 * ```
326- * @deprecated Use { @link captureAWSv3Client} instead.
339+ *
327340 * @param service - AWS SDK v2 client
328341 */
329342 /* v8 ignore start */ public captureAWSClient < T > ( service : T ) : T {
@@ -581,7 +594,7 @@ class Tracer extends Utility implements TracerInterface {
581594 * ```
582595 */
583596 public getRootXrayTraceId ( ) : string | undefined {
584- return this . envVarsService . getXrayTraceId ( ) ;
597+ return getXRayTraceIdFromEnv ( ) ;
585598 }
586599
587600 /**
@@ -628,7 +641,7 @@ class Tracer extends Utility implements TracerInterface {
628641 public isTraceSampled ( ) : boolean {
629642 if ( ! this . isTracingEnabled ( ) ) return false ;
630643
631- return this . envVarsService . getXrayTraceSampled ( ) ;
644+ return isRequestXRaySampled ( ) ;
632645 }
633646
634647 /**
@@ -733,39 +746,28 @@ class Tracer extends Utility implements TracerInterface {
733746 return this . customConfigService ;
734747 }
735748
736- /**
737- * Get for `envVarsService`.
738- * Used internally during initialization.
739- */
740- private getEnvVarsService ( ) : EnvironmentVariablesService {
741- return this . envVarsService ;
742- }
743-
744749 /**
745750 * Determine if we are running inside an Amplify CLI process.
746751 * Used internally during initialization.
747752 */
748753 private isAmplifyCli ( ) : boolean {
749- return (
750- this . getEnvVarsService ( ) . getAwsExecutionEnv ( ) ===
751- 'AWS_Lambda_amplify-mock'
752- ) ;
754+ return this . #envConfig. awsExecutionEnv === 'AWS_Lambda_amplify-mock' ;
753755 }
754756
755757 /**
756758 * Determine if we are running in a Lambda execution environment.
757759 * Used internally during initialization.
758760 */
759761 private isLambdaExecutionEnv ( ) : boolean {
760- return this . getEnvVarsService ( ) . getAwsExecutionEnv ( ) !== '' ;
762+ return this . #envConfig . awsExecutionEnv !== '' ;
761763 }
762764
763765 /**
764766 * Determine if we are running inside a SAM CLI process.
765767 * Used internally during initialization.
766768 */
767769 private isLambdaSamCli ( ) : boolean {
768- return this . getEnvVarsService ( ) . getSamLocal ( ) !== '' ;
770+ return this . #envConfig . samLocal !== '' ;
769771 }
770772
771773 /**
@@ -784,10 +786,8 @@ class Tracer extends Utility implements TracerInterface {
784786 return ;
785787 }
786788
787- const envVarsValue = this . getEnvVarsService ( ) . getTracingCaptureError ( ) ;
788- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
789+ if ( this . #envConfig. captureError . toLowerCase ( ) === 'false' ) {
789790 this . captureError = false ;
790-
791791 return ;
792792 }
793793 }
@@ -820,10 +820,8 @@ class Tracer extends Utility implements TracerInterface {
820820 return ;
821821 }
822822
823- const envVarsValue = this . getEnvVarsService ( ) . getCaptureHTTPsRequests ( ) ;
824- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
823+ if ( this . #envConfig. captureHTTPsRequests . toLowerCase ( ) === 'false' ) {
825824 this . captureHTTPsRequests = false ;
826-
827825 return ;
828826 }
829827 }
@@ -844,10 +842,8 @@ class Tracer extends Utility implements TracerInterface {
844842 return ;
845843 }
846844
847- const envVarsValue = this . getEnvVarsService ( ) . getTracingCaptureResponse ( ) ;
848- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
845+ if ( this . #envConfig. captureResponse . toLowerCase ( ) === 'false' ) {
849846 this . captureResponse = false ;
850-
851847 return ;
852848 }
853849 }
@@ -876,7 +872,6 @@ class Tracer extends Utility implements TracerInterface {
876872 const { enabled, serviceName, captureHTTPsRequests, customConfigService } =
877873 options ;
878874
879- this . envVarsService = environmentVariablesService ;
880875 this . setCustomConfigService ( customConfigService ) ;
881876 this . setTracingEnabled ( enabled ) ;
882877 this . setCaptureResponse ( ) ;
@@ -910,10 +905,11 @@ class Tracer extends Utility implements TracerInterface {
910905 return ;
911906 }
912907
913- const envVarsValue = this . getEnvVarsService ( ) . getServiceName ( ) ;
914- if ( envVarsValue !== undefined && this . isValidServiceName ( envVarsValue ) ) {
915- this . serviceName = envVarsValue ;
916-
908+ if (
909+ this . #envConfig. serviceName !== undefined &&
910+ this . isValidServiceName ( this . #envConfig. serviceName )
911+ ) {
912+ this . serviceName = this . #envConfig. serviceName ;
917913 return ;
918914 }
919915 this . serviceName = this . defaultServiceName ;
@@ -943,10 +939,8 @@ class Tracer extends Utility implements TracerInterface {
943939 return ;
944940 }
945941
946- const envVarsValue = this . getEnvVarsService ( ) . getTracingEnabled ( ) ;
947- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
942+ if ( this . #envConfig. tracingEnabled . toLowerCase ( ) === 'false' ) {
948943 this . tracingEnabled = false ;
949-
950944 return ;
951945 }
952946
@@ -958,6 +952,38 @@ class Tracer extends Utility implements TracerInterface {
958952 this . tracingEnabled = false ;
959953 }
960954 }
955+
956+ /**
957+ * Set environment variables for the tracer.
958+ * This method is called during initialization to ensure environment variables are available.
959+ */
960+ #setEnvConfig( ) : void {
961+ this . #envConfig. awsExecutionEnv = getStringFromEnv ( {
962+ key : 'AWS_EXECUTION_ENV' ,
963+ defaultValue : '' ,
964+ } ) ;
965+ this . #envConfig. samLocal = getStringFromEnv ( {
966+ key : 'AWS_SAM_LOCAL' ,
967+ defaultValue : '' ,
968+ } ) ;
969+ this . #envConfig. captureError = getStringFromEnv ( {
970+ key : 'POWERTOOLS_TRACER_CAPTURE_ERROR' ,
971+ defaultValue : '' ,
972+ } ) ;
973+ this . #envConfig. captureHTTPsRequests = getStringFromEnv ( {
974+ key : 'POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS' ,
975+ defaultValue : '' ,
976+ } ) ;
977+ this . #envConfig. captureResponse = getStringFromEnv ( {
978+ key : 'POWERTOOLS_TRACER_CAPTURE_RESPONSE' ,
979+ defaultValue : '' ,
980+ } ) ;
981+ this . #envConfig. tracingEnabled = getStringFromEnv ( {
982+ key : 'POWERTOOLS_TRACE_ENABLED' ,
983+ defaultValue : '' ,
984+ } ) ;
985+ this . #envConfig. serviceName = getServiceName ( ) ;
986+ }
961987}
962988
963989export { Tracer } ;
0 commit comments