From 0f122a153ee2ece925f41c850c233222748b1704 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Fri, 13 Sep 2024 11:56:55 +0200 Subject: [PATCH] fix(nestjs): Preserve original function name on `SentryTraced` functions (#13684) --- .../test-applications/nestjs-basic/src/app.controller.ts | 5 +++++ .../test-applications/nestjs-basic/src/app.service.ts | 5 +++++ .../nestjs-basic/tests/span-decorator.test.ts | 7 +++++++ packages/nestjs/src/decorators/sentry-traced.ts | 9 +++++++++ 4 files changed, 26 insertions(+) diff --git a/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.controller.ts b/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.controller.ts index 75308e8f0ea9..77e25a72dad5 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.controller.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.controller.ts @@ -116,4 +116,9 @@ export class AppController { testServiceWithCanActivate() { return this.appService.canActivate(); } + + @Get('test-function-name') + testFunctionName() { + return this.appService.getFunctionName(); + } } diff --git a/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.service.ts b/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.service.ts index 3e4639040a7e..72aef6947a6c 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.service.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.service.ts @@ -58,6 +58,11 @@ export class AppService { return { result: 'test' }; } + @SentryTraced('return the function name') + getFunctionName(): { result: string } { + return { result: this.getFunctionName.name }; + } + async testSpanDecoratorSync() { const returned = this.getString(); // Will fail if getString() is async, because returned will be a Promise<> diff --git a/dev-packages/e2e-tests/test-applications/nestjs-basic/tests/span-decorator.test.ts b/dev-packages/e2e-tests/test-applications/nestjs-basic/tests/span-decorator.test.ts index 4b3ea2c0ba40..ee7666a50f18 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs-basic/tests/span-decorator.test.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs-basic/tests/span-decorator.test.ts @@ -70,3 +70,10 @@ test('Transaction includes span and correct value for decorated sync function', ]), ); }); + +test('preserves original function name on decorated functions', async ({ baseURL }) => { + const response = await fetch(`${baseURL}/test-function-name`); + const body = await response.json(); + + expect(body.result).toEqual('getFunctionName'); +}); diff --git a/packages/nestjs/src/decorators/sentry-traced.ts b/packages/nestjs/src/decorators/sentry-traced.ts index b9ef861bc3b2..2f90e4dab5d9 100644 --- a/packages/nestjs/src/decorators/sentry-traced.ts +++ b/packages/nestjs/src/decorators/sentry-traced.ts @@ -20,6 +20,15 @@ export function SentryTraced(op: string = 'function') { }, ); }; + + // preserve the original name on the decorated function + Object.defineProperty(descriptor.value, 'name', { + value: originalMethod.name, + configurable: true, + enumerable: true, + writable: true, + }); + return descriptor; }; }