Skip to content

Commit

Permalink
enhance(backend): improve sentry integration
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Jun 6, 2024
1 parent 65d1927 commit ab69e11
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
23 changes: 23 additions & 0 deletions packages/backend/src/boot/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,36 @@
*/

import cluster from 'node:cluster';
import * as Sentry from '@sentry/node';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { envOption } from '@/env.js';
import { loadConfig } from '@/config.js';
import { jobQueue, server } from './common.js';

/**
* Init worker process
*/
export async function workerMain() {
const config = loadConfig();

if (config.sentryForBackend) {
Sentry.init({
integrations: [
...(config.sentryForBackend.enableNodeProfiling ? [nodeProfilingIntegration()] : []),
],

// Performance Monitoring
tracesSampleRate: 1.0, // Capture 100% of the transactions

// Set sampling rate for profiling - this is relative to tracesSampleRate
profilesSampleRate: 1.0,

maxBreadcrumbs: 0,

...config.sentryForBackend.options,
});
}

if (envOption.onlyServer) {
await server();
} else if (envOption.onlyQueue) {
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/queue/QueueProcessorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
this.systemQueueWorker
.on('active', (job) => systemLogger.debug(`active id=${job.id}`))
.on('completed', (job, result) => systemLogger.debug(`completed(${result}) id=${job.id}`))
.on('failed', (job, err) => systemLogger.warn(`failed(${err.stack}) id=${job ? job.id : '-'}`, { job, e: renderError(err) }))
.on('failed', (job, err) => systemLogger.error(`failed(${err.stack}) id=${job ? job.id : '-'}`, { job, e: renderError(err) }))
.on('error', (err: Error) => systemLogger.error(`error ${err.stack}`, { e: renderError(err) }))
.on('stalled', (jobId) => systemLogger.warn(`stalled id=${jobId}`));
}
Expand Down Expand Up @@ -214,7 +214,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
this.dbQueueWorker
.on('active', (job) => dbLogger.debug(`active id=${job.id}`))
.on('completed', (job, result) => dbLogger.debug(`completed(${result}) id=${job.id}`))
.on('failed', (job, err) => dbLogger.warn(`failed(${err.stack}) id=${job ? job.id : '-'}`, { job, e: renderError(err) }))
.on('failed', (job, err) => dbLogger.error(`failed(${err.stack}) id=${job ? job.id : '-'}`, { job, e: renderError(err) }))
.on('error', (err: Error) => dbLogger.error(`error ${err.stack}`, { e: renderError(err) }))
.on('stalled', (jobId) => dbLogger.warn(`stalled id=${jobId}`));
}
Expand Down
14 changes: 10 additions & 4 deletions packages/backend/src/server/api/ApiCallService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ApiCallService implements OnApplicationShutdown {
}
}

#onExecError(ep: IEndpoint, data: any, err: Error): void {
#onExecError(ep: IEndpoint, data: any, err: Error, userId?: MiUser['id']): void {
if (err instanceof ApiError || err instanceof AuthenticationError) {
throw err;
} else {
Expand All @@ -108,10 +108,12 @@ export class ApiCallService implements OnApplicationShutdown {
id: errId,
},
});
console.error(err, errId);

if (this.config.sentryForBackend) {
Sentry.captureMessage(`Internal error occurred in ${ep.name}: ${err.message}`, {
user: {
id: userId,
},
extra: {
ep: ep.name,
ps: data,
Expand Down Expand Up @@ -410,9 +412,13 @@ export class ApiCallService implements OnApplicationShutdown {

// API invoking
if (this.config.sentryForBackend) {
return await Sentry.startSpan({ name: 'API: ' + ep.name }, () => ep.exec(data, user, token, file, request.ip, request.headers).catch((err: Error) => this.#onExecError(ep, data, err)));
return await Sentry.startSpan({
name: 'API: ' + ep.name,
}, () => ep.exec(data, user, token, file, request.ip, request.headers)
.catch((err: Error) => this.#onExecError(ep, data, err, user?.id)));
} else {
return await ep.exec(data, user, token, file, request.ip, request.headers).catch((err: Error) => this.#onExecError(ep, data, err));
return await ep.exec(data, user, token, file, request.ip, request.headers)
.catch((err: Error) => this.#onExecError(ep, data, err, user?.id));
}
}

Expand Down

0 comments on commit ab69e11

Please sign in to comment.