#Interceptor on cron service

7 messages · Page 1 of 1 (latest)

spare panther
#

I have created a cron setup on a service, and I have the following Interceptor setup for Sentry.io profiler:

@Injectable()
export class SentryProfilerInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        // Start a new Sentry transaction for profiling
        const transaction = Sentry.startTransaction({
            op: context.getClass().name,
            name: context.getHandler().name,
        });

        // Set transaction on scope to associate with errors and get included span instrumentation
        // If there's currently an unfinished transaction, it may be dropped
        Sentry.getCurrentScope().setSpan(transaction);

        // Execute the request handler
        return next.handle().pipe(
            tap({
                next: () => {
                    transaction.setStatus(Sentry.spanStatusfromHttpCode(200));
                    transaction.finish();
                },
                error: () => {
                    transaction.setStatus(Sentry.spanStatusfromHttpCode(500));
                    transaction.finish();
                },
            }),
        );
    }
}

I want to record the performance on my cron, but it seems that I cannot use interceptor on services?

#

I could manually start a profiling transaction in my service code, but then I will have a lot of repeating code, hence I am checking if interceptor would help or not

wise dew
#

Interceptors (and other Enhancers) only bind to external entry points to the server (gateways, controllers, and resolvers). Cron jobs and queues are not considered external, and therefore don't get the enhancer bindings

spare panther
meager echo
#

either a js decorator or the actual decorator pattern e.g.

@Track()
class MyService {}

// or
class MyService {}

const provider: Provider = {
  provide: MyService,
  useClass: Decorate(MyService)
}

class Decorate { /* ... */ }

The former would be easier if the contracts are different. Could even make @Track() work on methods instead of a whole class e.g.

class MyService {
  @Track()
  doSomething() {}
}
spare panther