|
| 1 | +### Sentry |
| 2 | + |
| 3 | +[Sentry](https://sentry.io) is an error monitoring and performance tracking platform that helps developers identify and fix issues in real-time. This recipe shows how to integrate Sentry with your NestJS application for error monitoring and distributed tracing. |
| 4 | + |
| 5 | +#### Installation |
| 6 | + |
| 7 | +First, install the required dependencies: |
| 8 | + |
| 9 | + |
| 10 | +```bash |
| 11 | +$ npm install --save @sentry/nestjs @sentry/profiling-node |
| 12 | +``` |
| 13 | + |
| 14 | + |
| 15 | +#### Basic Setup |
| 16 | + |
| 17 | +To get started with Sentry, you'll need to create an initialization file (e.g., `sentry.init.ts`) that should be imported before any other modules in your application: |
| 18 | + |
| 19 | +```typescript |
| 20 | +import as Sentry from '@sentry/nestjs'; |
| 21 | +import { nodeProfilingIntegration } from '@sentry/profiling-node'; |
| 22 | +Sentry.init({ |
| 23 | +dsn: 'your-sentry-dsn', |
| 24 | +integrations: [ |
| 25 | +nodeProfilingIntegration(), |
| 26 | +], |
| 27 | +// Set sampling rate for tracing (adjust in production) |
| 28 | +tracesSampleRate: 1.0, |
| 29 | +// Set sampling rate for profiling |
| 30 | +profilesSampleRate: 1.0, |
| 31 | +}); |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +Update your `main.ts` file to import the Sentry initialization before other imports: |
| 37 | + |
| 38 | + |
| 39 | +```typescript |
| 40 | +// Import Sentry initialization first |
| 41 | +import './sentry.init'; |
| 42 | +import { NestFactory } from '@nestjs/core'; |
| 43 | +import { AppModule } from './app.module'; |
| 44 | +async function bootstrap() { |
| 45 | +const app = await NestFactory.create(AppModule); |
| 46 | +await app.listen(3000); |
| 47 | +} |
| 48 | +bootstrap(); |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +#### Module Integration |
| 53 | + |
| 54 | +Add the SentryModule to your application's root module: |
| 55 | + |
| 56 | + |
| 57 | +```typescript |
| 58 | +import { Module } from '@nestjs/common'; |
| 59 | +import { SentryModule } from '@sentry/nestjs/setup'; |
| 60 | +import { AppController } from './app.controller'; |
| 61 | +import { AppService } from './app.service'; |
| 62 | +@Module({ |
| 63 | +imports: [ |
| 64 | +SentryModule.forRoot(), |
| 65 | +// other modules... |
| 66 | +], |
| 67 | +controllers: [AppController], |
| 68 | +providers: [AppService], |
| 69 | +}) |
| 70 | +export class AppModule {} |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +#### Exception Handling |
| 75 | + |
| 76 | +To ensure Sentry captures unhandled exceptions, you can add the SentryGlobalFilter to your application module: |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { Module } from '@nestjs/common'; |
| 80 | +import { APP_FILTER } from '@nestjs/core'; |
| 81 | +import { SentryGlobalFilter } from '@sentry/nestjs/setup'; |
| 82 | +@Module({ |
| 83 | +providers: [ |
| 84 | +{ |
| 85 | +provide: APP_FILTER, |
| 86 | +useClass: SentryGlobalFilter, |
| 87 | +}, |
| 88 | +// other providers... |
| 89 | +], |
| 90 | +}) |
| 91 | +export class AppModule {} |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +For custom exception filters, you can use the `@SentryExceptionCaptured()` decorator: |
| 96 | + |
| 97 | +```typescript |
| 98 | +import { Catch, ExceptionFilter } from '@nestjs/common'; |
| 99 | +import { SentryExceptionCaptured } from '@sentry/nestjs'; |
| 100 | +@Catch() |
| 101 | +export class AllExceptionsFilter implements ExceptionFilter { |
| 102 | +@SentryExceptionCaptured() |
| 103 | +catch(exception: unknown, host: ArgumentsHost) { |
| 104 | +// Your exception handling logic |
| 105 | +} |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +#### Testing the Integration |
| 111 | + |
| 112 | +To verify your Sentry integration is working, you can add a test endpoint that throws an error: |
| 113 | + |
| 114 | +```typescript |
| 115 | +import { Controller, Get } from '@nestjs/common'; |
| 116 | +@Controller() |
| 117 | +export class AppController { |
| 118 | +@Get('debug-sentry') |
| 119 | +testSentry() { |
| 120 | +throw new Error('Test Sentry Integration!'); |
| 121 | +} |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | +Visit `/debug-sentry` in your application, and you should see the error appear in your Sentry dashboard. |
| 127 | + |
| 128 | +> info **Hint** For complete documentation about Sentry's NestJS integration, including advanced configuration options and features, visit the [official Sentry documentation](https://docs.sentry.io/platforms/javascript/guides/nestjs/). |
| 129 | +
|
0 commit comments