-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(all): moved EnvService to commons + exposed getXrayTraceId in tr…
…acer (#1123) * feat: moved EnvService to commons + exposed getXrayTraceId in tracer * docs: added docs section for getRootXrayTraceId * Update docs/core/tracer.md Co-authored-by: Josh Kellendonk <misterjoshua@users.noreply.github.com> * fix: remove reduntant methods * chore: updated unit test case Co-authored-by: Josh Kellendonk <misterjoshua@users.noreply.github.com>
- Loading branch information
1 parent
6dc2bef
commit c8e3c15
Showing
25 changed files
with
332 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Abstract class ConfigService | ||
* | ||
* This class defines common methods and variables that can be set by the developer | ||
* in the runtime. | ||
* | ||
* @class | ||
* @abstract | ||
*/ | ||
abstract class ConfigService { | ||
|
||
/** | ||
* It returns the value of an environment variable that has given name. | ||
* | ||
* @param {string} name | ||
* @returns {string} | ||
*/ | ||
public abstract get(name: string): string; | ||
|
||
/** | ||
* It returns the value of the POWERTOOLS_SERVICE_NAME environment variable. | ||
* | ||
* @returns {string} | ||
*/ | ||
public abstract getServiceName(): string; | ||
|
||
} | ||
|
||
export { | ||
ConfigService, | ||
}; |
67 changes: 67 additions & 0 deletions
67
packages/commons/src/config/EnvironmentVariablesService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ConfigService } from '.'; | ||
|
||
/** | ||
* Class EnvironmentVariablesService | ||
* | ||
* This class is used to return environment variables that are available in the runtime of | ||
* the current Lambda invocation. | ||
* These variables can be a mix of runtime environment variables set by AWS and | ||
* variables that can be set by the developer additionally. | ||
* | ||
* @class | ||
* @extends {ConfigService} | ||
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime | ||
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables | ||
*/ | ||
class EnvironmentVariablesService extends ConfigService { | ||
|
||
/** | ||
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables | ||
* @protected | ||
*/ | ||
protected serviceNameVariable = 'POWERTOOLS_SERVICE_NAME'; | ||
// Reserved environment variables | ||
private xRayTraceIdVariable = '_X_AMZN_TRACE_ID'; | ||
|
||
/** | ||
* It returns the value of an environment variable that has given name. | ||
* | ||
* @param {string} name | ||
* @returns {string} | ||
*/ | ||
public get(name: string): string { | ||
return process.env[name]?.trim() || ''; | ||
} | ||
|
||
/** | ||
* It returns the value of the POWERTOOLS_SERVICE_NAME environment variable. | ||
* | ||
* @returns {string} | ||
*/ | ||
public getServiceName(): string { | ||
return this.get(this.serviceNameVariable); | ||
} | ||
|
||
/** | ||
* It returns the value of the _X_AMZN_TRACE_ID environment variable. | ||
* | ||
* The AWS X-Ray Trace data available in the environment variable has this format: | ||
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`, | ||
* | ||
* The actual Trace ID is: `1-5759e988-bd862e3fe1be46a994272793`. | ||
* | ||
* @returns {string} | ||
*/ | ||
public getXrayTraceId(): string | undefined { | ||
const xRayTraceId = this.get(this.xRayTraceIdVariable); | ||
|
||
if (xRayTraceId === '') return undefined; | ||
|
||
return xRayTraceId.split(';')[0].replace('Root=', ''); | ||
} | ||
|
||
} | ||
|
||
export { | ||
EnvironmentVariablesService, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ConfigService'; | ||
export * from './EnvironmentVariablesService'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './utils/lambda'; | ||
export * from './Utility'; | ||
export * from './config'; | ||
export * as ContextExamples from './samples/resources/contexts'; | ||
export * as Events from './samples/resources/events'; |
113 changes: 113 additions & 0 deletions
113
packages/commons/tests/unit/config/EnvironmentVariablesService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* Test EnvironmentVariablesService class | ||
* | ||
* @group unit/commons/all | ||
*/ | ||
|
||
import { EnvironmentVariablesService } from '../../../src/config'; | ||
|
||
describe('Class: EnvironmentVariablesService', () => { | ||
|
||
const ENVIRONMENT_VARIABLES = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...ENVIRONMENT_VARIABLES }; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = ENVIRONMENT_VARIABLES; | ||
}); | ||
|
||
describe('Method: get', () => { | ||
|
||
test('When the variable IS present, it returns the value of a runtime variable', () => { | ||
|
||
// Prepare | ||
process.env.CUSTOM_VARIABLE = 'my custom value'; | ||
const service = new EnvironmentVariablesService(); | ||
|
||
// Act | ||
const value = service.get('CUSTOM_VARIABLE'); | ||
|
||
// Assess | ||
expect(value).toEqual('my custom value'); | ||
|
||
}); | ||
|
||
test('When the variable IS NOT present, it returns an empty string', () => { | ||
|
||
// Prepare | ||
delete process.env.CUSTOM_VARIABLE; | ||
const service = new EnvironmentVariablesService(); | ||
|
||
// Act | ||
const value = service.get('CUSTOM_VARIABLE'); | ||
|
||
// Assess | ||
expect(value).toEqual(''); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('Method: getServiceName', () => { | ||
|
||
test('It returns the value of the environment variable POWERTOOLS_SERVICE_NAME', () => { | ||
|
||
// Prepare | ||
process.env.POWERTOOLS_SERVICE_NAME = 'shopping-cart-api'; | ||
const service = new EnvironmentVariablesService(); | ||
|
||
// Act | ||
const value = service.getServiceName(); | ||
|
||
// Assess | ||
expect(value).toEqual('shopping-cart-api'); | ||
}); | ||
|
||
}); | ||
|
||
describe('Method: getXrayTraceId', () => { | ||
|
||
test('It returns the value of the environment variable _X_AMZN_TRACE_ID', () => { | ||
|
||
// Prepare | ||
process.env._X_AMZN_TRACE_ID = 'abcd123456789'; | ||
const service = new EnvironmentVariablesService(); | ||
|
||
// Act | ||
const value = service.getXrayTraceId(); | ||
|
||
// Assess | ||
expect(value).toEqual('abcd123456789'); | ||
}); | ||
test('It returns the value of the Root X-Ray segment ID properly formatted', () => { | ||
|
||
// Prepare | ||
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1'; | ||
const service = new EnvironmentVariablesService(); | ||
|
||
// Act | ||
const value = service.getXrayTraceId(); | ||
|
||
// Assess | ||
expect(value).toEqual('1-5759e988-bd862e3fe1be46a994272793'); | ||
}); | ||
|
||
test('It returns the value of the Root X-Ray segment ID properly formatted', () => { | ||
|
||
// Prepare | ||
delete process.env._X_AMZN_TRACE_ID; | ||
const service = new EnvironmentVariablesService(); | ||
|
||
// Act | ||
const value = service.getXrayTraceId(); | ||
|
||
// Assess | ||
expect(value).toEqual(undefined); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.