#How to wrap the call to `next.handle()` for tracing purposes?

2 messages · Page 1 of 1 (latest)

arctic heart
#

I'm using @opentelemtry/api library, and my goal is to write an interceptor that is going to start an active span for every route in the controller. I'm not familiar with RxJs and interceptors uses it. How can I wrap next.handle() call so I can start/stop the span correctly?
This is what I've tried, but this solution does not propagate the context correctly - every span created during controller exeuction is not nested into this active span that I'm creating in the interceptor...

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
  const tracer = trace.getTracer('default')
  
  tracer.startActiveSpan('test', (span) => {
    return next
      .handle()
      .pipe(
        tap(() => span.end()),
      );
  })
}```
ornate raptor
#

otel uses AsyncLocalStorage for context propagation. I had a similar problem with my library nestjs-cls when context would be lost in interceptors, but then this guy figured it out: https://github.com/Papooch/nestjs-cls/issues/5

GitHub

Hey there, just came across this library while investigating my own issues with AsyncLocalStorage losing context inside graphql interceptors. I'm not (yet) using this library, I have a custom i...