Skip to content

Latest commit

 

History

History
 
 

nestjs

README.md

Sentry

Official Sentry SDK for NestJS (EXPERIMENTAL)

npm version npm dm npm dt

This SDK is considered experimental and in an alpha state. It may experience breaking changes. Please reach out on GitHub if you have any feedback or concerns.

Installation

npm install @sentry/nestjs

# Or yarn
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 this first!
import './instrument';

// Now import other modules
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();

Then you can add the SentryModule as a root module:

import { Module } from '@nestjs/common';
import { SentryModule } from '@sentry/nestjs/setup';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    SentryModule.forRoot(),
    // ...other modules
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

The SentryModule needs to be registered before any module that should be instrumented by Sentry.

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() {
    // Your business logic here
  }
}

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/types';

const monitorConfig: MonitorConfig = {
  schedule: {
    type: 'crontab',
    value: '* * * * *',
  },
  checkinMargin: 2, // In minutes. Optional.
  maxRuntime: 10, // In minutes. Optional.
  timezone: 'America/Los_Angeles', // Optional.
};

export class MyCronService {
  @Cron('* * * * *')
  @SentryCron('my-monitor-slug', monitorConfig)
  handleCron() {
    // Your cron job logic here
  }
}

Links