diff --git a/src/index.ts b/src/index.ts index 5e0bc3a31..b28d931de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ import * as indexController from '@controllers/indexController'; import * as resourceController from '@controllers/resourceController'; import * as hookController from '@controllers/hookController'; import * as pullRequestsController from '@controllers/pullRequestsController'; +import * as sentry from './lib/sentry'; import EventBus from '@infra/eventBus/eventBus'; import { MARRAINAGE_EVENTS_VALUES } from '@models/marrainage'; import routes from './routes/routes'; @@ -40,7 +41,6 @@ import { corsOptions } from './utils/corsConfig'; import { errorHandler } from './middlewares/errorHandler'; import { setupSessionMiddleware } from './middlewares/sessionMiddleware'; import { PUBLIC_ROUTES } from './config/jwt.config'; -import { initializeSentry, sentryErrorHandler } from './lib/sentry'; export const app = express(); app.set('trust proxy', 1); @@ -53,7 +53,6 @@ app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, './views/templates')); // the code is running in directory "dist". // MIDDLEWARES -initializeSentry(app); app.use(compression()); setupStaticFiles(app); setupSessionMiddleware(app); @@ -117,7 +116,8 @@ app.post( express.json({ type: '*/*' }), hookController.postToHook ); -app.use(sentryErrorHandler); + +sentry.initCaptureConsoleWithHandler(app); export default app.listen(config.port, () => console.log(`Running on: ${config.protocol}://${config.host}:${config.port}`) diff --git a/src/lib/sentry.ts b/src/lib/sentry.ts index dc717be49..5214f4e13 100644 --- a/src/lib/sentry.ts +++ b/src/lib/sentry.ts @@ -1,21 +1,32 @@ import * as Sentry from '@sentry/node'; -import { ErrorRequestHandler } from 'express'; -import config from '@/config'; - -export const initializeSentry = (app) => { - if (!config.sentryDNS) { - console.log('Sentry DSN not found. Sentry is not initialized.'); - return; - } +import { CaptureConsole as CaptureConsoleIntegration } from "@sentry/integrations"; +import config from '@config'; +export function initCaptureConsole() { + const logLevel = ['error']; + console.log( + `Initializing Sentry for log level "${logLevel}" and config: ${config.sentryDNS}` + ); Sentry.init({ - dsn: process.env.SENTRY_DSN, - tracesSampleRate: 1.0, + dsn: config.sentryDNS as string, + // https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole + integrations: [new CaptureConsoleIntegration({ levels: logLevel })], }); +} - app.use(Sentry.Handlers.requestHandler()); - app.use(Sentry.Handlers.tracingHandler()); -}; +export function initCaptureConsoleWithHandler(app) { + if (config.sentryDNS) { + initCaptureConsole(); -export const sentryErrorHandler: ErrorRequestHandler = - Sentry.Handlers.errorHandler(); + // RequestHandler creates a separate execution context using domains, so that every + // transaction/span/breadcrumb is attached to its own Hub instance + app.use(Sentry.Handlers.requestHandler()); + + // The error handler must be before any other error middleware and after all controllers + app.use(Sentry.Handlers.errorHandler()); + } else { + console.log( + 'Sentry was not initialized as SENTRY_DNS env variable is missing' + ); + } +}