From 88ae5f1f6bde622214b99f4241d0826a747d6b43 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 27 Oct 2022 10:51:37 +0200 Subject: [PATCH] chore: updated unit test case --- .../layer-publisher.test.ts.snap | 2 +- .../src/config/EnvironmentVariablesService.ts | 4 ++- .../EnvironmentVariablesService.test.ts | 25 +++++++++++++++++++ packages/tracer/src/Tracer.ts | 2 +- packages/tracer/src/TracerInterface.ts | 2 +- 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/layer-publisher/tests/unit/__snapshots__/layer-publisher.test.ts.snap b/layer-publisher/tests/unit/__snapshots__/layer-publisher.test.ts.snap index 161eb78d35..9e6b8f7ad7 100644 --- a/layer-publisher/tests/unit/__snapshots__/layer-publisher.test.ts.snap +++ b/layer-publisher/tests/unit/__snapshots__/layer-publisher.test.ts.snap @@ -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", diff --git a/packages/commons/src/config/EnvironmentVariablesService.ts b/packages/commons/src/config/EnvironmentVariablesService.ts index d2716d6c72..380bbc0458 100644 --- a/packages/commons/src/config/EnvironmentVariablesService.ts +++ b/packages/commons/src/config/EnvironmentVariablesService.ts @@ -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=', ''); } diff --git a/packages/commons/tests/unit/config/EnvironmentVariablesService.test.ts b/packages/commons/tests/unit/config/EnvironmentVariablesService.test.ts index 33177252fa..692a5b1ff0 100644 --- a/packages/commons/tests/unit/config/EnvironmentVariablesService.test.ts +++ b/packages/commons/tests/unit/config/EnvironmentVariablesService.test.ts @@ -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); + }); }); diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index a583452025..09ac5e3314 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -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(); } diff --git a/packages/tracer/src/TracerInterface.ts b/packages/tracer/src/TracerInterface.ts index fd94db45cd..128a2c5553 100644 --- a/packages/tracer/src/TracerInterface.ts +++ b/packages/tracer/src/TracerInterface.ts @@ -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