@@ -10,8 +10,13 @@ import type {
10
10
GenericLogger ,
11
11
HandlerMethodDecorator ,
12
12
} from '@aws-lambda-powertools/commons/types' ;
13
+ import {
14
+ getBooleanFromEnv ,
15
+ getServiceName ,
16
+ getStringFromEnv ,
17
+ isDevMode ,
18
+ } from '@aws-lambda-powertools/commons/utils/env' ;
13
19
import type { Callback , Context , Handler } from 'aws-lambda' ;
14
- import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
15
20
import {
16
21
COLD_START_METRIC ,
17
22
DEFAULT_NAMESPACE ,
@@ -168,11 +173,6 @@ class Metrics extends Utility implements MetricsInterface {
168
173
*/
169
174
private dimensionSets : Dimensions [ ] = [ ] ;
170
175
171
- /**
172
- * Service for accessing environment variables
173
- */
174
- private envVarsService ?: EnvironmentVariablesService ;
175
-
176
176
/**
177
177
* Name of the Lambda function
178
178
*/
@@ -219,6 +219,18 @@ class Metrics extends Utility implements MetricsInterface {
219
219
*/
220
220
private disabled = false ;
221
221
222
+ /**
223
+ * Cached environment config values.
224
+ * Initialized once in setEnvConfig().
225
+ */
226
+ readonly #envConfig = {
227
+ namespace : '' ,
228
+ functionName : '' ,
229
+ serviceName : '' ,
230
+ disabled : false ,
231
+ devMode : false ,
232
+ } ;
233
+
222
234
/**
223
235
* Custom timestamp for the metrics
224
236
*/
@@ -912,13 +924,6 @@ class Metrics extends Utility implements MetricsInterface {
912
924
return this . customConfigService ;
913
925
}
914
926
915
- /**
916
- * Get the environment variables service.
917
- */
918
- private getEnvVarsService ( ) : EnvironmentVariablesService {
919
- return this . envVarsService as EnvironmentVariablesService ;
920
- }
921
-
922
927
/**
923
928
* Check if a metric is new or not.
924
929
*
@@ -952,7 +957,7 @@ class Metrics extends Utility implements MetricsInterface {
952
957
* @private
953
958
*/
954
959
private setConsole ( ) : void {
955
- if ( ! this . getEnvVarsService ( ) . isDevMode ( ) ) {
960
+ if ( ! this . #envConfig . devMode ) {
956
961
this . console = new Console ( {
957
962
stdout : process . stdout ,
958
963
stderr : process . stderr ,
@@ -978,8 +983,22 @@ class Metrics extends Utility implements MetricsInterface {
978
983
/**
979
984
* Set the environment variables service to be used.
980
985
*/
981
- private setEnvVarsService ( ) : void {
982
- this . envVarsService = new EnvironmentVariablesService ( ) ;
986
+ private setEnvConfig ( ) : void {
987
+ this . #envConfig. namespace = getStringFromEnv ( {
988
+ key : 'POWERTOOLS_METRICS_NAMESPACE' ,
989
+ defaultValue : '' ,
990
+ } ) ;
991
+ this . #envConfig. functionName = getStringFromEnv ( {
992
+ key : 'POWERTOOLS_METRICS_FUNCTION_NAME' ,
993
+ defaultValue : '' ,
994
+ } ) ;
995
+ this . #envConfig. serviceName = getServiceName ( ) ;
996
+ this . #envConfig. disabled = getBooleanFromEnv ( {
997
+ key : 'POWERTOOLS_METRICS_DISABLED' ,
998
+ defaultValue : false ,
999
+ extendedParsing : true ,
1000
+ } ) ;
1001
+ this . #envConfig. devMode = isDevMode ( ) ;
983
1002
}
984
1003
985
1004
/**
@@ -988,8 +1007,7 @@ class Metrics extends Utility implements MetricsInterface {
988
1007
* @param functionName - The function name to be used for the cold start metric set in the constructor
989
1008
*/
990
1009
protected setFunctionNameForColdStartMetric ( functionName ?: string ) : void {
991
- const value =
992
- functionName ?. trim ( ) ?? this . getEnvVarsService ( ) . getFunctionName ( ) . trim ( ) ;
1010
+ const value = functionName ?. trim ( ) ?? this . #envConfig. functionName ;
993
1011
if ( value && value . length > 0 ) {
994
1012
this . functionName = value ;
995
1013
}
@@ -1001,9 +1019,10 @@ class Metrics extends Utility implements MetricsInterface {
1001
1019
* @param namespace - The namespace to be used
1002
1020
*/
1003
1021
private setNamespace ( namespace : string | undefined ) : void {
1004
- this . namespace = ( namespace ||
1022
+ this . namespace =
1023
+ namespace ||
1005
1024
this . getCustomConfigService ( ) ?. getNamespace ( ) ||
1006
- this . getEnvVarsService ( ) . getNamespace ( ) ) as string ;
1025
+ this . #envConfig . namespace ;
1007
1026
}
1008
1027
1009
1028
/**
@@ -1012,7 +1031,14 @@ class Metrics extends Utility implements MetricsInterface {
1012
1031
* The `POWERTOOLS_METRICS_DISABLED` environment variable takes precedence over `POWERTOOLS_DEV`.
1013
1032
*/
1014
1033
private setDisabled ( ) : void {
1015
- this . disabled = this . getEnvVarsService ( ) . getMetricsDisabled ( ) ;
1034
+ if (
1035
+ 'POWERTOOLS_METRICS_DISABLED' in process . env &&
1036
+ process . env . POWERTOOLS_METRICS_DISABLED !== undefined
1037
+ ) {
1038
+ this . disabled = this . #envConfig. disabled ;
1039
+ return ;
1040
+ }
1041
+ this . disabled = this . #envConfig. devMode ;
1016
1042
}
1017
1043
1018
1044
/**
@@ -1032,7 +1058,7 @@ class Metrics extends Utility implements MetricsInterface {
1032
1058
functionName,
1033
1059
} = options ;
1034
1060
1035
- this . setEnvVarsService ( ) ;
1061
+ this . setEnvConfig ( ) ;
1036
1062
this . setConsole ( ) ;
1037
1063
this . setCustomConfigService ( customConfigService ) ;
1038
1064
this . setDisabled ( ) ;
@@ -1052,9 +1078,9 @@ class Metrics extends Utility implements MetricsInterface {
1052
1078
*/
1053
1079
private setService ( service : string | undefined ) : void {
1054
1080
const targetService =
1055
- ( ( service ||
1056
- this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1057
- this . getEnvVarsService ( ) . getServiceName ( ) ) as string ) ||
1081
+ service ||
1082
+ this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1083
+ this . #envConfig . serviceName ||
1058
1084
this . defaultServiceName ;
1059
1085
if ( targetService . length > 0 ) {
1060
1086
this . setDefaultDimensions ( { service : targetService } ) ;
0 commit comments