convert error reporting to a DI service

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2024-10-16 11:34:02 +02:00
parent e7a4b0da01
commit d9ecab0672
No known key found for this signature in database
2 changed files with 105 additions and 96 deletions

View file

@ -21,7 +21,7 @@ import { LICENSE_FEATURES, inDevelopment, inTest } from '@/constants';
import * as CrashJournal from '@/crash-journal'; import * as CrashJournal from '@/crash-journal';
import * as Db from '@/db'; import * as Db from '@/db';
import { getDataDeduplicationService } from '@/deduplication'; import { getDataDeduplicationService } from '@/deduplication';
import { initErrorHandling } from '@/error-reporting'; import { ErrorReporting } from '@/error-reporting';
import { MessageEventBus } from '@/eventbus/message-event-bus/message-event-bus'; import { MessageEventBus } from '@/eventbus/message-event-bus/message-event-bus';
import { TelemetryEventRelay } from '@/events/relays/telemetry.event-relay'; import { TelemetryEventRelay } from '@/events/relays/telemetry.event-relay';
import { initExpressionEvaluator } from '@/expression-evaluator'; import { initExpressionEvaluator } from '@/expression-evaluator';
@ -61,7 +61,7 @@ export abstract class BaseCommand extends Command {
protected needsCommunityPackages = false; protected needsCommunityPackages = false;
async init(): Promise<void> { async init(): Promise<void> {
await initErrorHandling(); await Container.get(ErrorReporting).init();
initExpressionEvaluator(); initExpressionEvaluator();
process.once('SIGTERM', this.onTerminationSignal('SIGTERM')); process.once('SIGTERM', this.onTerminationSignal('SIGTERM'));

View file

@ -5,102 +5,111 @@ import { AxiosError } from 'axios';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import { InstanceSettings } from 'n8n-core'; import { InstanceSettings } from 'n8n-core';
import { ErrorReporterProxy, ApplicationError } from 'n8n-workflow'; import { ErrorReporterProxy, ApplicationError } from 'n8n-workflow';
import Container from 'typedi'; import { Service } from 'typedi';
let initialized = false; @Service()
export class ErrorReporting {
private initialized = false;
export const initErrorHandling = async () => { constructor(
if (initialized) return; private readonly globalConfig: GlobalConfig,
private readonly instanceSettings: InstanceSettings,
) {}
process.on('uncaughtException', (error) => { async init() {
ErrorReporterProxy.error(error); if (this.initialized) return;
});
const dsn = Container.get(GlobalConfig).sentry.backendDsn; process.on('uncaughtException', (error) => {
if (!dsn) { ErrorReporterProxy.error(error);
initialized = true; });
return;
const dsn = this.globalConfig.sentry.backendDsn;
if (!dsn) {
this.initialized = true;
return;
}
// Collect longer stacktraces
Error.stackTraceLimit = 50;
const {
N8N_VERSION: release,
ENVIRONMENT: environment,
DEPLOYMENT_NAME: serverName,
} = process.env;
const { init, captureException, setTag } = await import('@sentry/node');
const { RewriteFrames } = await import('@sentry/integrations');
const { Integrations } = await import('@sentry/node');
const enabledIntegrations = [
'InboundFilters',
'FunctionToString',
'LinkedErrors',
'OnUnhandledRejection',
'ContextLines',
];
const seenErrors = new Set<string>();
init({
dsn,
release,
environment,
enableTracing: false,
serverName,
beforeBreadcrumb: () => null,
integrations: (integrations) => [
...integrations.filter(({ name }) => enabledIntegrations.includes(name)),
new RewriteFrames({ root: process.cwd() }),
new Integrations.RequestData({
include: {
cookies: false,
data: false,
headers: false,
query_string: false,
url: true,
user: false,
},
}),
],
beforeSend(event, { originalException }) {
if (!originalException) return null;
if (originalException instanceof AxiosError) return null;
if (
originalException instanceof QueryFailedError &&
['SQLITE_FULL', 'SQLITE_IOERR'].some((errMsg) =>
originalException.message.includes(errMsg),
)
) {
return null;
}
if (originalException instanceof ApplicationError) {
const { level, extra, tags } = originalException;
if (level === 'warning') return null;
event.level = level;
if (extra) event.extra = { ...event.extra, ...extra };
if (tags) event.tags = { ...event.tags, ...tags };
}
if (originalException instanceof Error && originalException.stack) {
const eventHash = createHash('sha1').update(originalException.stack).digest('base64');
if (seenErrors.has(eventHash)) return null;
seenErrors.add(eventHash);
}
return event;
},
});
setTag('server_type', this.instanceSettings.instanceType);
ErrorReporterProxy.init({
report: (error, options) => captureException(error, options),
});
this.initialized = true;
} }
}
// Collect longer stacktraces
Error.stackTraceLimit = 50;
const {
N8N_VERSION: release,
ENVIRONMENT: environment,
DEPLOYMENT_NAME: serverName,
} = process.env;
const { init, captureException, setTag } = await import('@sentry/node');
const { RewriteFrames } = await import('@sentry/integrations');
const { Integrations } = await import('@sentry/node');
const enabledIntegrations = [
'InboundFilters',
'FunctionToString',
'LinkedErrors',
'OnUnhandledRejection',
'ContextLines',
];
const seenErrors = new Set<string>();
init({
dsn,
release,
environment,
enableTracing: false,
serverName,
beforeBreadcrumb: () => null,
integrations: (integrations) => [
...integrations.filter(({ name }) => enabledIntegrations.includes(name)),
new RewriteFrames({ root: process.cwd() }),
new Integrations.RequestData({
include: {
cookies: false,
data: false,
headers: false,
query_string: false,
url: true,
user: false,
},
}),
],
beforeSend(event, { originalException }) {
if (!originalException) return null;
if (originalException instanceof AxiosError) return null;
if (
originalException instanceof QueryFailedError &&
['SQLITE_FULL', 'SQLITE_IOERR'].some((errMsg) => originalException.message.includes(errMsg))
) {
return null;
}
if (originalException instanceof ApplicationError) {
const { level, extra, tags } = originalException;
if (level === 'warning') return null;
event.level = level;
if (extra) event.extra = { ...event.extra, ...extra };
if (tags) event.tags = { ...event.tags, ...tags };
}
if (originalException instanceof Error && originalException.stack) {
const eventHash = createHash('sha1').update(originalException.stack).digest('base64');
if (seenErrors.has(eventHash)) return null;
seenErrors.add(eventHash);
}
return event;
},
});
setTag('server_type', Container.get(InstanceSettings).instanceType);
ErrorReporterProxy.init({
report: (error, options) => captureException(error, options),
});
initialized = true;
};