What is @sentry/nestjs?
@sentry/nestjs is a package that provides integration of Sentry with NestJS applications. It allows developers to easily add error tracking and performance monitoring to their NestJS applications, helping them to identify and fix issues more efficiently.
What are @sentry/nestjs's main functionalities?
Error Tracking
This feature allows you to integrate Sentry's error tracking capabilities into your NestJS application. By providing your DSN (Data Source Name), you can automatically capture and report errors to Sentry.
import { Module } from '@nestjs/common';
import { SentryModule } from '@ntegral/nestjs-sentry';
@Module({
imports: [
SentryModule.forRoot({
dsn: 'your-dsn-url',
}),
],
})
export class AppModule {}
Performance Monitoring
This feature allows you to monitor the performance of your NestJS application by capturing the duration of requests and sending this information to Sentry. This can help in identifying performance bottlenecks.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import * as Sentry from '@sentry/node';
@Injectable()
export class SentryInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const now = Date.now();
return next
.handle()
.pipe(
tap(() => Sentry.captureMessage(`Request took ${Date.now() - now}ms`))
);
}
}
Other packages similar to @sentry/nestjs
@sentry/node
@sentry/node is the official Sentry SDK for Node.js applications. While it provides similar error tracking and performance monitoring capabilities, it is more generic and not specifically tailored for NestJS applications like @sentry/nestjs.
nestjs-rollbar
nestjs-rollbar is a package that integrates Rollbar with NestJS applications for error tracking. It offers similar functionalities to @sentry/nestjs but uses Rollbar as the error tracking service instead of Sentry.
nestjs-bugsnag
nestjs-bugsnag is a package that integrates Bugsnag with NestJS applications for error monitoring. It provides similar features to @sentry/nestjs but uses Bugsnag as the error monitoring tool.
Official Sentry SDK for NestJS

Installation
npm install @sentry/nestjs
yarn add @sentry/nestjs
Usage
Add an instrument.ts
file:
import * as Sentry from '@sentry/nestjs';
Sentry.init({
dsn: '__DSN__',
});
You need to require or import the instrument.ts
file before requiring any other modules in your application. This is
necessary to ensure that Sentry can automatically instrument all modules in your application:
import './instrument';
import * as Sentry from '@sentry/nestjs';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Afterwards, add the SentryModule
as a root module to your main module:
import { Module } from '@nestjs/common';
import { SentryModule } from '@sentry/nestjs/setup';
@Module({
imports: [
SentryModule.forRoot(),
],
})
export class AppModule {}
In case you are using a global catch-all exception filter (which is either a filter registered with
app.useGlobalFilters()
or a filter registered in your app module providers annotated with an empty @Catch()
decorator), add a @SentryExceptionCaptured()
decorator to the catch()
method of this global error filter. This
decorator will report all unexpected errors that are received by your global error filter to Sentry:
import { Catch, ExceptionFilter } from '@nestjs/common';
import { SentryExceptionCaptured } from '@sentry/nestjs';
@Catch()
export class YourCatchAllExceptionFilter implements ExceptionFilter {
@SentryExceptionCaptured()
catch(exception, host): void {
}
}
In case you do not have a global catch-all exception filter, add the SentryGlobalFilter
to the providers of your main
module. This filter will report all unhandled errors that are not caught by any other error filter to Sentry.
Important: The SentryGlobalFilter
needs to be registered before any other exception filters.
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { SentryGlobalFilter } from '@sentry/nestjs/setup';
@Module({
providers: [
{
provide: APP_FILTER,
useClass: SentryGlobalFilter,
},
],
})
export class AppModule {}
Note: In NestJS + GraphQL applications replace the SentryGlobalFilter
with the SentryGlobalGraphQLFilter
.
SentryTraced
Use the @SentryTraced()
decorator to gain additional performance insights for any function within your NestJS
applications.
import { Injectable } from '@nestjs/common';
import { SentryTraced } from '@sentry/nestjs';
@Injectable()
export class ExampleService {
@SentryTraced('example function')
async performTask() {
}
}
SentryCron
Use the @SentryCron()
decorator to augment the native NestJS @Cron
decorator to send check-ins to Sentry before and
after each cron job run.
import { Cron } from '@nestjs/schedule';
import { SentryCron, MonitorConfig } from '@sentry/nestjs';
import type { MonitorConfig } from '@sentry/core';
const monitorConfig: MonitorConfig = {
schedule: {
type: 'crontab',
value: '* * * * *',
},
checkinMargin: 2,
maxRuntime: 10,
timezone: 'America/Los_Angeles',
};
export class MyCronService {
@Cron('* * * * *')
@SentryCron('my-monitor-slug', monitorConfig)
handleCron() {
}
}
Links