From 2882876f0be57c11a9d14782bd5a9056f727b463 Mon Sep 17 00:00:00 2001 From: Baptiste Studer Date: Sat, 18 Mar 2023 21:25:56 +0100 Subject: [PATCH] chore(ogre-server): Log errors to sentry --- packages/server/package.json | 2 ++ packages/server/src/error-handling/index.ts | 1 + packages/server/src/error-handling/utils.ts | 35 +++++++++++++++++++++ packages/server/src/index.ts | 3 ++ packages/server/src/middlewares/logError.ts | 4 ++- yarn.lock | 2 +- 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/error-handling/index.ts create mode 100644 packages/server/src/error-handling/utils.ts diff --git a/packages/server/package.json b/packages/server/package.json index a92ed25c..6d24f047 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -4,6 +4,8 @@ "dependencies": { "@prisma/client": "^3.12.0", "@sendgrid/mail": "^7.6.2", + "@sentry/node": "^7.43.0", + "@sentry/tracing": "^7.43.0", "body-parser": "^1.20.0", "cookie": "^0.5.0", "cookie-parser": "^1.4.6", diff --git a/packages/server/src/error-handling/index.ts b/packages/server/src/error-handling/index.ts new file mode 100644 index 00000000..178cd64f --- /dev/null +++ b/packages/server/src/error-handling/index.ts @@ -0,0 +1 @@ +export * from "./utils"; diff --git a/packages/server/src/error-handling/utils.ts b/packages/server/src/error-handling/utils.ts new file mode 100644 index 00000000..d26eb77f --- /dev/null +++ b/packages/server/src/error-handling/utils.ts @@ -0,0 +1,35 @@ +import * as Sentry from "@sentry/node"; +import * as Tracing from "@sentry/tracing"; +import { Request, Response, Router } from "express"; +import { getUserRequesting } from "../lib/express"; + +export { initErrorTracer, traceError, traceRequests }; + +const initErrorTracer = (app: Router) => { + Sentry.init({ + dsn: process.env.SENTRY_DSN, + environment: process.env.SENTRY_ENV || "development", + integrations: [ + new Sentry.Integrations.Http({ tracing: true }), + new Tracing.Integrations.Express({ app }), + ], + // TODO: adapt value in production. + tracesSampleRate: 1.0, + }); +}; + +const traceRequests = (app: Router) => { + app.use(Sentry.Handlers.requestHandler()); + app.use(Sentry.Handlers.tracingHandler()); +}; + +const traceError = (err: Error, req: Request, res: Response) => { + const data = Sentry.extractRequestData(req); + const user = getUserRequesting(res); + Sentry.captureException(err, { + user: { + id: user?.id?.toString(), + }, + contexts: data, + }); +}; diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 47678e42..c97e0658 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -11,11 +11,14 @@ import { initWebSocket } from "./modules/websocket"; import { logger } from "./logger"; import { logError, setRequestId } from "./middlewares"; import { limiter } from "./middlewares/limit"; +import { initErrorTracer, traceRequests } from "./error-handling"; const app = express(); +initErrorTracer(app); connectToDatase().then(seed); +traceRequests(app); app.use(bodyParser.urlencoded({ extended: false })); // Parse JSON bodies (as sent by API clients) app.use(bodyParser.json()); diff --git a/packages/server/src/middlewares/logError.ts b/packages/server/src/middlewares/logError.ts index 0d28b7a9..d7404178 100644 --- a/packages/server/src/middlewares/logError.ts +++ b/packages/server/src/middlewares/logError.ts @@ -1,4 +1,5 @@ import { NextFunction, Request, Response } from "express"; +import { traceError } from "../error-handling"; import { logger } from "../logger"; import { BusinessError, @@ -11,12 +12,13 @@ const isProd = process.env.NODE_ENV === "production"; const logError = ( err: Error, - _: Request, + req: Request, res: Response, // eslint-disable-next-line @typescript-eslint/no-unused-vars next: NextFunction ) => { logger.error(err); + traceError(err, req, res); if (err instanceof BusinessError) { if (err.code === "USER_NOT_AUTHENTICATED") { diff --git a/yarn.lock b/yarn.lock index 2cd571a1..e948e4dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1724,7 +1724,7 @@ "@sentry/utils" "7.43.0" tslib "^1.9.3" -"@sentry/node@^7.19.0": +"@sentry/node@^7.19.0", "@sentry/node@^7.43.0": version "7.43.0" resolved "https://registry.yarnpkg.com/@sentry/node/-/node-7.43.0.tgz#bcb553486ffe3f1413063e9125fd34f9d84277d2" integrity sha512-oXaTBq6Bk8Qwsd46hhRU2MLEnjYqWI41nPJmXyAWkDSYQTP7sUe1qM8bCUdsRpPwQh955Vq9qCRfgMbN4lEoAQ==