Skip to content

Commit b137b4a

Browse files
committed
1 parent 32203b0 commit b137b4a

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

content/recipes/sentry.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+

src/app/homepage/menu/menu.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export class MenuComponent implements OnInit {
246246
{ title: 'CQRS', path: '/recipes/cqrs' },
247247
{ title: 'Compodoc', path: '/recipes/documentation' },
248248
{ title: 'Prisma', path: '/recipes/prisma' },
249+
{ title: 'Sentry', path: '/recipes/sentry' },
249250
{ title: 'Serve static', path: '/recipes/serve-static' },
250251
{ title: 'Commander', path: '/recipes/nest-commander' },
251252
{ title: 'Async local storage', path: '/recipes/async-local-storage' },

src/app/homepage/pages/recipes/recipes.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { MikroOrmComponent } from './mikroorm/mikroorm.component';
1010
import { MongodbComponent } from './mongodb/mongodb.component';
1111
import { PrismaComponent } from './prisma/prisma.component';
1212
import { ReplComponent } from './repl/repl.component';
13+
import { SentryComponent } from './sentry/sentry.component';
1314
import { ServeStaticComponent } from './serve-static/serve-static.component';
1415
import { SqlSequelizeComponent } from './sql-sequelize/sql-sequelize.component';
1516
import { SqlTypeormComponent } from './sql-typeorm/sql-typeorm.component';
@@ -48,6 +49,11 @@ const routes: Routes = [
4849
component: CqrsComponent,
4950
data: { title: 'CQRS' },
5051
},
52+
{
53+
path: 'sentry',
54+
component: SentryComponent,
55+
data: { title: 'Sentry' },
56+
},
5157
{
5258
path: 'swagger',
5359
redirectTo: '/openapi/introduction',
@@ -142,6 +148,7 @@ const routes: Routes = [
142148
MikroOrmComponent,
143149
SqlTypeormComponent,
144150
SqlSequelizeComponent,
151+
SentryComponent,
145152
MongodbComponent,
146153
PrismaComponent,
147154
CqrsComponent,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { BasePageComponent } from '../../page/page.component';
3+
4+
@Component({
5+
selector: 'app-sentry',
6+
templateUrl: './sentry.component.html',
7+
changeDetection: ChangeDetectionStrategy.OnPush,
8+
})
9+
export class SentryComponent extends BasePageComponent {}

0 commit comments

Comments
 (0)