Skip to content

refactor(metrics): replace EnvironmentVariablesService with cached #envConfig #4188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 51 additions & 25 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import type {
GenericLogger,
HandlerMethodDecorator,
} from '@aws-lambda-powertools/commons/types';
import {
getBooleanFromEnv,
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,
Expand Down Expand Up @@ -168,11 +173,6 @@ class Metrics extends Utility implements MetricsInterface {
*/
private dimensionSets: Dimensions[] = [];

/**
* Service for accessing environment variables
*/
private envVarsService?: EnvironmentVariablesService;

/**
* Name of the Lambda function
*/
Expand Down Expand Up @@ -219,6 +219,18 @@ class Metrics extends Utility implements MetricsInterface {
*/
private disabled = false;

/**
* Cached environment config values.
* Initialized once in setEnvConfig().
*/
readonly #envConfig = {
namespace: '',
functionName: '',
serviceName: '',
disabled: false,
devMode: false,
};

/**
* Custom timestamp for the metrics
*/
Expand Down Expand Up @@ -912,13 +924,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.
*
Expand Down Expand Up @@ -952,7 +957,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,
Expand All @@ -978,8 +983,22 @@ 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: '',
});
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();
}

/**
Expand All @@ -988,8 +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.getEnvVarsService().getFunctionName().trim();
const value = functionName?.trim() ?? this.#envConfig.functionName;
if (value && value.length > 0) {
this.functionName = value;
}
Expand All @@ -1001,9 +1019,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.getEnvVarsService().getNamespace()) as string;
this.#envConfig.namespace;
}

/**
Expand All @@ -1012,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.getEnvVarsService().getMetricsDisabled();
if (
'POWERTOOLS_METRICS_DISABLED' in process.env &&
process.env.POWERTOOLS_METRICS_DISABLED !== undefined
) {
this.disabled = this.#envConfig.disabled;
return;
}
this.disabled = this.#envConfig.devMode;
}

/**
Expand All @@ -1032,7 +1058,7 @@ class Metrics extends Utility implements MetricsInterface {
functionName,
} = options;

this.setEnvVarsService();
this.setEnvConfig();
this.setConsole();
this.setCustomConfigService(customConfigService);
this.setDisabled();
Expand All @@ -1052,9 +1078,9 @@ class Metrics extends Utility implements MetricsInterface {
*/
private setService(service: string | undefined): void {
const targetService =
((service ||
this.getCustomConfigService()?.getServiceName() ||
this.getEnvVarsService().getServiceName()) as string) ||
service ||
this.getCustomConfigService()?.getServiceName() ||
this.#envConfig.serviceName ||
this.defaultServiceName;
if (targetService.length > 0) {
this.setDefaultDimensions({ service: targetService });
Expand Down
48 changes: 0 additions & 48 deletions packages/metrics/src/config/EnvironmentVariablesService.ts

This file was deleted.