Skip to content

Commit 0f122a1

Browse files
authored
fix(nestjs): Preserve original function name on SentryTraced functions (#13684)
1 parent 8dd3de1 commit 0f122a1

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,9 @@ export class AppController {
116116
testServiceWithCanActivate() {
117117
return this.appService.canActivate();
118118
}
119+
120+
@Get('test-function-name')
121+
testFunctionName() {
122+
return this.appService.getFunctionName();
123+
}
119124
}

dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export class AppService {
5858
return { result: 'test' };
5959
}
6060

61+
@SentryTraced('return the function name')
62+
getFunctionName(): { result: string } {
63+
return { result: this.getFunctionName.name };
64+
}
65+
6166
async testSpanDecoratorSync() {
6267
const returned = this.getString();
6368
// Will fail if getString() is async, because returned will be a Promise<>

dev-packages/e2e-tests/test-applications/nestjs-basic/tests/span-decorator.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ test('Transaction includes span and correct value for decorated sync function',
7070
]),
7171
);
7272
});
73+
74+
test('preserves original function name on decorated functions', async ({ baseURL }) => {
75+
const response = await fetch(`${baseURL}/test-function-name`);
76+
const body = await response.json();
77+
78+
expect(body.result).toEqual('getFunctionName');
79+
});

packages/nestjs/src/decorators/sentry-traced.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ export function SentryTraced(op: string = 'function') {
2020
},
2121
);
2222
};
23+
24+
// preserve the original name on the decorated function
25+
Object.defineProperty(descriptor.value, 'name', {
26+
value: originalMethod.name,
27+
configurable: true,
28+
enumerable: true,
29+
writable: true,
30+
});
31+
2332
return descriptor;
2433
};
2534
}

0 commit comments

Comments
 (0)