From 79f0754bf72601eb9e6c13e8913ec857e9f6a29e Mon Sep 17 00:00:00 2001 From: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Date: Thu, 7 Nov 2024 14:38:05 +0200 Subject: [PATCH] chore: add sentry performance tracking api-v2 (#17519) * chore: add sentry performance tracking api-v2 * fixup! chore: add sentry performance tracking api-v2 --- apps/api/v2/package.json | 2 ++ apps/api/v2/src/app.module.ts | 8 +++++++- apps/api/v2/src/app.ts | 4 ---- apps/api/v2/src/env.ts | 2 ++ apps/api/v2/src/instrument.ts | 11 +++++++---- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/api/v2/package.json b/apps/api/v2/package.json index 6dffb53412..77cbe4910d 100644 --- a/apps/api/v2/package.json +++ b/apps/api/v2/package.json @@ -45,7 +45,9 @@ "@nestjs/platform-express": "^10.0.0", "@nestjs/swagger": "^7.3.0", "@nestjs/throttler": "6.2.1", + "@sentry/nestjs": "^8.37.1", "@sentry/node": "^8.8.0", + "@sentry/profiling-node": "^8.37.1", "body-parser": "^1.20.2", "bull": "^4.12.4", "class-transformer": "^0.5.1", diff --git a/apps/api/v2/src/app.module.ts b/apps/api/v2/src/app.module.ts index e4c1693753..b9bd76cfd7 100644 --- a/apps/api/v2/src/app.module.ts +++ b/apps/api/v2/src/app.module.ts @@ -17,13 +17,15 @@ import { ThrottlerStorageRedisService } from "@nest-lab/throttler-storage-redis" import { BullModule } from "@nestjs/bull"; import { MiddlewareConsumer, Module, NestModule, RequestMethod } from "@nestjs/common"; import { ConfigModule } from "@nestjs/config"; -import { APP_GUARD, APP_INTERCEPTOR } from "@nestjs/core"; +import { APP_GUARD, APP_INTERCEPTOR, APP_FILTER } from "@nestjs/core"; import { seconds, ThrottlerModule } from "@nestjs/throttler"; +import { SentryModule, SentryGlobalFilter } from "@sentry/nestjs/setup"; import { AppController } from "./app.controller"; @Module({ imports: [ + SentryModule.forRoot(), ConfigModule.forRoot({ ignoreEnvFile: true, isGlobal: true, @@ -59,6 +61,10 @@ import { AppController } from "./app.controller"; ], controllers: [AppController], providers: [ + { + provide: APP_FILTER, + useClass: SentryGlobalFilter, + }, { provide: ThrottlerStorageRedisService, useFactory: (redisService: RedisService) => { diff --git a/apps/api/v2/src/app.ts b/apps/api/v2/src/app.ts index 3ce349268c..b452d0ddca 100644 --- a/apps/api/v2/src/app.ts +++ b/apps/api/v2/src/app.ts @@ -7,7 +7,6 @@ import type { ValidationError } from "@nestjs/common"; import { BadRequestException, ValidationPipe, VersioningType } from "@nestjs/common"; import { BaseExceptionFilter, HttpAdapterHost } from "@nestjs/core"; import type { NestExpressApplication } from "@nestjs/platform-express"; -import * as Sentry from "@sentry/node"; import * as cookieParser from "cookie-parser"; import { Request } from "express"; import helmet from "helmet"; @@ -71,9 +70,6 @@ export const bootstrap = (app: NestExpressApplication): NestExpressApplication = // Exception filters, new filters go at the bottom, keep the order const { httpAdapter } = app.get(HttpAdapterHost); - if (process.env.SENTRY_DSN) { - Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter)); - } app.useGlobalFilters(new PrismaExceptionFilter()); app.useGlobalFilters(new ZodExceptionFilter()); app.useGlobalFilters(new HttpExceptionFilter()); diff --git a/apps/api/v2/src/env.ts b/apps/api/v2/src/env.ts index b81b5a81f8..833a816144 100644 --- a/apps/api/v2/src/env.ts +++ b/apps/api/v2/src/env.ts @@ -10,6 +10,8 @@ export type Environment = { DATABASE_URL: string; JWT_SECRET: string; SENTRY_DSN: string; + SENTRY_TRACES_SAMPLE_RATE?: number; + SENTRY_PROFILES_SAMPLE_RATE?: number; LOG_LEVEL: keyof typeof logLevels; REDIS_URL: string; STRIPE_API_KEY: string; diff --git a/apps/api/v2/src/instrument.ts b/apps/api/v2/src/instrument.ts index 77c172854e..a579907a2b 100644 --- a/apps/api/v2/src/instrument.ts +++ b/apps/api/v2/src/instrument.ts @@ -1,12 +1,15 @@ import { getEnv } from "@/env"; -import * as Sentry from "@sentry/node"; +import * as Sentry from "@sentry/nestjs"; +import { nodeProfilingIntegration } from "@sentry/profiling-node"; if (process.env.SENTRY_DSN) { // Ensure to call this before requiring any other modules! Sentry.init({ dsn: getEnv("SENTRY_DSN"), - // Add Performance Monitoring by setting tracesSampleRate - // We recommend adjusting this value in production - // todo: Evaluate? tracesSampleRate: 1.0 + integrations: [nodeProfilingIntegration()], + // Performance Monitoring + tracesSampleRate: getEnv("SENTRY_TRACES_SAMPLE_RATE") ?? 1.0, // Capture 100% of the transactions + // Set sampling rate for profiling - this is relative to tracesSampleRate + profilesSampleRate: getEnv("SENTRY_PROFILES_SAMPLE_RATE") ?? 1.0, }); }