Skip to content

Commit

Permalink
fix(nestjs): Preserve original function name on SentryTraced functi…
Browse files Browse the repository at this point in the history
…ons (#13684)
  • Loading branch information
chargome committed Sep 13, 2024
1 parent 8dd3de1 commit 0f122a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ export class AppController {
testServiceWithCanActivate() {
return this.appService.canActivate();
}

@Get('test-function-name')
testFunctionName() {
return this.appService.getFunctionName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
9 changes: 9 additions & 0 deletions packages/nestjs/src/decorators/sentry-traced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

0 comments on commit 0f122a1

Please sign in to comment.