|
| 1 | +### Sentry |
| 2 | + |
| 3 | +[Sentry](https://sentry.io) is an error tracking and performance monitoring platform that helps developers identify and fix issues in real-time. This recipe shows how to integrate Sentry's [NestJS SDK](https://docs.sentry.io/platforms/javascript/guides/nestjs/) with your NestJS application. |
| 4 | + |
| 5 | +#### Installation |
| 6 | + |
| 7 | +First, install the required dependencies: |
| 8 | + |
| 9 | +```bash |
| 10 | +$ npm install --save @sentry/nestjs @sentry/profiling-node |
| 11 | +``` |
| 12 | + |
| 13 | +> info **Hint** `@sentry/profiling-node` is optional, but recommended for performance profiling. |
| 14 | +
|
| 15 | +#### Basic setup |
| 16 | + |
| 17 | +To get started with Sentry, you'll need to create a file named `instrument.js` that should be imported before any other modules in your application: |
| 18 | + |
| 19 | +```typescript |
| 20 | +@@filename(instrument) |
| 21 | +const Sentry = require("@sentry/nestjs"); |
| 22 | +const { nodeProfilingIntegration } = require("@sentry/profiling-node"); |
| 23 | + |
| 24 | +// Ensure to call this before requiring any other modules! |
| 25 | +Sentry.init({ |
| 26 | + dsn: SENTRY_DSN, |
| 27 | + integrations: [ |
| 28 | + // Add our Profiling integration |
| 29 | + nodeProfilingIntegration(), |
| 30 | + ], |
| 31 | + |
| 32 | + // Add Tracing by setting tracesSampleRate |
| 33 | + // We recommend adjusting this value in production |
| 34 | + tracesSampleRate: 1.0, |
| 35 | + |
| 36 | + // Set sampling rate for profiling |
| 37 | + // This is relative to tracesSampleRate |
| 38 | + profilesSampleRate: 1.0, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +Update your `main.ts` file to import `instrument.js` before other imports: |
| 43 | + |
| 44 | +```typescript |
| 45 | +@@filename(main) |
| 46 | +// Import this first! |
| 47 | +import "./instrument"; |
| 48 | + |
| 49 | +// Now import other modules |
| 50 | +import { NestFactory } from "@nestjs/core"; |
| 51 | +import { AppModule } from "./app.module"; |
| 52 | + |
| 53 | +async function bootstrap() { |
| 54 | + const app = await NestFactory.create(AppModule); |
| 55 | + await app.listen(3000); |
| 56 | +} |
| 57 | + |
| 58 | +bootstrap(); |
| 59 | +``` |
| 60 | + |
| 61 | +Afterwards, add the `SentryModule` as a root module to your main module: |
| 62 | + |
| 63 | +```typescript |
| 64 | +@@filename(app.module) |
| 65 | +import { Module } from "@nestjs/common"; |
| 66 | +import { SentryModule } from "@sentry/nestjs/setup"; |
| 67 | +import { AppController } from "./app.controller"; |
| 68 | +import { AppService } from "./app.service"; |
| 69 | + |
| 70 | +@Module({ |
| 71 | + imports: [ |
| 72 | + SentryModule.forRoot(), |
| 73 | + // ...other modules |
| 74 | + ], |
| 75 | + controllers: [AppController], |
| 76 | + providers: [AppService], |
| 77 | +}) |
| 78 | +export class AppModule {} |
| 79 | +``` |
| 80 | + |
| 81 | +#### Exception handling |
| 82 | + |
| 83 | +If you're 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 a `@Catch()` decorator without arguments), add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. This decorator will report all unexpected errors that are received by your global error filter to Sentry: |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { Catch, ExceptionFilter } from '@nestjs/common'; |
| 87 | +import { SentryExceptionCaptured } from '@sentry/nestjs'; |
| 88 | + |
| 89 | +@Catch() |
| 90 | +export class YourCatchAllExceptionFilter implements ExceptionFilter { |
| 91 | + @SentryExceptionCaptured() |
| 92 | + catch(exception, host): void { |
| 93 | + // your implementation here |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. `HttpExceptions` (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles. |
| 99 | + |
| 100 | +If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. This filter will report any unhandled errors that aren't caught by other error filters to Sentry. |
| 101 | + |
| 102 | +> warning **Warning** The `SentryGlobalFilter` needs to be registered before any other exception filters. |
| 103 | +
|
| 104 | +```typescript |
| 105 | +@@filename(app.module) |
| 106 | +import { Module } from "@nestjs/common"; |
| 107 | +import { APP_FILTER } from "@nestjs/core"; |
| 108 | +import { SentryGlobalFilter } from "@sentry/nestjs/setup"; |
| 109 | + |
| 110 | +@Module({ |
| 111 | + providers: [ |
| 112 | + { |
| 113 | + provide: APP_FILTER, |
| 114 | + useClass: SentryGlobalFilter, |
| 115 | + }, |
| 116 | + // ..other providers |
| 117 | + ], |
| 118 | +}) |
| 119 | +export class AppModule {} |
| 120 | +``` |
| 121 | + |
| 122 | +#### Readable stack traces |
| 123 | + |
| 124 | +Depending on how you've set up your project, the stack traces in your Sentry errors probably won't look like your actual code. |
| 125 | + |
| 126 | +To fix this, upload your source maps to Sentry. The easiest way to do this is by using the Sentry Wizard: |
| 127 | + |
| 128 | +```bash |
| 129 | +npx @sentry/wizard@latest -i sourcemaps |
| 130 | +``` |
| 131 | + |
| 132 | +#### Testing the integration |
| 133 | + |
| 134 | +To verify your Sentry integration is working, you can add a test endpoint that throws an error: |
| 135 | + |
| 136 | +```typescript |
| 137 | +@Get("debug-sentry") |
| 138 | +getError() { |
| 139 | + throw new Error("My first Sentry error!"); |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +Visit `/debug-sentry` in your application, and you should see the error appear in your Sentry dashboard. |
| 144 | + |
| 145 | +### Summary |
| 146 | + |
| 147 | +For complete documentation about Sentry's NestJS SDK, including advanced configuration options and features, visit the [official Sentry documentation](https://docs.sentry.io/platforms/javascript/guides/nestjs/). |
| 148 | + |
| 149 | +While software bugs are Sentry's thing, we still write them. If you come across any problems while installing our SDK, please open a [GitHub Issue](https://github.com/getsentry/sentry-javascript/issues) or reach out on [Discord](https://discord.com/invite/sentry). |
0 commit comments