@@ -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 ) {
@@ -581,7 +591,7 @@ class Tracer extends Utility implements TracerInterface {
581591 * ```
582592 */
583593 public getRootXrayTraceId ( ) : string | undefined {
584- return this . envVarsService . getXrayTraceId ( ) ;
594+ return getXRayTraceIdFromEnv ( ) ;
585595 }
586596
587597 /**
@@ -628,7 +638,7 @@ class Tracer extends Utility implements TracerInterface {
628638 public isTraceSampled ( ) : boolean {
629639 if ( ! this . isTracingEnabled ( ) ) return false ;
630640
631- return this . envVarsService . getXrayTraceSampled ( ) ;
641+ return isRequestXRaySampled ( ) ;
632642 }
633643
634644 /**
@@ -733,39 +743,28 @@ class Tracer extends Utility implements TracerInterface {
733743 return this . customConfigService ;
734744 }
735745
736- /**
737- * Get for `envVarsService`.
738- * Used internally during initialization.
739- */
740- private getEnvVarsService ( ) : EnvironmentVariablesService {
741- return this . envVarsService ;
742- }
743-
744746 /**
745747 * Determine if we are running inside an Amplify CLI process.
746748 * Used internally during initialization.
747749 */
748750 private isAmplifyCli ( ) : boolean {
749- return (
750- this . getEnvVarsService ( ) . getAwsExecutionEnv ( ) ===
751- 'AWS_Lambda_amplify-mock'
752- ) ;
751+ return this . #envConfig. awsExecutionEnv === 'AWS_Lambda_amplify-mock' ;
753752 }
754753
755754 /**
756755 * Determine if we are running in a Lambda execution environment.
757756 * Used internally during initialization.
758757 */
759758 private isLambdaExecutionEnv ( ) : boolean {
760- return this . getEnvVarsService ( ) . getAwsExecutionEnv ( ) !== '' ;
759+ return this . #envConfig . awsExecutionEnv !== '' ;
761760 }
762761
763762 /**
764763 * Determine if we are running inside a SAM CLI process.
765764 * Used internally during initialization.
766765 */
767766 private isLambdaSamCli ( ) : boolean {
768- return this . getEnvVarsService ( ) . getSamLocal ( ) !== '' ;
767+ return this . #envConfig . samLocal !== '' ;
769768 }
770769
771770 /**
@@ -784,10 +783,8 @@ class Tracer extends Utility implements TracerInterface {
784783 return ;
785784 }
786785
787- const envVarsValue = this . getEnvVarsService ( ) . getTracingCaptureError ( ) ;
788- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
786+ if ( this . #envConfig. captureError . toLowerCase ( ) === 'false' ) {
789787 this . captureError = false ;
790-
791788 return ;
792789 }
793790 }
@@ -820,10 +817,8 @@ class Tracer extends Utility implements TracerInterface {
820817 return ;
821818 }
822819
823- const envVarsValue = this . getEnvVarsService ( ) . getCaptureHTTPsRequests ( ) ;
824- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
820+ if ( this . #envConfig. captureHTTPsRequests . toLowerCase ( ) === 'false' ) {
825821 this . captureHTTPsRequests = false ;
826-
827822 return ;
828823 }
829824 }
@@ -844,10 +839,8 @@ class Tracer extends Utility implements TracerInterface {
844839 return ;
845840 }
846841
847- const envVarsValue = this . getEnvVarsService ( ) . getTracingCaptureResponse ( ) ;
848- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
842+ if ( this . #envConfig. captureResponse . toLowerCase ( ) === 'false' ) {
849843 this . captureResponse = false ;
850-
851844 return ;
852845 }
853846 }
@@ -876,7 +869,6 @@ class Tracer extends Utility implements TracerInterface {
876869 const { enabled, serviceName, captureHTTPsRequests, customConfigService } =
877870 options ;
878871
879- this . envVarsService = environmentVariablesService ;
880872 this . setCustomConfigService ( customConfigService ) ;
881873 this . setTracingEnabled ( enabled ) ;
882874 this . setCaptureResponse ( ) ;
@@ -910,10 +902,11 @@ class Tracer extends Utility implements TracerInterface {
910902 return ;
911903 }
912904
913- const envVarsValue = this . getEnvVarsService ( ) . getServiceName ( ) ;
914- if ( envVarsValue !== undefined && this . isValidServiceName ( envVarsValue ) ) {
915- this . serviceName = envVarsValue ;
916-
905+ if (
906+ this . #envConfig. serviceName !== undefined &&
907+ this . isValidServiceName ( this . #envConfig. serviceName )
908+ ) {
909+ this . serviceName = this . #envConfig. serviceName ;
917910 return ;
918911 }
919912 this . serviceName = this . defaultServiceName ;
@@ -943,10 +936,8 @@ class Tracer extends Utility implements TracerInterface {
943936 return ;
944937 }
945938
946- const envVarsValue = this . getEnvVarsService ( ) . getTracingEnabled ( ) ;
947- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
939+ if ( this . #envConfig. tracingEnabled . toLowerCase ( ) === 'false' ) {
948940 this . tracingEnabled = false ;
949-
950941 return ;
951942 }
952943
@@ -958,6 +949,38 @@ class Tracer extends Utility implements TracerInterface {
958949 this . tracingEnabled = false ;
959950 }
960951 }
952+
953+ /**
954+ * Set environment variables for the tracer.
955+ * This method is called during initialization to ensure environment variables are available.
956+ */
957+ #setEnvConfig( ) : void {
958+ this . #envConfig. awsExecutionEnv = getStringFromEnv ( {
959+ key : 'AWS_EXECUTION_ENV' ,
960+ defaultValue : '' ,
961+ } ) ;
962+ this . #envConfig. samLocal = getStringFromEnv ( {
963+ key : 'AWS_SAM_LOCAL' ,
964+ defaultValue : '' ,
965+ } ) ;
966+ this . #envConfig. captureError = getStringFromEnv ( {
967+ key : 'POWERTOOLS_TRACER_CAPTURE_ERROR' ,
968+ defaultValue : '' ,
969+ } ) ;
970+ this . #envConfig. captureHTTPsRequests = getStringFromEnv ( {
971+ key : 'POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS' ,
972+ defaultValue : '' ,
973+ } ) ;
974+ this . #envConfig. captureResponse = getStringFromEnv ( {
975+ key : 'POWERTOOLS_TRACER_CAPTURE_RESPONSE' ,
976+ defaultValue : '' ,
977+ } ) ;
978+ this . #envConfig. tracingEnabled = getStringFromEnv ( {
979+ key : 'POWERTOOLS_TRACE_ENABLED' ,
980+ defaultValue : '' ,
981+ } ) ;
982+ this . #envConfig. serviceName = getServiceName ( ) ;
983+ }
961984}
962985
963986export { Tracer } ;
0 commit comments