Skip to content

Commit 75f7b93

Browse files
authoredMar 21, 2025··
perf(nestjs): Remove usage of addNonEnumerableProperty (#15766)
ref #15725 (comment) Similar to the work done in #15765 we can avoid usage of `addNonEnumerableProperty` with usage of a `WeakSet`.
1 parent ab16123 commit 75f7b93

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed
 

‎packages/nestjs/src/integrations/sentry-nest-instrumentation.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import type { Span } from '@sentry/core';
99
import {
1010
SDK_VERSION,
11-
addNonEnumerableProperty,
1211
getActiveSpan,
1312
isThenable,
1413
startInactiveSpan,
@@ -90,7 +89,9 @@ export class SentryNestInstrumentation extends InstrumentationBase {
9089
/**
9190
* Creates a wrapper function for the @Injectable decorator.
9291
*/
93-
private _createWrapInjectable() {
92+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
93+
private _createWrapInjectable(): (original: any) => (options?: unknown) => (target: InjectableTarget) => any {
94+
const SeenNestjsContextSet = new WeakSet<MinimalNestJsExecutionContext>();
9495
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9596
return function wrapInjectable(original: any) {
9697
return function wrappedInjectable(options?: unknown) {
@@ -197,8 +198,8 @@ export class SentryNestInstrumentation extends InstrumentationBase {
197198
return withActiveSpan(parentSpan, () => {
198199
const handleReturnObservable = Reflect.apply(originalHandle, thisArgHandle, argsHandle);
199200

200-
if (!context._sentryInterceptorInstrumented) {
201-
addNonEnumerableProperty(context, '_sentryInterceptorInstrumented', true);
201+
if (!SeenNestjsContextSet.has(context)) {
202+
SeenNestjsContextSet.add(context);
202203
afterSpan = startInactiveSpan(
203204
getMiddlewareSpanOptions(target, 'Interceptors - After Route'),
204205
);
@@ -209,8 +210,8 @@ export class SentryNestInstrumentation extends InstrumentationBase {
209210
} else {
210211
const handleReturnObservable = Reflect.apply(originalHandle, thisArgHandle, argsHandle);
211212

212-
if (!context._sentryInterceptorInstrumented) {
213-
addNonEnumerableProperty(context, '_sentryInterceptorInstrumented', true);
213+
if (!SeenNestjsContextSet.has(context)) {
214+
SeenNestjsContextSet.add(context);
214215
afterSpan = startInactiveSpan(getMiddlewareSpanOptions(target, 'Interceptors - After Route'));
215216
}
216217

0 commit comments

Comments
 (0)
Please sign in to comment.