Skip to content

Instantly share code, notes, and snippets.

@emmanuelnk
Created January 28, 2025 22:23
Show Gist options
  • Save emmanuelnk/b8ee27169930d44ff84fd3aaf5421802 to your computer and use it in GitHub Desktop.
Save emmanuelnk/b8ee27169930d44ff84fd3aaf5421802 to your computer and use it in GitHub Desktop.
Nest NewRelic Observability Interceptor
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