Skip to content

feat(commons): environment variable helpers #3945

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 10 commits into from
May 22, 2025
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
8 changes: 8 additions & 0 deletions packages/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
"import": "./lib/esm/unmarshallDynamoDB.js",
"require": "./lib/cjs/unmarshallDynamoDB.js"
},
"./utils/env": {
"import": "./lib/esm/envUtils.js",
"require": "./lib/cjs/envUtils.js"
},
"./types": {
"import": "./lib/esm/types/index.js",
"require": "./lib/cjs/types/index.js"
Expand All @@ -76,6 +80,10 @@
"lib/cjs/unmarshallDynamoDB.d.ts",
"lib/esm/unmarshallDynamoDB.d.ts"
],
"utils/env": [
"lib/cjs/envUtils.d.ts",
"lib/esm/envUtils.d.ts"
],
"types": [
"lib/cjs/types/index.d.ts",
"lib/esm/types/index.d.ts"
Expand Down
41 changes: 10 additions & 31 deletions packages/commons/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
getServiceName,
getXRayTraceIdFromEnv,
isDevMode,
isRequestXRaySampled,
} from '../envUtils.js';
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';

/**
Expand Down Expand Up @@ -46,7 +52,7 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
* Get the value of the `POWERTOOLS_SERVICE_NAME` environment variable.
*/
public getServiceName(): string {
return this.get(this.serviceNameVariable);
return getServiceName();
}

/**
Expand All @@ -58,9 +64,7 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
* The actual Trace ID is: `1-5759e988-bd862e3fe1be46a994272793`.
*/
public getXrayTraceId(): string | undefined {
const xRayTraceData = this.getXrayTraceData();

return xRayTraceData?.Root;
return getXRayTraceIdFromEnv();
}

/**
Expand All @@ -70,16 +74,14 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
*/
public getXrayTraceSampled(): boolean {
const xRayTraceData = this.getXrayTraceData();

return xRayTraceData?.Sampled === '1';
return isRequestXRaySampled();
}

/**
* Determine if the current invocation is running in a development environment.
*/
public isDevMode(): boolean {
return this.isValueTrue(this.get(this.devModeVariable));
return isDevMode();
}

/**
Expand All @@ -103,29 +105,6 @@ class EnvironmentVariablesService implements ConfigServiceInterface {

return falsyValues.includes(value.toLowerCase());
}

/**
* Get the AWS X-Ray Trace data from the environment variable.
*
* The method parses the environment variable `_X_AMZN_TRACE_ID` and returns an object with the key-value pairs.
*/
private getXrayTraceData(): Record<string, string> | undefined {
const xRayTraceEnv = this.get(this.xRayTraceIdVariable);

if (xRayTraceEnv === '') return undefined;

if (!xRayTraceEnv.includes('=')) return { Root: xRayTraceEnv };

const xRayTraceData: Record<string, string> = {};

for (const field of xRayTraceEnv.split(';')) {
const [key, value] = field.split('=');

xRayTraceData[key] = value;
}

return xRayTraceData;
}
}

export { EnvironmentVariablesService };
9 changes: 9 additions & 0 deletions packages/commons/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const POWERTOOLS_DEV_ENV_VAR = 'POWERTOOLS_DEV' as const;
const POWERTOOLS_SERVICE_NAME_ENV_VAR = 'POWERTOOLS_SERVICE_NAME' as const;
const XRAY_TRACE_ID_ENV_VAR = '_X_AMZN_TRACE_ID' as const;

export {
POWERTOOLS_DEV_ENV_VAR,
POWERTOOLS_SERVICE_NAME_ENV_VAR,
XRAY_TRACE_ID_ENV_VAR,
};
Loading