Skip to content

Commit 7cfcd85

Browse files
authored
feat(commons): environment variable helpers (#3945)
1 parent b28636a commit 7cfcd85

File tree

9 files changed

+862
-80
lines changed

9 files changed

+862
-80
lines changed

packages/commons/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
"import": "./lib/esm/unmarshallDynamoDB.js",
5454
"require": "./lib/cjs/unmarshallDynamoDB.js"
5555
},
56+
"./utils/env": {
57+
"import": "./lib/esm/envUtils.js",
58+
"require": "./lib/cjs/envUtils.js"
59+
},
5660
"./types": {
5761
"import": "./lib/esm/types/index.js",
5862
"require": "./lib/cjs/types/index.js"
@@ -76,6 +80,10 @@
7680
"lib/cjs/unmarshallDynamoDB.d.ts",
7781
"lib/esm/unmarshallDynamoDB.d.ts"
7882
],
83+
"utils/env": [
84+
"lib/cjs/envUtils.d.ts",
85+
"lib/esm/envUtils.d.ts"
86+
],
7987
"types": [
8088
"lib/cjs/types/index.d.ts",
8189
"lib/esm/types/index.d.ts"

packages/commons/src/config/EnvironmentVariablesService.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import {
2+
getServiceName,
3+
getXRayTraceIdFromEnv,
4+
isDevMode,
5+
isRequestXRaySampled,
6+
} from '../envUtils.js';
17
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
28

39
/**
@@ -46,7 +52,7 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
4652
* Get the value of the `POWERTOOLS_SERVICE_NAME` environment variable.
4753
*/
4854
public getServiceName(): string {
49-
return this.get(this.serviceNameVariable);
55+
return getServiceName();
5056
}
5157

5258
/**
@@ -58,9 +64,7 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
5864
* The actual Trace ID is: `1-5759e988-bd862e3fe1be46a994272793`.
5965
*/
6066
public getXrayTraceId(): string | undefined {
61-
const xRayTraceData = this.getXrayTraceData();
62-
63-
return xRayTraceData?.Root;
67+
return getXRayTraceIdFromEnv();
6468
}
6569

6670
/**
@@ -70,16 +74,14 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
7074
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
7175
*/
7276
public getXrayTraceSampled(): boolean {
73-
const xRayTraceData = this.getXrayTraceData();
74-
75-
return xRayTraceData?.Sampled === '1';
77+
return isRequestXRaySampled();
7678
}
7779

7880
/**
7981
* Determine if the current invocation is running in a development environment.
8082
*/
8183
public isDevMode(): boolean {
82-
return this.isValueTrue(this.get(this.devModeVariable));
84+
return isDevMode();
8385
}
8486

8587
/**
@@ -103,29 +105,6 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
103105

104106
return falsyValues.includes(value.toLowerCase());
105107
}
106-
107-
/**
108-
* Get the AWS X-Ray Trace data from the environment variable.
109-
*
110-
* The method parses the environment variable `_X_AMZN_TRACE_ID` and returns an object with the key-value pairs.
111-
*/
112-
private getXrayTraceData(): Record<string, string> | undefined {
113-
const xRayTraceEnv = this.get(this.xRayTraceIdVariable);
114-
115-
if (xRayTraceEnv === '') return undefined;
116-
117-
if (!xRayTraceEnv.includes('=')) return { Root: xRayTraceEnv };
118-
119-
const xRayTraceData: Record<string, string> = {};
120-
121-
for (const field of xRayTraceEnv.split(';')) {
122-
const [key, value] = field.split('=');
123-
124-
xRayTraceData[key] = value;
125-
}
126-
127-
return xRayTraceData;
128-
}
129108
}
130109

131110
export { EnvironmentVariablesService };

packages/commons/src/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const POWERTOOLS_DEV_ENV_VAR = 'POWERTOOLS_DEV' as const;
2+
const POWERTOOLS_SERVICE_NAME_ENV_VAR = 'POWERTOOLS_SERVICE_NAME' as const;
3+
const XRAY_TRACE_ID_ENV_VAR = '_X_AMZN_TRACE_ID' as const;
4+
5+
export {
6+
POWERTOOLS_DEV_ENV_VAR,
7+
POWERTOOLS_SERVICE_NAME_ENV_VAR,
8+
XRAY_TRACE_ID_ENV_VAR,
9+
};

0 commit comments

Comments
 (0)