-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nestjs): Check arguments before instrumenting with @Injectable
#13544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a703dc4
7a154f9
7f7bac6
5233460
4f2d735
5f79bc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,15 @@ import { | |
import { getActiveSpan, startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from '@sentry/core'; | ||
import type { Span } from '@sentry/types'; | ||
import { SDK_VERSION, addNonEnumerableProperty, isThenable } from '@sentry/utils'; | ||
import { getMiddlewareSpanOptions, instrumentObservable, isPatched } from './helpers'; | ||
import type { CallHandler, CatchTarget, InjectableTarget, MinimalNestJsExecutionContext, Observable } from './types'; | ||
import { getMiddlewareSpanOptions, getNextProxy, instrumentObservable, isPatched } from './helpers'; | ||
import type { | ||
CallHandler, | ||
CatchTarget, | ||
InjectableTarget, | ||
MinimalNestJsExecutionContext, | ||
NextFunction, | ||
Observable, | ||
} from './types'; | ||
|
||
const supportedVersions = ['>=8.0.0 <11']; | ||
|
||
|
@@ -103,21 +110,16 @@ export class SentryNestInstrumentation extends InstrumentationBase { | |
const [req, res, next, ...args] = argsUse; | ||
const prevSpan = getActiveSpan(); | ||
|
||
return startSpanManual(getMiddlewareSpanOptions(target), (span: Span) => { | ||
const nextProxy = new Proxy(next, { | ||
apply: (originalNext, thisArgNext, argsNext) => { | ||
span.end(); | ||
|
||
if (prevSpan) { | ||
return withActiveSpan(prevSpan, () => { | ||
return Reflect.apply(originalNext, thisArgNext, argsNext); | ||
}); | ||
} else { | ||
return Reflect.apply(originalNext, thisArgNext, argsNext); | ||
} | ||
}, | ||
}); | ||
// Check that we can reasonably assume that the target is a middleware. | ||
// Without this guard, instrumentation will fail if a function named 'use' on a service, which is | ||
// decorated with @Injectable, is called. | ||
if (!(next satisfies NextFunction)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just fyi, satisfies is a type annotation and has no effect during runtime. What was your intention with this if statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To early-return before we try to proxy next. Thanks for the hint, interesting that it does seem to work nevertheless. I added tests first to verify the behavior, then introduced this check and that fixed it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's check for truthyness of |
||
return originalUse.apply(thisArgUse, argsUse); | ||
} | ||
|
||
return startSpanManual(getMiddlewareSpanOptions(target), (span: Span) => { | ||
// proxy next to end span on call | ||
const nextProxy = getNextProxy(next, span, prevSpan); | ||
return originalUse.apply(thisArgUse, [req, res, nextProxy, args]); | ||
}); | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably also move this line below all the guarding.