Skip to content

Commit

Permalink
chore: updated unit test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Oct 27, 2022
1 parent b553fea commit 88ae5f1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Object {
"S3Bucket": Object {
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
},
"S3Key": "c2f621503b147cecccf2e6cc6df420a8f11ee29ae042d2c1f523cf5db3f47ca2.zip",
"S3Key": "dbdb3f66eaeed03649521bf73dbcdd95a713086afccbac3f57fa407ffb76bdaa.zip",
},
"Description": "Lambda Powertools for TypeScript version 1.0.1",
"LayerName": "AWSLambdaPowertoolsTypeScript",
Expand Down
4 changes: 3 additions & 1 deletion packages/commons/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ class EnvironmentVariablesService extends ConfigService {
*
* @returns {string}
*/
public getXrayTraceId(): string {
public getXrayTraceId(): string | undefined {
const xRayTraceId = this.get(this.xRayTraceIdVariable);

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

return xRayTraceId.split(';')[0].replace('Root=', '');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ describe('Class: EnvironmentVariablesService', () => {
// 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);
});

});

Expand Down
2 changes: 1 addition & 1 deletion packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class Tracer extends Utility implements TracerInterface {
*
* @returns string - The root X-Ray trace id.
*/
public getRootXrayTraceId(): string {
public getRootXrayTraceId(): string | undefined {
return this.envVarsService.getXrayTraceId();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tracer/src/TracerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface TracerInterface {
captureLambdaHandler(options?: CaptureLambdaHandlerOptions): HandlerMethodDecorator
captureMethod(options?: CaptureMethodOptions): MethodDecorator
getSegment(): Segment | Subsegment
getRootXrayTraceId(): string
getRootXrayTraceId(): string | undefined
isTracingEnabled(): boolean
putAnnotation: (key: string, value: string | number | boolean) => void
putMetadata: (key: string, value: unknown, namespace?: string | undefined) => void
Expand Down

0 comments on commit 88ae5f1

Please sign in to comment.