From 9d4100feea1be23e0df21cdcee63025fef218c87 Mon Sep 17 00:00:00 2001 From: Aidan T Grace Date: Tue, 26 Jul 2022 16:50:14 +0100 Subject: [PATCH 1/6] Update Tracer.ts using the captureMethod decorator the "this" is no longer the original obj so changing to the target fixes this --- packages/tracer/src/Tracer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index a5c7aa1aaa..7a93c89454 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -424,7 +424,7 @@ class Tracer extends Utility implements TracerInterface { return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { let result; try { - result = await originalMethod.apply(this, [...args]); + result = await originalMethod.apply(target, [...args]); this.addResponseAsMetadata(result, originalMethod.name); } catch (error) { this.addErrorAsMetadata(error as Error); @@ -813,4 +813,4 @@ class Tracer extends Utility implements TracerInterface { export { Tracer -}; \ No newline at end of file +}; From b568f7d263e3cd7911a767b87192a86f568dccfd Mon Sep 17 00:00:00 2001 From: Aidan Grace Date: Thu, 28 Jul 2022 15:48:23 +0100 Subject: [PATCH 2/6] added test to verify change to captureMethod. --- packages/tracer/src/Tracer.ts | 18 ++++++------ packages/tracer/tests/unit/Tracer.test.ts | 36 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 7a93c89454..4f3329f024 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -415,20 +415,20 @@ class Tracer extends Utility implements TracerInterface { // The descriptor.value is the method this decorator decorates, it cannot be undefined. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const originalMethod = descriptor.value!; - - descriptor.value = (...args: unknown[]) => { + + descriptor.value = ((...args: unknown[]) => { if (!this.isTracingEnabled()) { - return originalMethod.apply(target, [...args]); + return originalMethod.apply(target, [ ...args ]); } - return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { - let result; + return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { + + let result: unknown; try { - result = await originalMethod.apply(target, [...args]); + result = await originalMethod.apply(target, [ ...args ]); this.addResponseAsMetadata(result, originalMethod.name); } catch (error) { - this.addErrorAsMetadata(error as Error); - + this.addErrorAsMetadata(error as Error); throw error; } finally { subsegment?.close(); @@ -437,7 +437,7 @@ class Tracer extends Utility implements TracerInterface { return result; }); - }; + }) ; return descriptor; }; diff --git a/packages/tracer/tests/unit/Tracer.test.ts b/packages/tracer/tests/unit/Tracer.test.ts index 6644e25a46..ed0681c018 100644 --- a/packages/tracer/tests/unit/Tracer.test.ts +++ b/packages/tracer/tests/unit/Tracer.test.ts @@ -973,6 +973,42 @@ describe('Class: Tracer', () => { }); + test('when used as decorator and when calling other methods/props in the class they are called in the orginal scope', async () => { + + // Prepare + const tracer: Tracer = new Tracer(); + const newSubsegment: Segment | Subsegment | undefined = new Subsegment('### dummyMethod'); + jest.spyOn(tracer.provider, 'getSegment') + .mockImplementation(() => newSubsegment); + setContextMissingStrategy(() => null); + + class Lambda implements LambdaInterface { + + public otherMethod(){ + return 'otherMethod'; + } + + @tracer.captureMethod() + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + public async dummyMethod(): Promise { + return `otherMethod:${this.otherMethod()}`; + } + + @tracer.captureLambdaHandler() + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + public async handler(_event: TEvent, _context: Context, _callback: Callback): void | Promise { + return (await this.dummyMethod() as unknown) + } + + } + + // Act / Assess + expect(await (new Lambda()).handler({}, context, () => console.log('Lambda invoked!'))).toEqual('otherMethod:otherMethod'); + + }); + }); describe('Method: captureAWS', () => { From c9707e895ebeb9f52bd75d2e8e826210490e3cc6 Mon Sep 17 00:00:00 2001 From: Aidan Grace Date: Thu, 28 Jul 2022 15:59:26 +0100 Subject: [PATCH 3/6] fix to code formting to pass tests --- packages/tracer/src/Tracer.ts | 4 ++-- packages/tracer/tests/unit/Tracer.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 4f3329f024..a7576aaee1 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -418,14 +418,14 @@ class Tracer extends Utility implements TracerInterface { descriptor.value = ((...args: unknown[]) => { if (!this.isTracingEnabled()) { - return originalMethod.apply(target, [ ...args ]); + return originalMethod.apply(target, [...args]); } return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { let result: unknown; try { - result = await originalMethod.apply(target, [ ...args ]); + result = await originalMethod.apply(target, [...args]); this.addResponseAsMetadata(result, originalMethod.name); } catch (error) { this.addErrorAsMetadata(error as Error); diff --git a/packages/tracer/tests/unit/Tracer.test.ts b/packages/tracer/tests/unit/Tracer.test.ts index ed0681c018..1a932b8814 100644 --- a/packages/tracer/tests/unit/Tracer.test.ts +++ b/packages/tracer/tests/unit/Tracer.test.ts @@ -984,7 +984,7 @@ describe('Class: Tracer', () => { class Lambda implements LambdaInterface { - public otherMethod(){ + public otherMethod(): string { return 'otherMethod'; } @@ -999,7 +999,7 @@ describe('Class: Tracer', () => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore public async handler(_event: TEvent, _context: Context, _callback: Callback): void | Promise { - return (await this.dummyMethod() as unknown) + return (await this.dummyMethod() as unknown); } } From f0481de89674003876fa68a45129213ac7dad80c Mon Sep 17 00:00:00 2001 From: Aidan Grace Date: Thu, 28 Jul 2022 16:12:40 +0100 Subject: [PATCH 4/6] reverted Tracer.ts and reedited for a clean commit --- packages/tracer/src/Tracer.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index a7576aaee1..a5c7aa1aaa 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -415,20 +415,20 @@ class Tracer extends Utility implements TracerInterface { // The descriptor.value is the method this decorator decorates, it cannot be undefined. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const originalMethod = descriptor.value!; - - descriptor.value = ((...args: unknown[]) => { + + descriptor.value = (...args: unknown[]) => { if (!this.isTracingEnabled()) { return originalMethod.apply(target, [...args]); } - return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { - - let result: unknown; + return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { + let result; try { - result = await originalMethod.apply(target, [...args]); + result = await originalMethod.apply(this, [...args]); this.addResponseAsMetadata(result, originalMethod.name); } catch (error) { - this.addErrorAsMetadata(error as Error); + this.addErrorAsMetadata(error as Error); + throw error; } finally { subsegment?.close(); @@ -437,7 +437,7 @@ class Tracer extends Utility implements TracerInterface { return result; }); - }) ; + }; return descriptor; }; @@ -813,4 +813,4 @@ class Tracer extends Utility implements TracerInterface { export { Tracer -}; +}; \ No newline at end of file From ec6f272c1543de99ca9f23638328bcf9f61bfd9a Mon Sep 17 00:00:00 2001 From: Aidan Grace Date: Thu, 28 Jul 2022 16:18:16 +0100 Subject: [PATCH 5/6] issue with the last commit, fixing Tracter again --- packages/tracer/src/Tracer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index a5c7aa1aaa..663b98afc7 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -424,7 +424,7 @@ class Tracer extends Utility implements TracerInterface { return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => { let result; try { - result = await originalMethod.apply(this, [...args]); + result = await originalMethod.apply(target, [...args]); this.addResponseAsMetadata(result, originalMethod.name); } catch (error) { this.addErrorAsMetadata(error as Error); From 35311df29e4e625b8885ce1b843736576a81189f Mon Sep 17 00:00:00 2001 From: Aidan Grace Date: Fri, 29 Jul 2022 09:57:51 +0100 Subject: [PATCH 6/6] fix lint issues --- packages/tracer/tests/unit/Tracer.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/tracer/tests/unit/Tracer.test.ts b/packages/tracer/tests/unit/Tracer.test.ts index 1a932b8814..0c6e086508 100644 --- a/packages/tracer/tests/unit/Tracer.test.ts +++ b/packages/tracer/tests/unit/Tracer.test.ts @@ -984,10 +984,6 @@ describe('Class: Tracer', () => { class Lambda implements LambdaInterface { - public otherMethod(): string { - return 'otherMethod'; - } - @tracer.captureMethod() // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -1002,6 +998,10 @@ describe('Class: Tracer', () => { return (await this.dummyMethod() as unknown); } + public otherMethod(): string { + return 'otherMethod'; + } + } // Act / Assess