Skip to content

Commit

Permalink
wip: SentryExceptionLimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
yowpark committed Nov 17, 2023
1 parent aefcaa0 commit 6b81bf0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ArgumentsHost, Catch, HttpServer } from '@nestjs/common';

import { SentryExceptionLimiter } from '@dogu-private/nestjs-common';
import { BaseExceptionFilter } from '@nestjs/core';
import { env } from '../env';

@Catch()
export class AllExceptionsFilter extends BaseExceptionFilter {
private sentryExceptionLimiter = new SentryExceptionLimiter(env.DOGU_USE_SENTRY, 0.2);

constructor(applicationRef?: HttpServer) {
super(applicationRef);
}

override catch(exception: unknown, host: ArgumentsHost): void {
this.sentryExceptionLimiter.sendException(exception);
super.catch(exception, host);
}
}
5 changes: 3 additions & 2 deletions packages/typescript-private/device-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata';

import { ChildError, PlatformAbility } from '@dogu-private/dost-children';
import { handleLoggerCreateWithSentry, initSentry, SentryAllExceptionsFilter } from '@dogu-private/nestjs-common';
import { handleLoggerCreateWithSentry, initSentry } from '@dogu-private/nestjs-common';
import { Code, DOGU_PROTOCOL_VERSION } from '@dogu-private/types';
import { errorify } from '@dogu-tech/common';
import { ValidationPipe } from '@nestjs/common';
Expand All @@ -11,6 +11,7 @@ import * as Sentry from '@sentry/node';
import { WinstonModule } from 'nest-winston';
import { AppModule } from './app/app.module';
import { env } from './env';
import { AllExceptionsFilter } from './filter/exception.filter';
import { adbLogger, deviceInfoLogger, logger, zombieLogger } from './logger/logger.instance';
import { openPathMap } from './path-map';
import { addProcessEventHandler } from './process-event';
Expand Down Expand Up @@ -51,7 +52,7 @@ export async function bootstrap(): Promise<void> {
app
.useWebSocketAdapter(new WsAdapter(app))
.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }))
.useGlobalFilters(new SentryAllExceptionsFilter(env.DOGU_USE_SENTRY, app.getHttpAdapter()))
.useGlobalFilters(new AllExceptionsFilter(app.getHttpAdapter()))
.enableCors({
origin: true,
preflightContinue: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ArgumentsHost, Catch, HttpServer } from '@nestjs/common';

import { SentryExceptionLimiter } from '@dogu-private/nestjs-common';
import { BaseExceptionFilter } from '@nestjs/core';
import { env } from '../env';

@Catch()
export class AllExceptionsFilter extends BaseExceptionFilter {
private sentryExceptionLimiter = new SentryExceptionLimiter(env.DOGU_USE_SENTRY, 0.2);

constructor(applicationRef?: HttpServer) {
super(applicationRef);
}

override catch(exception: unknown, host: ArgumentsHost): void {
this.sentryExceptionLimiter.sendException(exception);
super.catch(exception, host);
}
}
5 changes: 3 additions & 2 deletions packages/typescript-private/host-agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata';

import { ChildError } from '@dogu-private/dost-children';
import { handleLoggerCreateWithSentry, initSentry, SentryAllExceptionsFilter } from '@dogu-private/nestjs-common';
import { handleLoggerCreateWithSentry, initSentry } from '@dogu-private/nestjs-common';
import { Code } from '@dogu-private/types';
import { errorify } from '@dogu-tech/common';
import { NestFactory } from '@nestjs/core';
Expand All @@ -12,6 +12,7 @@ import http from 'http';
import { WinstonModule } from 'nest-winston';
import { AppModule } from './app/app.module';
import { env } from './env';
import { AllExceptionsFilter } from './filter/exception.filter';
import { logger } from './logger/logger.instance';
import { MessageMicroService } from './message/message.microservice';
import { MessagePuller } from './message/message.puller';
Expand Down Expand Up @@ -44,7 +45,7 @@ export async function bootstrap(): Promise<void> {
strategy: messageMicroService,
logger: winstonModuleLogger,
});
app.useGlobalFilters(new SentryAllExceptionsFilter(env.DOGU_USE_SENTRY, app.getHttpAdapter()));
app.useGlobalFilters(new AllExceptionsFilter(app.getHttpAdapter()));

const messagePuller = app.get(MessagePuller);

Expand Down
25 changes: 10 additions & 15 deletions packages/typescript-private/nestjs-common/src/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { stringify } from '@dogu-tech/common';
import { Logger } from '@dogu-tech/node';
import { ArgumentsHost, Catch, HttpServer } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import * as Sentry from '@sentry/node';
import Transport from 'winston-transport';

Expand Down Expand Up @@ -36,22 +34,19 @@ export class SentryBreadCrumbTrasponrt extends Transport {
}
}

@Catch()
export class SentryAllExceptionsFilter extends BaseExceptionFilter {
export class SentryExceptionLimiter {
constructor(
private useSentry: boolean,
applicationRef?: HttpServer,
private sendRatio: number = 0.2,
) {
super(applicationRef);
}
override catch(exception: unknown, host: ArgumentsHost): void {
if (this.useSentry) {
const rand = Math.random();
if (rand < this.sendRatio) {
Sentry.captureException(exception);
}
) {}

sendException(exception: unknown): void {
if (!this.useSentry) {
return;
}
const rand = Math.random();
if (rand < this.sendRatio) {
Sentry.captureException(exception);
}
super.catch(exception, host);
}
}
9 changes: 4 additions & 5 deletions projects/console-web-server/src/filter/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { SentryExceptionLimiter } from '@dogu-private/nestjs-common';
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import * as Sentry from '@sentry/node';
import { Request, Response } from 'express';
import { logger } from '../module/logger/logger.instance';
import { isSentryEnabled } from '../utils/sentry';

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
private logger = logger;
private sentryExceptionLimiter = new SentryExceptionLimiter(isSentryEnabled(), 0.1);

constructor(private readonly httpAdapterHost: HttpAdapterHost) {}

catch(exception: any, host: ArgumentsHost) {
if (isSentryEnabled()) {
Sentry.captureException(exception);
}
catch(exception: unknown, host: ArgumentsHost): void {
this.sentryExceptionLimiter.sendException(exception);

const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
Expand Down
15 changes: 0 additions & 15 deletions projects/console-web-server/src/utils/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import * as Sentry from '@sentry/node';
import Transport from 'winston-transport';
import { env } from '../env';

export function isSentryEnabled(): boolean {
return env.DOGU_RUN_TYPE === 'production' || env.DOGU_RUN_TYPE === 'development';
}

export class SentryBreadCrumbTrasponrt extends Transport {
override log(info: any, callback: () => void) {
Sentry.addBreadcrumb({
type: 'default',
category: info.level,
message: info.message,
level: info.level,
data: info.meta,
});
callback();
}
}

0 comments on commit 6b81bf0

Please sign in to comment.