From cb945ba2505032d1a835ab0a255014f10f8be88e Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 14 Jul 2024 13:33:58 +0530 Subject: [PATCH] chore: Update npm dependencies and add Sentry integration for error logging and performance monitoring --- package.json | 8 +++++--- src/handlers/logger.ts | 4 ++-- src/index.ts | 45 +++++++++++++++++++++++++++++++----------- tsconfig.json | 5 +++++ 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index afcf768b..c3e938df 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "dev": "bun run src/index.ts", "commands": "node dist/src/libs/registerCommands.js", "bun-cmd": "bun run src/libs/registerCommands.ts", - "format": "npx prettier --write \"**/*.{js,css,ejs, ts}\"" + "format": "npx prettier --write \"**/*.{js,css,ejs, ts}\"", + "upload-source-maps": "sentry-cli sourcemaps inject dist && sentry-cli sourcemaps upload dist --project node" }, "author": "Naiyar Imam", "license": "MIT", @@ -28,6 +29,8 @@ "@nestjs/platform-express": "^10.3.8", "@sapphire/stopwatch": "^1.5.2", "@sapphire/type": "^2.4.4", + "@sentry/node": "^8.17.0", + "@sentry/profiling-node": "^8.17.0", "chalk": "^4.1.2", "cors": "^2.8.5", "cropify": "^1.0.9", @@ -46,8 +49,7 @@ "pino-pretty": "^10.2.0", "skyhelper-utils": "^1.2.5", "table": "^6.8.1", - "topgg-autoposter": "^2.0.2", - "uuid": "^9.0.1" + "topgg-autoposter": "^2.0.2" }, "imports": { "#handlers": [ diff --git a/src/handlers/logger.ts b/src/handlers/logger.ts index 073c483a..42aa8406 100644 --- a/src/handlers/logger.ts +++ b/src/handlers/logger.ts @@ -1,9 +1,9 @@ import { EmbedBuilder, WebhookClient, codeBlock } from "discord.js"; import config from "#src/config"; -import { v4 as genId } from "uuid"; import util from "node:util"; import { postToHaste } from "skyhelper-utils"; import { Logger } from "@nestjs/common"; +import { captureException } from "@sentry/node"; const webhookLogger = process.env.ERROR_LOGS ? new WebhookClient({ url: process.env.ERROR_LOGS }) : undefined; async function sendWebhook(id: string, content: any, err?: any): Promise { @@ -52,7 +52,7 @@ export default class { * @returns The error ID */ static error(content: any, ex?: any) { - const id = genId(); + const id = captureException(ex || content); if (ex) { Logger.error(ex, `${id} => ${content}`); } else { diff --git a/src/index.ts b/src/index.ts index 079dca9d..c7c285be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import * as Sentry from "@sentry/node"; +import { nodeProfilingIntegration } from "@sentry/profiling-node"; import "dotenv/config"; import { SkyHelper } from "#structures"; import { initializeMongoose } from "#src/database/mongoose"; @@ -11,20 +13,37 @@ declare global { isBun?: boolean; } interface ProcessEnv { - TOKEN: string - MONGO_CONNECTION: string - AUTH_TOKEN: string - TOPGG_TOKEN?: string - GUILD?: string - ERROR_LOGS?: string - READY_LOGS?: string - SUGGESTION?: string - CONTACT_US?: string - COMMANDS_USED?: string - BUG_REPORTS?: string + TOKEN: string; + NODE_ENV: "development" | "production"; + MONGO_CONNECTION: string; + SENTRY_DSN: string; + AUTH_TOKEN: string; + TOPGG_TOKEN?: string; + GUILD?: string; + ERROR_LOGS?: string; + READY_LOGS?: string; + SUGGESTION?: string; + CONTACT_US?: string; + COMMANDS_USED?: string; + BUG_REPORTS?: string; } } } +// Init Sentry +Sentry.init({ + dsn: process.env.SENTRY_DSN, + integrations: [ + nodeProfilingIntegration(), + Sentry.rewriteFramesIntegration({ + root: global.__dirname, + }), + ], + // Performance Monitoring + tracesSampleRate: 1.0, // Capture 100% of the transactions + + // Set sampling rate for profiling - this is relative to tracesSampleRate + profilesSampleRate: 1.0, +}); const root = process.isBun ? "src" : "dist/src"; await client.loadEvents(root + "/events"); @@ -36,7 +55,9 @@ await initializeMongoose(); console.log(chalk.blueBright("\n\n<------------------------ Dashboard --------------------------->\n")); if (client.config.DASHBOARD.enabled) Dashboard(client); // Catching unhandle rejections -process.on("unhandledRejection", (err) => client.logger.error(err)); +process.on("unhandledRejection", (err) => { + client.logger.error(err); +}); process.on("uncaughtException", (err) => client.logger.error(err)); // Login client.login(process.env.TOKEN); diff --git a/tsconfig.json b/tsconfig.json index 94d16382..168c351d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,11 @@ "target": "ES2022", "module": "NodeNext", "allowSyntheticDefaultImports": true, + + // Source maps + "sourceMap": true, + "inlineSources": true, + "sourceRoot": "/", "resolveJsonModule": true, "forceConsistentCasingInFileNames": true, "allowJs": true,