From cdab5e2ae808d4acb712460f60552a5260e25cdd Mon Sep 17 00:00:00 2001 From: Roberto Arruda Date: Tue, 10 Dec 2024 12:13:42 -0400 Subject: [PATCH] chore: improve instrumentSentry.ts import before another import Signed-off-by: Roberto Arruda --- src/main.ts | 25 ++++++++++++------------- src/utils/instrumentSentry.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 src/utils/instrumentSentry.ts diff --git a/src/main.ts b/src/main.ts index 5204b6983..938d51d24 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,7 @@ +// Import this first from sentry instrument! +import '@utils/instrumentSentry'; + +// Now import other modules import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; import { HttpStatus, router } from '@api/routes/index.router'; @@ -21,19 +25,6 @@ function initWA() { async function bootstrap() { const logger = new Logger('SERVER'); const app = express(); - const dsn = process.env.SENTRY_DSN; - - if (dsn) { - logger.info('Sentry - ON'); - Sentry.init({ - dsn: dsn, - environment: process.env.NODE_ENV || 'development', - tracesSampleRate: 1.0, - profilesSampleRate: 1.0, - }); - - Sentry.setupExpressErrorHandler(app); - } let providerFiles: ProviderFiles = null; if (configService.get('PROVIDER').ENABLED) { @@ -141,6 +132,14 @@ async function bootstrap() { eventManager.init(server); + if (process.env.SENTRY_DSN) { + logger.info('Sentry - ON'); + + // Add this after all routes, + // but before any and other error-handling middlewares are defined + Sentry.setupExpressErrorHandler(app); + } + server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); initWA(); diff --git a/src/utils/instrumentSentry.ts b/src/utils/instrumentSentry.ts new file mode 100644 index 000000000..8a6606759 --- /dev/null +++ b/src/utils/instrumentSentry.ts @@ -0,0 +1,12 @@ +import * as Sentry from "@sentry/node"; + +const dsn = process.env.SENTRY_DSN; + +if (dsn) { + Sentry.init({ + dsn: dsn, + environment: process.env.NODE_ENV || 'development', + tracesSampleRate: 1.0, + profilesSampleRate: 1.0, + }); +} \ No newline at end of file