Skip to content

Commit

Permalink
feat: integrating sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingcedru committed Dec 3, 2024
1 parent e20668c commit 6949411
Show file tree
Hide file tree
Showing 8 changed files with 1,572 additions and 1,798 deletions.
8 changes: 5 additions & 3 deletions apps/drec-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"@nestjs/schedule": "1.0.1",
"@nestjs/swagger": "5.2.1",
"@nestjs/typeorm": "8.0.2",
"@sentry/nestjs": "^8.42.0",
"@sentry/node": "^8.42.0",
"ansi-regex": "^4.1.1",
"aws-sdk": "^2.1272.0",
"axios": "0.28.0",
Expand All @@ -85,6 +87,7 @@
"dotenv": "8.2.0",
"ejs": "^3.1.7",
"ethers": "5.3.1",
"express": "4.19.2",
"follow-redirects": "1.15.6",
"form-data": "~4.0.0",
"handlebars": "^4.7.7",
Expand Down Expand Up @@ -114,15 +117,14 @@
"rxjs": "^7.1.0",
"shell-quote": "^1.7.3",
"simple-get": "^2.8.2",
"sql-parser": "~0.5.0",
"stream": "^0.0.2",
"swagger-ui-express": "4.1.6",
"terser": "^5.14.2",
"typeorm": "0.2.41",
"uglify-js": "2.6.0",
"underscore": "^1.12.1",
"uuid": "8.3.2",
"sql-parser": "~0.5.0",
"express": "4.19.2"
"uuid": "8.3.2"
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.22",
Expand Down
3,265 changes: 1,472 additions & 1,793 deletions apps/drec-api/pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion apps/drec-api/src/drec.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ import { UserLoginSessionEntity } from './pods/user/user_login_session.entity';
import { DeviceLateongoingIssueCertificateEntity } from './pods/device/device_lateongoing_certificate.entity';
import { CertificateSettingEntity } from './pods/device-group/certificate_setting.entity';
import { HttpModule } from '@nestjs/axios';
import { SentryModule } from '@sentry/nestjs/setup';
import { APP_FILTER } from '@nestjs/core';
import { SentryGlobalFilter } from '@sentry/nestjs/setup';
import { SentryFilter } from './filters/sentry.filter';

const getEnvFilePath = () => {
const pathsToTest = [
Expand Down Expand Up @@ -152,6 +156,7 @@ const QueueingModule = () => {

@Module({
imports: [
SentryModule.forRoot(),
HttpModule,
ConfigModule.forRoot({
envFilePath: getEnvFilePath(),
Expand Down Expand Up @@ -182,6 +187,11 @@ const QueueingModule = () => {
OnChainCertificateModule,
BlockchainPropertiesModule,
],
providers: [OnApplicationBootstrapHookService],
providers: [OnApplicationBootstrapHookService,
{
provide: APP_FILTER,
useClass: SentryFilter
},
],
})
export class DrecModule {}
44 changes: 44 additions & 0 deletions apps/drec-api/src/filters/global.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Catch, ExceptionFilter, HttpException, HttpStatus, Logger } from '@nestjs/common';
import { WithSentry } from '@sentry/nestjs';
import { ArgumentsHost } from '@nestjs/common';

@Catch()
export class GlobalExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(GlobalExceptionFilter.name);

@WithSentry()
catch(exception: any, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();

const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;

const message =
exception instanceof HttpException
? exception.message
: 'Internal server error';

// Only log to Sentry if not in local development
if (process.env.NODE_ENV !== 'local') {
this.logger.error({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
message,
stack: exception.stack,
environment: process.env.NODE_ENV
});
}

response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
message
});
}
}
24 changes: 24 additions & 0 deletions apps/drec-api/src/filters/sentry.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
import { Response } from 'express';
import * as Sentry from '@sentry/node';

@Catch()
export class SentryFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();

Sentry.captureException(exception);

const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;

response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
message: 'Internal server error'
});
}
}
2 changes: 1 addition & 1 deletion apps/drec-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { useContainer } from 'class-validator';
import fs from 'fs';
import { DrecModule } from './drec.module';

import "./instrument";
import * as PortUtils from './port';

export { DrecModule };
Expand Down
6 changes: 6 additions & 0 deletions apps/drec-api/src/instrument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Import with `const Sentry = require("@sentry/nestjs");` if you are using CJS
import * as Sentry from "@sentry/nestjs"

Sentry.init({
dsn: "https://714f3c892f47269a430f97326ca12ac0@o4508380579430400.ingest.de.sentry.io/4508380581789776",
});
9 changes: 9 additions & 0 deletions apps/drec-api/src/pods/reads/reads.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ export class ReadsController extends BaseReadsController {
super(baseReadsService);
}

@Get('/test-sentry')
@UseGuards(AuthGuard('jwt'), PermissionGuard)
@Permission('Read')
@ACLModules('READS_MANAGEMENT_CRUDL')
async throwTestError() {
throw new HttpException('Test Sentry Error', HttpStatus.INTERNAL_SERVER_ERROR);
}


/**
* This api user for get all the timezone list and also from serach key
* @param searchKeyword :string
Expand Down

0 comments on commit 6949411

Please sign in to comment.