Skip to content

Commit

Permalink
chore(dependencies): update sentry to v8, refactor code to view perfo…
Browse files Browse the repository at this point in the history
…rmance hit
  • Loading branch information
dsevillamartin committed Nov 15, 2024
1 parent 20ad613 commit 171c3d0
Show file tree
Hide file tree
Showing 8 changed files with 1,424 additions and 151 deletions.
1 change: 0 additions & 1 deletion lib/GitHub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
createOAuthUserAuth,
} = require('@octokit/auth-oauth-app');
const pick = require('lodash/pick');
const EventHandler = require('./EventHandler');
const Constants = require('./Constants');
const Log = require('../Util/Log');
const redis = require('../Util/redis');
Expand Down
41 changes: 8 additions & 33 deletions lib/Util/Log.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const winston = require('winston');
const path = require('path');
const util = require('util');
const YappyGitHub = require('./YappyGitHub');

const { MESSAGE } = require('triple-beam');
const jsonStringify = require('fast-safe-stringify');
Expand Down Expand Up @@ -84,20 +83,7 @@ class Log {

winston.addColors(this._colors);

if (process.env.SENTRY) {
const tracesSampleRate = Number(process.env.SENTRY_SAMPLE_RATE) || 0;

this.sentry = true;
Sentry.init({
dsn: process.env.SENTRY,
release: YappyGitHub.git.release,
environment:
process.env.NODE_ENV === 'production' ? 'production' : 'development',
integrations: [new Sentry.Integrations.Http({ tracing: true })],
tracesSampleRate,
});
this.info(`Sentry | Initialized (sample rate = ${tracesSampleRate})`);
}
this.sentry = !!process.env.SENTRY;

this.error = this.error.bind(this);
this.warn = this.warn.bind(this);
Expand All @@ -115,13 +101,17 @@ class Log {
if (!error) return;

if (this.sentry && !error.sentry) {
let eventId;

if (error instanceof Error) {
error.sentry = Sentry.captureException(error);
eventId = Sentry.captureException(error);
} else {
error.sentry = Sentry.captureMessage(
eventId = Sentry.captureMessage(
typeof error === 'object' ? util.inspect(error) : error
);
}

if (typeof error === 'object') error.sentry = eventId;
}

if (error.name == 'DiscordAPIError') delete error.stack;
Expand Down Expand Up @@ -187,26 +177,11 @@ class Log {

configureExpressInit(app) {
if (this.sentry) {
Sentry.getCurrentHub().getClient().addIntegration(
new Sentry.Integrations.Express({
app,
})
);

app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
Sentry.setupExpressErrorHandler(app);

this.info('Sentry | Express initialized');
}
}

configureExpressError(app) {
if (this.sentry) {
app.use(Sentry.Handlers.errorHandler());

this.info('Sentry | Express error handler initialized');
}
}
}

module.exports = new Log();
7 changes: 6 additions & 1 deletion lib/Util/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ const redis = require('redis');
const Log = require('./Log');
const client = redis.createClient(
process.env.REDIS_HOST,
process.env.REDIS_HOST
process.env.REDIS_PORT,
{
socket: {
connectTimeout: 5000,
},
}
);

exports.get = (db, key) => client.get(`${db}:${key}`);
Expand Down
2 changes: 0 additions & 2 deletions lib/Web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,6 @@ app.use(function (req, res, next) {
next(new NotFoundError());
});

Log.configureExpressError(app);

app.use((err, req, res, next) => {
const status = err?.status ?? (Number.isInteger(err) ? err : 500);

Expand Down
45 changes: 38 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
require('dotenv').config();

require('./instrument');

global.Log = require('./Util/Log');

Log.info('Initializing');
async function initialize() {
const components = [
{ name: 'models', import: () => require('./Models') },
{ name: 'github', import: () => require('./GitHub') },
{ name: 'discord', import: () => require('./Discord') },
{ name: 'web', import: () => require('./Web') },
];

Log.info('* Initializing components...');

try {
for (const component of components) {
const startTime = process.hrtime();

try {
await Promise.resolve(component.import());
const [seconds, nanoseconds] = process.hrtime(startTime);
const ms = (seconds * 1000 + nanoseconds / 1e6).toFixed(2);
Log.info(`* Loaded ${component.name} (${ms}ms)`);
} catch (err) {
Log.error(`* Failed to load ${component.name}:`, err);
throw err; // Re-throw to stop initialization
}
}

require('./Web');
require('./Models');
require('./Util');
require('./Util/YappyGitHub');
require('./Discord');
require('./GitHub');
Log.info('* All components initialized successfully');
} catch (err) {
Log.error('* Initialization failed:', err);
process.exit(1);
}
}

const logUnhandled = (type) => (err) => {
try {
Expand All @@ -23,3 +48,9 @@ const logUnhandled = (type) => (err) => {

process.on('unhandledRejection', logUnhandled('Rejection'));
process.on('uncaughtException', logUnhandled('Exception'));

// Start initialization
initialize().catch((err) => {
Log.error('Fatal initialization error:', err);
process.exit(1);
});
24 changes: 24 additions & 0 deletions lib/instrument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const YappyGitHub = require('./Util/YappyGitHub');

if (process.env.SENTRY) {
console.log(`Sentry | Initializing...`);

const Sentry = require('@sentry/node');
const tracesSampleRate = Number(process.env.SENTRY_SAMPLE_RATE) || 0;

Sentry.init({
dsn: process.env.SENTRY,
release: YappyGitHub.git.release,
environment:
process.env.NODE_ENV === 'production' ? 'production' : 'development',
integrations: [
Sentry.httpIntegration({ tracing: true }),
Sentry.expressIntegration(),
],
tracesSampleRate,
autoSessionTracking: false,
defaultIntegrations: false,
});

console.log(`Sentry | Initialized (sample rate = ${tracesSampleRate})`);
}
Loading

0 comments on commit 171c3d0

Please sign in to comment.