Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Fix Sentry error reporting on task runners #12495

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/@n8n/task-runner/src/config/sentry-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Config, Env } from '@n8n/config';

@Config
export class SentryConfig {
/** Sentry DSN */
/** Sentry DSN (data source name) */
@Env('N8N_SENTRY_DSN')
sentryDsn: string = '';
dsn: string = '';

//#region Metadata about the environment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('JsTaskRunner', () => {
...jsRunnerOpts,
},
sentryConfig: {
sentryDsn: '',
dsn: '',
deploymentName: '',
environment: '',
n8nVersion: '',
Expand Down
6 changes: 4 additions & 2 deletions packages/@n8n/task-runner/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ void (async function start() {
defaultTimezone: config.baseRunnerConfig.timezone,
});

if (config.sentryConfig.sentryDsn) {
const { dsn } = config.sentryConfig;

if (dsn) {
const { ErrorReporter } = await import('n8n-core');
errorReporter = Container.get(ErrorReporter);
await errorReporter.init('task_runner', config.sentryConfig.sentryDsn);
await errorReporter.init({ serverType: 'task_runner', ...config.sentryConfig });
}

runner = new JsTaskRunner(config);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ export abstract class BaseCommand extends Command {

async init(): Promise<void> {
this.errorReporter = Container.get(ErrorReporter);
await this.errorReporter.init(
this.instanceSettings.instanceType,
this.globalConfig.sentry.backendDsn,
);
await this.errorReporter.init({
serverType: this.instanceSettings.instanceType,
dsn: this.globalConfig.sentry.backendDsn,
});
initExpressionEvaluator();

process.once('SIGTERM', this.onTerminationSignal('SIGTERM'));
Expand Down
28 changes: 15 additions & 13 deletions packages/core/src/error-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { createHash } from 'node:crypto';
import type { InstanceType } from './InstanceSettings';
import { Logger } from './logging/logger';

type ErrorReporterInitOptions = {
serverType: InstanceType | 'task_runner';
dsn: string;
release?: string;
environment?: string;
serverName?: string;
netroy marked this conversation as resolved.
Show resolved Hide resolved
};

@Service()
export class ErrorReporter {
/** Hashes of error stack traces, to deduplicate error reports. */
Expand Down Expand Up @@ -44,22 +52,16 @@ export class ErrorReporter {
await close(timeoutInMs);
}

async init(instanceType: InstanceType | 'task_runner', dsn: string) {
async init(opts: ErrorReporterInitOptions) {
process.on('uncaughtException', (error) => {
this.error(error);
});

if (!dsn) return;
if (!opts.dsn) 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 { requestDataIntegration, rewriteFramesIntegration } = await import('@sentry/node');

Expand All @@ -72,11 +74,11 @@ export class ErrorReporter {
];

init({
dsn,
release,
environment,
dsn: opts.dsn,
release: opts.release ?? process.env.N8N_VERSION,
environment: opts.environment ?? process.env.ENVIRONMENT,
enableTracing: false,
serverName,
serverName: opts.serverName ?? process.env.DEPLOYMENT_NAME,
beforeBreadcrumb: () => null,
beforeSend: this.beforeSend.bind(this) as NodeOptions['beforeSend'],
integrations: (integrations) => [
Expand All @@ -95,7 +97,7 @@ export class ErrorReporter {
],
});

setTag('server_type', instanceType);
setTag('server_type', opts.serverType);

this.report = (error, options) => captureException(error, options);
}
Expand Down
Loading