mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 15:34: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 cluster from 'node:cluster';
 | 
				
			||||||
 | 
					import * as Sentry from '@sentry/node';
 | 
				
			||||||
 | 
					import { nodeProfilingIntegration } from '@sentry/profiling-node';
 | 
				
			||||||
import { envOption } from '@/env.js';
 | 
					import { envOption } from '@/env.js';
 | 
				
			||||||
 | 
					import { loadConfig } from '@/config.js';
 | 
				
			||||||
import { jobQueue, server } from './common.js';
 | 
					import { jobQueue, server } from './common.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Init worker process
 | 
					 * Init worker process
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export async function workerMain() {
 | 
					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) {
 | 
						if (envOption.onlyServer) {
 | 
				
			||||||
		await server();
 | 
							await server();
 | 
				
			||||||
	} else if (envOption.onlyQueue) {
 | 
						} else if (envOption.onlyQueue) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -165,7 +165,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
 | 
				
			||||||
			this.systemQueueWorker
 | 
								this.systemQueueWorker
 | 
				
			||||||
				.on('active', (job) => systemLogger.debug(`active id=${job.id}`))
 | 
									.on('active', (job) => systemLogger.debug(`active id=${job.id}`))
 | 
				
			||||||
				.on('completed', (job, result) => systemLogger.debug(`completed(${result}) 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('error', (err: Error) => systemLogger.error(`error ${err.stack}`, { e: renderError(err) }))
 | 
				
			||||||
				.on('stalled', (jobId) => systemLogger.warn(`stalled id=${jobId}`));
 | 
									.on('stalled', (jobId) => systemLogger.warn(`stalled id=${jobId}`));
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -214,7 +214,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
 | 
				
			||||||
			this.dbQueueWorker
 | 
								this.dbQueueWorker
 | 
				
			||||||
				.on('active', (job) => dbLogger.debug(`active id=${job.id}`))
 | 
									.on('active', (job) => dbLogger.debug(`active id=${job.id}`))
 | 
				
			||||||
				.on('completed', (job, result) => dbLogger.debug(`completed(${result}) 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('error', (err: Error) => dbLogger.error(`error ${err.stack}`, { e: renderError(err) }))
 | 
				
			||||||
				.on('stalled', (jobId) => dbLogger.warn(`stalled id=${jobId}`));
 | 
									.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) {
 | 
							if (err instanceof ApiError || err instanceof AuthenticationError) {
 | 
				
			||||||
			throw err;
 | 
								throw err;
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
| 
						 | 
					@ -108,10 +108,12 @@ export class ApiCallService implements OnApplicationShutdown {
 | 
				
			||||||
					id: errId,
 | 
										id: errId,
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
			console.error(err, errId);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (this.config.sentryForBackend) {
 | 
								if (this.config.sentryForBackend) {
 | 
				
			||||||
				Sentry.captureMessage(`Internal error occurred in ${ep.name}: ${err.message}`, {
 | 
									Sentry.captureMessage(`Internal error occurred in ${ep.name}: ${err.message}`, {
 | 
				
			||||||
 | 
										user: {
 | 
				
			||||||
 | 
											id: userId,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
					extra: {
 | 
										extra: {
 | 
				
			||||||
						ep: ep.name,
 | 
											ep: ep.name,
 | 
				
			||||||
						ps: data,
 | 
											ps: data,
 | 
				
			||||||
| 
						 | 
					@ -410,9 +412,13 @@ export class ApiCallService implements OnApplicationShutdown {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// API invoking
 | 
							// API invoking
 | 
				
			||||||
		if (this.config.sentryForBackend) {
 | 
							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 {
 | 
							} 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