-
Notifications
You must be signed in to change notification settings - Fork 146
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
feat(all): moved EnvService to commons + exposed getXrayTraceId in tracer #1123
Merged
dreamorosi
merged 5 commits into
main
from
1070-feature-tracer-access-to-the-root-trace-id
Oct 27, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8d3fa28
feat: moved EnvService to commons + exposed getXrayTraceId in tracer
dreamorosi a355f7b
docs: added docs section for getRootXrayTraceId
dreamorosi c2febb1
Update docs/core/tracer.md
dreamorosi b553fea
fix: remove reduntant methods
dreamorosi 88ae5f1
chore: updated unit test case
dreamorosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add better test for this logic: