@@ -17,13 +17,15 @@ import type {
17
17
HandlerMethodDecorator ,
18
18
SyncHandler ,
19
19
} from '@aws-lambda-powertools/commons/types' ;
20
+ import {
21
+ getServiceName ,
22
+ getStringFromEnv ,
23
+ getXRayTraceIdFromEnv ,
24
+ isRequestXRaySampled ,
25
+ } from '@aws-lambda-powertools/commons/utils/env' ;
20
26
import type { Handler } from 'aws-lambda' ;
21
27
import type { Segment , Subsegment } from 'aws-xray-sdk-core' ;
22
28
import xraySdk from 'aws-xray-sdk-core' ;
23
- import {
24
- type EnvironmentVariablesService ,
25
- environmentVariablesService ,
26
- } from './config/EnvironmentVariablesService.js' ;
27
29
import { ProviderService } from './provider/ProviderService.js' ;
28
30
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js' ;
29
31
import type { ProviderServiceInterface } from './types/ProviderService.js' ;
@@ -174,11 +176,18 @@ class Tracer extends Utility implements TracerInterface {
174
176
private customConfigService ?: ConfigServiceInterface ;
175
177
176
178
/**
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 .
178
180
*/
179
- private envVarsService ! : EnvironmentVariablesService ;
181
+ readonly #envConfig = {
182
+ awsExecutionEnv : '' ,
183
+ samLocal : '' ,
184
+ captureError : '' ,
185
+ captureHTTPsRequests : '' ,
186
+ captureResponse : '' ,
187
+ tracingEnabled : '' ,
188
+ serviceName : '' ,
189
+ } ;
180
190
181
- // serviceName is always initialized in the constructor in setOptions()
182
191
/**
183
192
* The name of the service, is always initialized in the constructor in setOptions().
184
193
*/
@@ -192,6 +201,7 @@ class Tracer extends Utility implements TracerInterface {
192
201
public constructor ( options : TracerOptions = { } ) {
193
202
super ( ) ;
194
203
204
+ this . #setEnvConfig( ) ;
195
205
this . setOptions ( options ) ;
196
206
this . provider = new ProviderService ( ) ;
197
207
if ( this . isTracingEnabled ( ) && this . captureHTTPsRequests ) {
@@ -581,7 +591,7 @@ class Tracer extends Utility implements TracerInterface {
581
591
* ```
582
592
*/
583
593
public getRootXrayTraceId ( ) : string | undefined {
584
- return this . envVarsService . getXrayTraceId ( ) ;
594
+ return getXRayTraceIdFromEnv ( ) ;
585
595
}
586
596
587
597
/**
@@ -628,7 +638,7 @@ class Tracer extends Utility implements TracerInterface {
628
638
public isTraceSampled ( ) : boolean {
629
639
if ( ! this . isTracingEnabled ( ) ) return false ;
630
640
631
- return this . envVarsService . getXrayTraceSampled ( ) ;
641
+ return isRequestXRaySampled ( ) ;
632
642
}
633
643
634
644
/**
@@ -733,39 +743,28 @@ class Tracer extends Utility implements TracerInterface {
733
743
return this . customConfigService ;
734
744
}
735
745
736
- /**
737
- * Get for `envVarsService`.
738
- * Used internally during initialization.
739
- */
740
- private getEnvVarsService ( ) : EnvironmentVariablesService {
741
- return this . envVarsService ;
742
- }
743
-
744
746
/**
745
747
* Determine if we are running inside an Amplify CLI process.
746
748
* Used internally during initialization.
747
749
*/
748
750
private isAmplifyCli ( ) : boolean {
749
- return (
750
- this . getEnvVarsService ( ) . getAwsExecutionEnv ( ) ===
751
- 'AWS_Lambda_amplify-mock'
752
- ) ;
751
+ return this . #envConfig. awsExecutionEnv === 'AWS_Lambda_amplify-mock' ;
753
752
}
754
753
755
754
/**
756
755
* Determine if we are running in a Lambda execution environment.
757
756
* Used internally during initialization.
758
757
*/
759
758
private isLambdaExecutionEnv ( ) : boolean {
760
- return this . getEnvVarsService ( ) . getAwsExecutionEnv ( ) !== '' ;
759
+ return this . #envConfig . awsExecutionEnv !== '' ;
761
760
}
762
761
763
762
/**
764
763
* Determine if we are running inside a SAM CLI process.
765
764
* Used internally during initialization.
766
765
*/
767
766
private isLambdaSamCli ( ) : boolean {
768
- return this . getEnvVarsService ( ) . getSamLocal ( ) !== '' ;
767
+ return this . #envConfig . samLocal !== '' ;
769
768
}
770
769
771
770
/**
@@ -784,10 +783,8 @@ class Tracer extends Utility implements TracerInterface {
784
783
return ;
785
784
}
786
785
787
- const envVarsValue = this . getEnvVarsService ( ) . getTracingCaptureError ( ) ;
788
- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
786
+ if ( this . #envConfig. captureError . toLowerCase ( ) === 'false' ) {
789
787
this . captureError = false ;
790
-
791
788
return ;
792
789
}
793
790
}
@@ -820,10 +817,8 @@ class Tracer extends Utility implements TracerInterface {
820
817
return ;
821
818
}
822
819
823
- const envVarsValue = this . getEnvVarsService ( ) . getCaptureHTTPsRequests ( ) ;
824
- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
820
+ if ( this . #envConfig. captureHTTPsRequests . toLowerCase ( ) === 'false' ) {
825
821
this . captureHTTPsRequests = false ;
826
-
827
822
return ;
828
823
}
829
824
}
@@ -844,10 +839,8 @@ class Tracer extends Utility implements TracerInterface {
844
839
return ;
845
840
}
846
841
847
- const envVarsValue = this . getEnvVarsService ( ) . getTracingCaptureResponse ( ) ;
848
- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
842
+ if ( this . #envConfig. captureResponse . toLowerCase ( ) === 'false' ) {
849
843
this . captureResponse = false ;
850
-
851
844
return ;
852
845
}
853
846
}
@@ -876,7 +869,6 @@ class Tracer extends Utility implements TracerInterface {
876
869
const { enabled, serviceName, captureHTTPsRequests, customConfigService } =
877
870
options ;
878
871
879
- this . envVarsService = environmentVariablesService ;
880
872
this . setCustomConfigService ( customConfigService ) ;
881
873
this . setTracingEnabled ( enabled ) ;
882
874
this . setCaptureResponse ( ) ;
@@ -910,10 +902,11 @@ class Tracer extends Utility implements TracerInterface {
910
902
return ;
911
903
}
912
904
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 ;
917
910
return ;
918
911
}
919
912
this . serviceName = this . defaultServiceName ;
@@ -943,10 +936,8 @@ class Tracer extends Utility implements TracerInterface {
943
936
return ;
944
937
}
945
938
946
- const envVarsValue = this . getEnvVarsService ( ) . getTracingEnabled ( ) ;
947
- if ( envVarsValue . toLowerCase ( ) === 'false' ) {
939
+ if ( this . #envConfig. tracingEnabled . toLowerCase ( ) === 'false' ) {
948
940
this . tracingEnabled = false ;
949
-
950
941
return ;
951
942
}
952
943
@@ -958,6 +949,38 @@ class Tracer extends Utility implements TracerInterface {
958
949
this . tracingEnabled = false ;
959
950
}
960
951
}
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
+ }
961
984
}
962
985
963
986
export { Tracer } ;
0 commit comments