mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-03 23:14:13 +00:00 
			
		
		
		
	enhance(backend): improve sentry integration
This commit is contained in:
		
							parent
							
								
									65d19279a2
								
							
						
					
					
						commit
						ab69e113f4
					
				
					 3 changed files with 35 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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}`));
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -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}`));
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			@ -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));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue