Skip to content

Instrumentation of NestJS interceptors #13033

Closed
@nicohrubec

Description

@nicohrubec

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.

Metadata

Metadata

Assignees

Labels

Package: nestjsIssues related to the Sentry Nestjs SDK

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions