Skip to content

Commit 8d3fa28

Browse files
committed
feat: moved EnvService to commons + exposed getXrayTraceId in tracer
1 parent 652b7f0 commit 8d3fa28

20 files changed

+271
-302
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Abstract class ConfigService
3+
*
4+
* This class defines common methods and variables that can be set by the developer
5+
* in the runtime.
6+
*
7+
* @class
8+
* @abstract
9+
*/
10+
abstract class ConfigService {
11+
12+
/**
13+
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables
14+
* @protected
15+
*/
16+
protected serviceNameVariable = 'POWERTOOLS_SERVICE_NAME';
17+
18+
/**
19+
* It returns the value of an environment variable that has given name.
20+
*
21+
* @param {string} name
22+
* @returns {string}
23+
*/
24+
public abstract get(name: string): string;
25+
26+
/**
27+
* It returns the value of the POWERTOOLS_SERVICE_NAME environment variable.
28+
*
29+
* @returns {string}
30+
*/
31+
public abstract getServiceName(): string;
32+
33+
}
34+
35+
export {
36+
ConfigService,
37+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ConfigService } from '.';
2+
3+
/**
4+
* Class EnvironmentVariablesService
5+
*
6+
* This class is used to return environment variables that are available in the runtime of
7+
* the current Lambda invocation.
8+
* These variables can be a mix of runtime environment variables set by AWS and
9+
* variables that can be set by the developer additionally.
10+
*
11+
* @class
12+
* @extends {ConfigService}
13+
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
14+
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables
15+
*/
16+
class EnvironmentVariablesService extends ConfigService {
17+
18+
// Reserved environment variables
19+
private xRayTraceIdVariable = '_X_AMZN_TRACE_ID';
20+
21+
/**
22+
* It returns the value of an environment variable that has given name.
23+
*
24+
* @param {string} name
25+
* @returns {string}
26+
*/
27+
public get(name: string): string {
28+
return process.env[name]?.trim() || '';
29+
}
30+
31+
/**
32+
* It returns the value of the POWERTOOLS_SERVICE_NAME environment variable.
33+
*
34+
* @returns {string}
35+
*/
36+
public getServiceName(): string {
37+
return this.get(this.serviceNameVariable);
38+
}
39+
40+
/**
41+
* It returns the value of the _X_AMZN_TRACE_ID environment variable.
42+
*
43+
* The AWS X-Ray Trace data available in the environment variable has this format:
44+
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
45+
*
46+
* The actual Trace ID is: `1-5759e988-bd862e3fe1be46a994272793`.
47+
*
48+
* @returns {string}
49+
*/
50+
public getXrayTraceId(): string {
51+
const xRayTraceId = this.get(this.xRayTraceIdVariable);
52+
53+
return xRayTraceId.split(';')[0].replace('Root=', '');
54+
}
55+
56+
}
57+
58+
export {
59+
EnvironmentVariablesService,
60+
};

packages/commons/src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './ConfigService';
2+
export * from './EnvironmentVariablesService';

packages/commons/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './utils/lambda';
22
export * from './Utility';
3+
export * from './config';
34
export * as ContextExamples from './samples/resources/contexts';
45
export * as Events from './samples/resources/events';
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Test EnvironmentVariablesService class
3+
*
4+
* @group unit/commons/all
5+
*/
6+
7+
import { EnvironmentVariablesService } from '../../../src/config';
8+
9+
describe('Class: EnvironmentVariablesService', () => {
10+
11+
const ENVIRONMENT_VARIABLES = process.env;
12+
13+
beforeEach(() => {
14+
jest.resetModules();
15+
process.env = { ...ENVIRONMENT_VARIABLES };
16+
});
17+
18+
afterAll(() => {
19+
process.env = ENVIRONMENT_VARIABLES;
20+
});
21+
22+
describe('Method: get', () => {
23+
24+
test('When the variable IS present, it returns the value of a runtime variable', () => {
25+
26+
// Prepare
27+
process.env.CUSTOM_VARIABLE = 'my custom value';
28+
const service = new EnvironmentVariablesService();
29+
30+
// Act
31+
const value = service.get('CUSTOM_VARIABLE');
32+
33+
// Assess
34+
expect(value).toEqual('my custom value');
35+
36+
});
37+
38+
test('When the variable IS NOT present, it returns an empty string', () => {
39+
40+
// Prepare
41+
delete process.env.CUSTOM_VARIABLE;
42+
const service = new EnvironmentVariablesService();
43+
44+
// Act
45+
const value = service.get('CUSTOM_VARIABLE');
46+
47+
// Assess
48+
expect(value).toEqual('');
49+
50+
});
51+
52+
});
53+
54+
describe('Method: getServiceName', () => {
55+
56+
test('It returns the value of the environment variable POWERTOOLS_SERVICE_NAME', () => {
57+
58+
// Prepare
59+
process.env.POWERTOOLS_SERVICE_NAME = 'shopping-cart-api';
60+
const service = new EnvironmentVariablesService();
61+
62+
// Act
63+
const value = service.getServiceName();
64+
65+
// Assess
66+
expect(value).toEqual('shopping-cart-api');
67+
});
68+
69+
});
70+
71+
describe('Method: getXrayTraceId', () => {
72+
73+
test('It returns the value of the environment variable _X_AMZN_TRACE_ID', () => {
74+
75+
// Prepare
76+
process.env._X_AMZN_TRACE_ID = 'abcd123456789';
77+
const service = new EnvironmentVariablesService();
78+
79+
// Act
80+
const value = service.getXrayTraceId();
81+
82+
// Assess
83+
expect(value).toEqual('abcd123456789');
84+
});
85+
86+
});
87+
88+
});

packages/logger/src/Logger.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class Logger extends Utility implements ClassThatLogs {
117117

118118
private static readonly defaultServiceName: string = 'service_undefined';
119119

120-
private envVarsService?: EnvironmentVariablesService;
120+
// envVarsService is always initialized in the constructor in setOptions()
121+
private envVarsService!: EnvironmentVariablesService;
121122

122123
private logEvent: boolean = false;
123124

@@ -455,7 +456,7 @@ class Logger extends Utility implements ClassThatLogs {
455456
logLevel,
456457
timestamp: new Date(),
457458
message: typeof input === 'string' ? input : input.message,
458-
xRayTraceId: this.getXrayTraceId(),
459+
xRayTraceId: this.envVarsService.getXrayTraceId(),
459460
}, this.getPowertoolLogData());
460461

461462
const logItem = new LogItem({
@@ -545,23 +546,6 @@ class Logger extends Utility implements ClassThatLogs {
545546
return <number> this.powertoolLogData.sampleRateValue;
546547
}
547548

548-
/**
549-
* It returns the current X-Ray Trace ID parsing the content of the `_X_AMZN_TRACE_ID` env variable.
550-
*
551-
* The X-Ray Trace data available in the environment variable has this format:
552-
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
553-
*
554-
* The actual Trace ID is: `1-5759e988-bd862e3fe1be46a994272793`.
555-
*
556-
* @private
557-
* @returns {string}
558-
*/
559-
private getXrayTraceId(): string {
560-
const xRayTraceId = this.getEnvVarsService().getXrayTraceId();
561-
562-
return xRayTraceId.length > 0 ? xRayTraceId.split(';')[0].replace('Root=', '') : xRayTraceId;
563-
}
564-
565549
/**
566550
* It returns true if the provided log level is valid.
567551
*

packages/logger/src/config/ConfigService.ts

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

packages/logger/src/config/EnvironmentVariablesService.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ConfigService } from '.';
1+
import { ConfigServiceInterface } from './ConfigServiceInterface';
2+
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
23

34
/**
45
* Class EnvironmentVariablesService
@@ -9,28 +10,22 @@ import { ConfigService } from '.';
910
* variables that can be set by the developer additionally.
1011
*
1112
* @class
12-
* @extends {ConfigService}
13+
* @extends {CommonEnvironmentVariablesService}
14+
* @implements {ConfigServiceInterface}
1315
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
1416
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables
1517
*/
16-
class EnvironmentVariablesService extends ConfigService {
18+
class EnvironmentVariablesService extends CommonEnvironmentVariablesService implements ConfigServiceInterface {
1719

1820
// Reserved environment variables
1921
private awsRegionVariable = 'AWS_REGION';
22+
private currentEnvironmentVariable = 'ENVIRONMENT';
2023
private functionNameVariable = 'AWS_LAMBDA_FUNCTION_NAME';
2124
private functionVersionVariable = 'AWS_LAMBDA_FUNCTION_VERSION';
25+
private logEventVariable = 'POWERTOOLS_LOGGER_LOG_EVENT';
26+
private logLevelVariable = 'LOG_LEVEL';
2227
private memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE';
23-
private xRayTraceIdVariable = '_X_AMZN_TRACE_ID';
24-
25-
/**
26-
* It returns the value of an environment variable that has given name.
27-
*
28-
* @param {string} name
29-
* @returns {string}
30-
*/
31-
public get(name: string): string {
32-
return process.env[name]?.trim() || '';
33-
}
28+
private sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE';
3429

3530
/**
3631
* It returns the value of the AWS_REGION environment variable.
@@ -85,7 +80,9 @@ class EnvironmentVariablesService extends ConfigService {
8580
* @returns {boolean}
8681
*/
8782
public getLogEvent(): boolean {
88-
return this.isValueTrue(this.get(this.logEventVariable));
83+
const value = this.get(this.logEventVariable);
84+
85+
return value.toLowerCase() === 'true' || value === '1';
8986
}
9087

9188
/**
@@ -123,7 +120,7 @@ class EnvironmentVariablesService extends ConfigService {
123120
* @returns {string}
124121
*/
125122
public getXrayTraceId(): string {
126-
return this.get(this.xRayTraceIdVariable);
123+
return super.getXrayTraceId();
127124
}
128125

129126
}

packages/logger/src/config/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './ConfigService';
21
export * from './ConfigServiceInterface';
32
export * from './EnvironmentVariablesService';

0 commit comments

Comments
 (0)