Skip to content

Commit

Permalink
feat: lc 604 sentry integration (#420)
Browse files Browse the repository at this point in the history
* feat: sentry integration

* chore: change interceptor types

* chore: remove unnecessary import

* fix: build fix

* chore: add blank values to env.example
  • Loading branch information
MarcinMaciejewski337 authored Mar 5, 2025
1 parent 287b1de commit 7f00760
Show file tree
Hide file tree
Showing 6 changed files with 779 additions and 16 deletions.
4 changes: 4 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ SES_EMAIL=
STRIPE_SECRET_KEY=
STRIPE_PUBLISHABLE_KEY=
STRIPE_WEBHOOK_SECRET=

#SENTRY
SENTRY_ENVIRONMENT=development
SENTRY_DSN=
3 changes: 3 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"@nestjs/terminus": "10.2.3",
"@nestjs/websockets": "10.4.4",
"@repo/email-templates": "workspace:*",
"@sentry/integrations": "^7.114.0",
"@sentry/node": "^9.3.0",
"@sentry/profiling-node": "^9.3.0",
"@sinclair/typebox": "0.32.34",
"@types/bcryptjs": "^2.4.6",
"@types/multer": "1.4.12",
Expand Down
7 changes: 6 additions & 1 deletion apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DrizzlePostgresModule } from "@knaadh/nestjs-drizzle-postgres";
import { Module } from "@nestjs/common";
import { ConditionalModule, ConfigModule, ConfigService } from "@nestjs/config";
import { APP_GUARD } from "@nestjs/core";
import { APP_GUARD, APP_INTERCEPTOR } from "@nestjs/core";
import { JwtModule } from "@nestjs/jwt";
import { ScheduleModule } from "@nestjs/schedule";

Expand All @@ -26,6 +26,7 @@ import { LessonModule } from "./lesson/lesson.module";
import { QuestionsModule } from "./questions/question.module";
import { S3Module } from "./s3/s3.module";
import { ScormModule } from "./scorm/scorm.module";
import { SentryInterceptor } from "./sentry/sentry.interceptor";
import { StatisticsModule } from "./statistics/statistics.module";
import * as schema from "./storage/schema";
import { StripeModule } from "./stripe/stripe.module";
Expand Down Expand Up @@ -86,6 +87,10 @@ import { UserModule } from "./user/user.module";
],
controllers: [],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: SentryInterceptor,
},
{
provide: APP_GUARD,
useClass: JwtAuthGuard,
Expand Down
9 changes: 9 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
import cookieParser from "cookie-parser";
import { patchNestJsSwagger, applyFormats } from "nestjs-typebox";

Expand All @@ -11,6 +13,13 @@ patchNestJsSwagger();
applyFormats();

async function bootstrap() {
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [nodeProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
});

const app = await NestFactory.create(AppModule, {
rawBody: true,
});
Expand Down
18 changes: 18 additions & 0 deletions apps/api/src/sentry/sentry.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from "@nestjs/common";
import * as Sentry from "@sentry/node";
import { catchError, throwError } from "rxjs";

import type { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
import type { Observable } from "rxjs";

@Injectable()
export class SentryInterceptor<T> implements NestInterceptor<T> {
intercept(context: ExecutionContext, next: CallHandler<T>): Observable<T> {
return next.handle().pipe(
catchError((error) => {
Sentry.captureException(error);
return throwError(() => error);
}),
);
}
}
Loading

0 comments on commit 7f00760

Please sign in to comment.