Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

chore: add Sentry configuration for better error handling #2349

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 172 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"@octokit/request": "^6.2.1",
"@octokit/rest": "^19.0.4",
"@sentry/integrations": "^7.17.4",
"@sentry/node": "^7.17.4",
"@sentry/node": "^7.93.0",
"@sentry/tracing": "^7.93.0",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.0.11",
"airtable": "^0.12.1",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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';
Expand Down Expand Up @@ -41,6 +40,7 @@ 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);
Expand All @@ -53,6 +53,7 @@ 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);
Expand Down Expand Up @@ -116,8 +117,7 @@ app.post(
express.json({ type: '*/*' }),
hookController.postToHook
);

sentry.initCaptureConsoleWithHandler(app);
app.use(sentryErrorHandler);

export default app.listen(config.port, () =>
console.log(`Running on: ${config.protocol}://${config.host}:${config.port}`)
Expand Down
41 changes: 15 additions & 26 deletions src/lib/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import * as Sentry from '@sentry/node';
import { CaptureConsole as CaptureConsoleIntegration } from "@sentry/integrations";
import config from '@config';
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;
}

export function initCaptureConsole() {
const logLevel = ['error'];
console.log(
`Initializing Sentry for log level "${logLevel}" and config: ${config.sentryDNS}`
);
Sentry.init({
dsn: config.sentryDNS as string,
// https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole
integrations: [new CaptureConsoleIntegration({ levels: logLevel })],
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});
}

export function initCaptureConsoleWithHandler(app) {
if (config.sentryDNS) {
initCaptureConsole();
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
};

// 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'
);
}
}
export const sentryErrorHandler: ErrorRequestHandler =
Sentry.Handlers.errorHandler();
Loading