Created
January 28, 2025 22:23
-
-
Save emmanuelnk/b8ee27169930d44ff84fd3aaf5421802 to your computer and use it in GitHub Desktop.
Nest NewRelic Observability Interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Injectable, | |
NestInterceptor, | |
ExecutionContext, | |
CallHandler, | |
} from '@nestjs/common'; | |
import { Observable } from 'rxjs'; | |
import { tap } from 'rxjs/operators'; | |
// eslint-disable-next-line @typescript-eslint/no-var-requires | |
const newrelic = require('newrelic'); | |
@Injectable() | |
export class ObservabilityInterceptor implements NestInterceptor { | |
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
return newrelic.startWebTransaction(context.getHandler().name, () => { | |
const transaction = this.getTransaction(); | |
// Add custom attributes here | |
// newrelic.addCustomAttribute('someAttr', someAttr); | |
return next.handle().pipe( | |
tap(() => { | |
this.endTransaction(transaction); | |
}), | |
); | |
}); | |
} | |
getTransaction(): any { | |
return newrelic.getTransaction(); | |
} | |
endTransaction(transaction: any): any { | |
return transaction.end(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment