import { Injectable , NestInterceptor , ExecutionContext , CallHandler , } from '@nestjs/common' ; import { Observable } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import * as Constants from '@nestjs/common/constants' ; import * as Sentry from '@sentry/node' ; import { Reflector } from '@nestjs/core' ; @ Injectable ( ) export class ApmInterceptor implements NestInterceptor { constructor ( private readonly reflector ) { } intercept ( context , next ) { this . addRouteToSentry ( context ) ; return next . handle ( ) . pipe ( catchError ( ( error ) => { Sentry . captureException ( error ) ; throw error ; } ) ) ; } addRouteToSentry ( context ) { const scope = Sentry . getCurrentHub ( ) . getScope ( ) ; if ( scope ) { const transaction = scope . getTransaction ( ) ; if ( transaction ) { const classPath = this . reflector . get < string > ( Constants . PATH_METADATA , context . getClass ( ) ) ; const handlerPath = this . reflector . get < string > ( Constants . PATH_METADATA , context . getHandler ( ) ) . replace ( /^\// , '' ) ; const method = context . switchToHttp ( ) . getRequest ( ) . method ; transaction . name = `${ method } /api/v1/${ classPath }/${ handlerPath }` ; } } } }