n8n/packages/cli/src/ErrorReporting.ts
कारतोफ्फेलस्क्रिप्ट™ a651089a10
refactor(core): Update backend sentry setup to reduce noise (no-changelog) (#8026)
## Summary
This PR updates our backend sentry setup to remove integrations that
don't provide us any value. This also reduces the amount of PII that
gets sent to Sentry.

[Sample event](https://n8nio.sentry.io/issues/4725315362/)

## Related tickets
[ENG-95](https://linear.app/n8n/issue/ENG-95)

## Review / Merge checklist
- [x] PR title and summary are descriptive.
2023-12-18 14:26:07 +01:00

89 lines
2.1 KiB
TypeScript

import { createHash } from 'crypto';
import config from '@/config';
import { ErrorReporterProxy, ApplicationError } from 'n8n-workflow';
let initialized = false;
export const initErrorHandling = async () => {
if (initialized) return;
process.on('uncaughtException', (error) => {
ErrorReporterProxy.error(error);
});
const dsn = config.getEnv('diagnostics.config.sentry.dsn');
if (!dsn) {
initialized = true;
return;
}
// Collect longer stacktraces
Error.stackTraceLimit = 50;
const {
N8N_VERSION: release,
ENVIRONMENT: environment,
DEPLOYMENT_NAME: serverName,
} = process.env;
const { init, captureException, addEventProcessor } = await import('@sentry/node');
const { RewriteFrames } = await import('@sentry/integrations');
const { Integrations } = await import('@sentry/node');
const enabledIntegrations = [
'InboundFilters',
'FunctionToString',
'LinkedErrors',
'OnUnhandledRejection',
'ContextLines',
];
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,
},
}),
],
});
const seenErrors = new Set<string>();
addEventProcessor((event, { originalException }) => {
if (!originalException) 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 };
}
const eventHash = createHash('sha1').update(JSON.stringify(originalException)).digest('base64');
if (seenErrors.has(eventHash)) return null;
seenErrors.add(eventHash);
return event;
});
ErrorReporterProxy.init({
report: (error, options) => captureException(error, options),
});
initialized = true;
};