From 292feed712a37abeb011e0c551a6f8b875a7cd55 Mon Sep 17 00:00:00 2001 From: uttam282005 Date: Sat, 19 Jul 2025 21:05:19 +0530 Subject: [PATCH 1/3] refactor(metrics): replace EnvironmentVariablesService with cached #envConfig --- packages/metrics/src/Metrics.ts | 71 +++++++++++++------ .../src/config/EnvironmentVariablesService.ts | 48 ------------- 2 files changed, 48 insertions(+), 71 deletions(-) delete mode 100644 packages/metrics/src/config/EnvironmentVariablesService.ts diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index 38e41a9cc1..de98b4c1b0 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -10,8 +10,12 @@ import type { GenericLogger, HandlerMethodDecorator, } from '@aws-lambda-powertools/commons/types'; +import { + getServiceName, + getStringFromEnv, + isDevMode, +} from '@aws-lambda-powertools/commons/utils/env'; import type { Callback, Context, Handler } from 'aws-lambda'; -import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js'; import { COLD_START_METRIC, DEFAULT_NAMESPACE, @@ -168,11 +172,6 @@ class Metrics extends Utility implements MetricsInterface { */ private dimensionSets: Dimensions[] = []; - /** - * Service for accessing environment variables - */ - private envVarsService?: EnvironmentVariablesService; - /** * Name of the Lambda function */ @@ -219,6 +218,18 @@ class Metrics extends Utility implements MetricsInterface { */ private disabled = false; + /** + * Cached environment config values. + * Initialized once in setEnvConfig(). + */ + #envConfig!: { + namespace: string; + functionName: string; + serviceName: string; + disabled: boolean; + devMode: boolean; + }; + /** * Custom timestamp for the metrics */ @@ -912,13 +923,6 @@ class Metrics extends Utility implements MetricsInterface { return this.customConfigService; } - /** - * Get the environment variables service. - */ - private getEnvVarsService(): EnvironmentVariablesService { - return this.envVarsService as EnvironmentVariablesService; - } - /** * Check if a metric is new or not. * @@ -952,7 +956,7 @@ class Metrics extends Utility implements MetricsInterface { * @private */ private setConsole(): void { - if (!this.getEnvVarsService().isDevMode()) { + if (!this.#envConfig.devMode) { this.console = new Console({ stdout: process.stdout, stderr: process.stderr, @@ -978,8 +982,31 @@ class Metrics extends Utility implements MetricsInterface { /** * Set the environment variables service to be used. */ - private setEnvVarsService(): void { - this.envVarsService = new EnvironmentVariablesService(); + private setEnvConfig(): void { + this.#envConfig = { + namespace: getStringFromEnv({ + key: 'POWERTOOLS_METRICS_NAMESPACE', + defaultValue: '', + }), + functionName: getStringFromEnv({ + key: 'POWERTOOLS_METRICS_FUNCTION_NAME', + defaultValue: '', + }), + serviceName: getServiceName(), + disabled: this.#getMetricsDisabledFromEnv(), + devMode: isDevMode(), + }; + } + + #getMetricsDisabledFromEnv(): boolean { + const disabledValue = getStringFromEnv({ + key: 'POWERTOOLS_METRICS_DISABLED', + defaultValue: '', + }); + + if (disabledValue.toLowerCase() === 'false') return false; + if (disabledValue.toLowerCase() === 'true') return true; + return isDevMode(); } /** @@ -988,8 +1015,7 @@ class Metrics extends Utility implements MetricsInterface { * @param functionName - The function name to be used for the cold start metric set in the constructor */ protected setFunctionNameForColdStartMetric(functionName?: string): void { - const value = - functionName?.trim() ?? this.getEnvVarsService().getFunctionName().trim(); + const value = functionName?.trim() ?? this.#envConfig.functionName.trim(); if (value && value.length > 0) { this.functionName = value; } @@ -1003,7 +1029,7 @@ class Metrics extends Utility implements MetricsInterface { private setNamespace(namespace: string | undefined): void { this.namespace = (namespace || this.getCustomConfigService()?.getNamespace() || - this.getEnvVarsService().getNamespace()) as string; + this.#envConfig.namespace) as string; } /** @@ -1012,7 +1038,7 @@ class Metrics extends Utility implements MetricsInterface { * The `POWERTOOLS_METRICS_DISABLED` environment variable takes precedence over `POWERTOOLS_DEV`. */ private setDisabled(): void { - this.disabled = this.getEnvVarsService().getMetricsDisabled(); + this.disabled = this.#envConfig.disabled; } /** @@ -1032,7 +1058,7 @@ class Metrics extends Utility implements MetricsInterface { functionName, } = options; - this.setEnvVarsService(); + this.setEnvConfig(); this.setConsole(); this.setCustomConfigService(customConfigService); this.setDisabled(); @@ -1054,8 +1080,7 @@ class Metrics extends Utility implements MetricsInterface { const targetService = ((service || this.getCustomConfigService()?.getServiceName() || - this.getEnvVarsService().getServiceName()) as string) || - this.defaultServiceName; + this.#envConfig.serviceName) as string) || this.defaultServiceName; if (targetService.length > 0) { this.setDefaultDimensions({ service: targetService }); } diff --git a/packages/metrics/src/config/EnvironmentVariablesService.ts b/packages/metrics/src/config/EnvironmentVariablesService.ts deleted file mode 100644 index 6fb6c6719c..0000000000 --- a/packages/metrics/src/config/EnvironmentVariablesService.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons'; -import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js'; - -/** - * Class EnvironmentVariablesService - * - * This class is used to return environment variables that are available in the runtime of - * the current Lambda invocation. - */ -class EnvironmentVariablesService - extends CommonEnvironmentVariablesService - implements ConfigServiceInterface -{ - private readonly namespaceVariable = 'POWERTOOLS_METRICS_NAMESPACE'; - private readonly functionNameVariable = 'POWERTOOLS_METRICS_FUNCTION_NAME'; - private readonly disabledVariable = 'POWERTOOLS_METRICS_DISABLED'; - - /** - * Get the value of the `POWERTOOLS_METRICS_NAMESPACE` environment variable. - */ - public getNamespace(): string { - return this.get(this.namespaceVariable); - } - - /** - * Get the value of the `POWERTOOLS_METRICS_FUNCTION_NAME` environment variable. - */ - public getFunctionName(): string { - return this.get(this.functionNameVariable); - } - - /** - * Get the value of the `POWERTOOLS_METRICS_DISABLED` or `POWERTOOLS_DEV` environment variables. - * - * The `POWERTOOLS_METRICS_DISABLED` environment variable takes precedence over `POWERTOOLS_DEV`. - */ - public getMetricsDisabled(): boolean { - const value = this.get(this.disabledVariable); - - if (this.isValueFalse(value)) return false; - if (this.isValueTrue(value)) return true; - if (this.isDevMode()) return true; - - return false; - } -} - -export { EnvironmentVariablesService }; From 66da594655784c8c76c4b5779f3f1c70988faddb Mon Sep 17 00:00:00 2001 From: uttam282005 Date: Mon, 21 Jul 2025 15:39:38 +0530 Subject: [PATCH 2/3] chore: remove unnecessary type casts for namespace and serviceName --- packages/metrics/src/Metrics.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index de98b4c1b0..f352ccc7ee 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -1027,9 +1027,10 @@ class Metrics extends Utility implements MetricsInterface { * @param namespace - The namespace to be used */ private setNamespace(namespace: string | undefined): void { - this.namespace = (namespace || + this.namespace = + namespace || this.getCustomConfigService()?.getNamespace() || - this.#envConfig.namespace) as string; + this.#envConfig.namespace; } /** @@ -1078,9 +1079,10 @@ class Metrics extends Utility implements MetricsInterface { */ private setService(service: string | undefined): void { const targetService = - ((service || - this.getCustomConfigService()?.getServiceName() || - this.#envConfig.serviceName) as string) || this.defaultServiceName; + service || + this.getCustomConfigService()?.getServiceName() || + this.#envConfig.serviceName || + this.defaultServiceName; if (targetService.length > 0) { this.setDefaultDimensions({ service: targetService }); } From 5341b5bbdd2b19cb43668d3b295c6e7a3f7598f7 Mon Sep 17 00:00:00 2001 From: uttam282005 Date: Mon, 21 Jul 2025 16:48:36 +0530 Subject: [PATCH 3/3] fix: ensure full parsing of POWERTOOLS_METRICS_DISABLED and remove non-null assertions from #envConfig --- packages/metrics/src/Metrics.ts | 59 ++++++++++++++++----------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index f352ccc7ee..a309186f0e 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -11,6 +11,7 @@ import type { HandlerMethodDecorator, } from '@aws-lambda-powertools/commons/types'; import { + getBooleanFromEnv, getServiceName, getStringFromEnv, isDevMode, @@ -222,12 +223,12 @@ class Metrics extends Utility implements MetricsInterface { * Cached environment config values. * Initialized once in setEnvConfig(). */ - #envConfig!: { - namespace: string; - functionName: string; - serviceName: string; - disabled: boolean; - devMode: boolean; + readonly #envConfig = { + namespace: '', + functionName: '', + serviceName: '', + disabled: false, + devMode: false, }; /** @@ -983,30 +984,21 @@ class Metrics extends Utility implements MetricsInterface { * Set the environment variables service to be used. */ private setEnvConfig(): void { - this.#envConfig = { - namespace: getStringFromEnv({ - key: 'POWERTOOLS_METRICS_NAMESPACE', - defaultValue: '', - }), - functionName: getStringFromEnv({ - key: 'POWERTOOLS_METRICS_FUNCTION_NAME', - defaultValue: '', - }), - serviceName: getServiceName(), - disabled: this.#getMetricsDisabledFromEnv(), - devMode: isDevMode(), - }; - } - - #getMetricsDisabledFromEnv(): boolean { - const disabledValue = getStringFromEnv({ - key: 'POWERTOOLS_METRICS_DISABLED', + this.#envConfig.namespace = getStringFromEnv({ + key: 'POWERTOOLS_METRICS_NAMESPACE', defaultValue: '', }); - - if (disabledValue.toLowerCase() === 'false') return false; - if (disabledValue.toLowerCase() === 'true') return true; - return isDevMode(); + this.#envConfig.functionName = getStringFromEnv({ + key: 'POWERTOOLS_METRICS_FUNCTION_NAME', + defaultValue: '', + }); + this.#envConfig.serviceName = getServiceName(); + this.#envConfig.disabled = getBooleanFromEnv({ + key: 'POWERTOOLS_METRICS_DISABLED', + defaultValue: false, + extendedParsing: true, + }); + this.#envConfig.devMode = isDevMode(); } /** @@ -1015,7 +1007,7 @@ class Metrics extends Utility implements MetricsInterface { * @param functionName - The function name to be used for the cold start metric set in the constructor */ protected setFunctionNameForColdStartMetric(functionName?: string): void { - const value = functionName?.trim() ?? this.#envConfig.functionName.trim(); + const value = functionName?.trim() ?? this.#envConfig.functionName; if (value && value.length > 0) { this.functionName = value; } @@ -1039,7 +1031,14 @@ class Metrics extends Utility implements MetricsInterface { * The `POWERTOOLS_METRICS_DISABLED` environment variable takes precedence over `POWERTOOLS_DEV`. */ private setDisabled(): void { - this.disabled = this.#envConfig.disabled; + if ( + 'POWERTOOLS_METRICS_DISABLED' in process.env && + process.env.POWERTOOLS_METRICS_DISABLED !== undefined + ) { + this.disabled = this.#envConfig.disabled; + return; + } + this.disabled = this.#envConfig.devMode; } /**