Skip to content

Commit 79f0754

Browse files
chore: add sentry performance tracking api-v2 (#17519)
* chore: add sentry performance tracking api-v2 * fixup! chore: add sentry performance tracking api-v2
1 parent 540bd47 commit 79f0754

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

apps/api/v2/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
"@nestjs/platform-express": "^10.0.0",
4646
"@nestjs/swagger": "^7.3.0",
4747
"@nestjs/throttler": "6.2.1",
48+
"@sentry/nestjs": "^8.37.1",
4849
"@sentry/node": "^8.8.0",
50+
"@sentry/profiling-node": "^8.37.1",
4951
"body-parser": "^1.20.2",
5052
"bull": "^4.12.4",
5153
"class-transformer": "^0.5.1",

apps/api/v2/src/app.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import { ThrottlerStorageRedisService } from "@nest-lab/throttler-storage-redis"
1717
import { BullModule } from "@nestjs/bull";
1818
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from "@nestjs/common";
1919
import { ConfigModule } from "@nestjs/config";
20-
import { APP_GUARD, APP_INTERCEPTOR } from "@nestjs/core";
20+
import { APP_GUARD, APP_INTERCEPTOR, APP_FILTER } from "@nestjs/core";
2121
import { seconds, ThrottlerModule } from "@nestjs/throttler";
22+
import { SentryModule, SentryGlobalFilter } from "@sentry/nestjs/setup";
2223

2324
import { AppController } from "./app.controller";
2425

2526
@Module({
2627
imports: [
28+
SentryModule.forRoot(),
2729
ConfigModule.forRoot({
2830
ignoreEnvFile: true,
2931
isGlobal: true,
@@ -59,6 +61,10 @@ import { AppController } from "./app.controller";
5961
],
6062
controllers: [AppController],
6163
providers: [
64+
{
65+
provide: APP_FILTER,
66+
useClass: SentryGlobalFilter,
67+
},
6268
{
6369
provide: ThrottlerStorageRedisService,
6470
useFactory: (redisService: RedisService) => {

apps/api/v2/src/app.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { ValidationError } from "@nestjs/common";
77
import { BadRequestException, ValidationPipe, VersioningType } from "@nestjs/common";
88
import { BaseExceptionFilter, HttpAdapterHost } from "@nestjs/core";
99
import type { NestExpressApplication } from "@nestjs/platform-express";
10-
import * as Sentry from "@sentry/node";
1110
import * as cookieParser from "cookie-parser";
1211
import { Request } from "express";
1312
import helmet from "helmet";
@@ -71,9 +70,6 @@ export const bootstrap = (app: NestExpressApplication): NestExpressApplication =
7170

7271
// Exception filters, new filters go at the bottom, keep the order
7372
const { httpAdapter } = app.get(HttpAdapterHost);
74-
if (process.env.SENTRY_DSN) {
75-
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
76-
}
7773
app.useGlobalFilters(new PrismaExceptionFilter());
7874
app.useGlobalFilters(new ZodExceptionFilter());
7975
app.useGlobalFilters(new HttpExceptionFilter());

apps/api/v2/src/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type Environment = {
1010
DATABASE_URL: string;
1111
JWT_SECRET: string;
1212
SENTRY_DSN: string;
13+
SENTRY_TRACES_SAMPLE_RATE?: number;
14+
SENTRY_PROFILES_SAMPLE_RATE?: number;
1315
LOG_LEVEL: keyof typeof logLevels;
1416
REDIS_URL: string;
1517
STRIPE_API_KEY: string;

apps/api/v2/src/instrument.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { getEnv } from "@/env";
2-
import * as Sentry from "@sentry/node";
2+
import * as Sentry from "@sentry/nestjs";
3+
import { nodeProfilingIntegration } from "@sentry/profiling-node";
34

45
if (process.env.SENTRY_DSN) {
56
// Ensure to call this before requiring any other modules!
67
Sentry.init({
78
dsn: getEnv("SENTRY_DSN"),
8-
// Add Performance Monitoring by setting tracesSampleRate
9-
// We recommend adjusting this value in production
10-
// todo: Evaluate? tracesSampleRate: 1.0
9+
integrations: [nodeProfilingIntegration()],
10+
// Performance Monitoring
11+
tracesSampleRate: getEnv("SENTRY_TRACES_SAMPLE_RATE") ?? 1.0, // Capture 100% of the transactions
12+
// Set sampling rate for profiling - this is relative to tracesSampleRate
13+
profilesSampleRate: getEnv("SENTRY_PROFILES_SAMPLE_RATE") ?? 1.0,
1114
});
1215
}

0 commit comments

Comments
 (0)