Skip to content
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

Instrumentation of NestJS interceptors #13033

Closed
Tracked by #12504
nicohrubec opened this issue Jul 24, 2024 · 2 comments · Fixed by #13264
Closed
Tracked by #12504

Instrumentation of NestJS interceptors #13033

nicohrubec opened this issue Jul 24, 2024 · 2 comments · Fixed by #13264
Assignees
Labels
Package: nestjs Issues related to the Sentry Nestjs SDK

Comments

@nicohrubec
Copy link
Contributor

nicohrubec commented Jul 24, 2024

https://docs.nestjs.com/interceptors

Interceptors are a bit harder than the other constructs we have instrumented so far. All interceptors get a CallHandler implementing a handle() method, which can be used to invoke the route execution. So interceptors basically wrap the execution of the route and therefore allow you to add logic before and after the route execution. Ideally, we would like to collect a span before the route and one span after the route execution for each interceptor.

Solution: According to the docs interceptors are annotated with the @Injectable decorator and implement a intercept method. We can use that to hook into the framework.

Before Route: This worked without any issues. We can start a span shortly before calling intercept() and then proxy CallHandler.next() to end the span before the route is actually executed.

After Route: This is much harder. After the route execution is done an rxjs Observable is returned, which users can chain arbitrary operations to, which in turn might lead to new Observable objects being created etc. We ran into various issues trying to make these spans work for individual interceptors. In the end we simplified the approach and now we just create one span that traces anything that happens after the route execution. Specifically this means that if there are multiple interceptors with logic after the rout execution, we only send exactly one span to Sentry encapsulating the logic for all these interceptors.

Limitation: Although the nest docs say that interceptors should always have an @Injectable decorator, interceptors work just fine without it unless it has dependencies on other services.

@nicohrubec nicohrubec self-assigned this Jul 24, 2024
@nicohrubec nicohrubec added the Package: nestjs Issues related to the Sentry Nestjs SDK label Jul 24, 2024
@rubiin
Copy link

rubiin commented Aug 25, 2024

@nicohrubec
Copy link
Contributor Author

Hey @rubiin, thanks for the suggestion. However, I think this is different to what we are trying to achieve here. The goal of this ticket is not to capture exceptions but to get performance tracing for operations that happen within user-defined interceptors. The error reporting is currently implemented with global error filters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nestjs Issues related to the Sentry Nestjs SDK
Projects
Archived in project
3 participants