Skip to content

Commit 919063b

Browse files
authored
refactor(metrics): replace EnvironmentVariablesService with cached #envConfig (#4188)
1 parent 7757cf0 commit 919063b

File tree

2 files changed

+51
-73
lines changed

2 files changed

+51
-73
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import type {
1010
GenericLogger,
1111
HandlerMethodDecorator,
1212
} from '@aws-lambda-powertools/commons/types';
13+
import {
14+
getBooleanFromEnv,
15+
getServiceName,
16+
getStringFromEnv,
17+
isDevMode,
18+
} from '@aws-lambda-powertools/commons/utils/env';
1319
import type { Callback, Context, Handler } from 'aws-lambda';
14-
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
1520
import {
1621
COLD_START_METRIC,
1722
DEFAULT_NAMESPACE,
@@ -168,11 +173,6 @@ class Metrics extends Utility implements MetricsInterface {
168173
*/
169174
private dimensionSets: Dimensions[] = [];
170175

171-
/**
172-
* Service for accessing environment variables
173-
*/
174-
private envVarsService?: EnvironmentVariablesService;
175-
176176
/**
177177
* Name of the Lambda function
178178
*/
@@ -219,6 +219,18 @@ class Metrics extends Utility implements MetricsInterface {
219219
*/
220220
private disabled = false;
221221

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+
222234
/**
223235
* Custom timestamp for the metrics
224236
*/
@@ -912,13 +924,6 @@ class Metrics extends Utility implements MetricsInterface {
912924
return this.customConfigService;
913925
}
914926

915-
/**
916-
* Get the environment variables service.
917-
*/
918-
private getEnvVarsService(): EnvironmentVariablesService {
919-
return this.envVarsService as EnvironmentVariablesService;
920-
}
921-
922927
/**
923928
* Check if a metric is new or not.
924929
*
@@ -952,7 +957,7 @@ class Metrics extends Utility implements MetricsInterface {
952957
* @private
953958
*/
954959
private setConsole(): void {
955-
if (!this.getEnvVarsService().isDevMode()) {
960+
if (!this.#envConfig.devMode) {
956961
this.console = new Console({
957962
stdout: process.stdout,
958963
stderr: process.stderr,
@@ -978,8 +983,22 @@ class Metrics extends Utility implements MetricsInterface {
978983
/**
979984
* Set the environment variables service to be used.
980985
*/
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();
9831002
}
9841003

9851004
/**
@@ -988,8 +1007,7 @@ class Metrics extends Utility implements MetricsInterface {
9881007
* @param functionName - The function name to be used for the cold start metric set in the constructor
9891008
*/
9901009
protected setFunctionNameForColdStartMetric(functionName?: string): void {
991-
const value =
992-
functionName?.trim() ?? this.getEnvVarsService().getFunctionName().trim();
1010+
const value = functionName?.trim() ?? this.#envConfig.functionName;
9931011
if (value && value.length > 0) {
9941012
this.functionName = value;
9951013
}
@@ -1001,9 +1019,10 @@ class Metrics extends Utility implements MetricsInterface {
10011019
* @param namespace - The namespace to be used
10021020
*/
10031021
private setNamespace(namespace: string | undefined): void {
1004-
this.namespace = (namespace ||
1022+
this.namespace =
1023+
namespace ||
10051024
this.getCustomConfigService()?.getNamespace() ||
1006-
this.getEnvVarsService().getNamespace()) as string;
1025+
this.#envConfig.namespace;
10071026
}
10081027

10091028
/**
@@ -1012,7 +1031,14 @@ class Metrics extends Utility implements MetricsInterface {
10121031
* The `POWERTOOLS_METRICS_DISABLED` environment variable takes precedence over `POWERTOOLS_DEV`.
10131032
*/
10141033
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;
10161042
}
10171043

10181044
/**
@@ -1032,7 +1058,7 @@ class Metrics extends Utility implements MetricsInterface {
10321058
functionName,
10331059
} = options;
10341060

1035-
this.setEnvVarsService();
1061+
this.setEnvConfig();
10361062
this.setConsole();
10371063
this.setCustomConfigService(customConfigService);
10381064
this.setDisabled();
@@ -1052,9 +1078,9 @@ class Metrics extends Utility implements MetricsInterface {
10521078
*/
10531079
private setService(service: string | undefined): void {
10541080
const targetService =
1055-
((service ||
1056-
this.getCustomConfigService()?.getServiceName() ||
1057-
this.getEnvVarsService().getServiceName()) as string) ||
1081+
service ||
1082+
this.getCustomConfigService()?.getServiceName() ||
1083+
this.#envConfig.serviceName ||
10581084
this.defaultServiceName;
10591085
if (targetService.length > 0) {
10601086
this.setDefaultDimensions({ service: targetService });

packages/metrics/src/config/EnvironmentVariablesService.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)