Skip to content

Commit 88c0838

Browse files
authored
fix(core): Fix Sentry error reporting on task runners (#12495)
1 parent dd36bb2 commit 88c0838

File tree

7 files changed

+62
-19
lines changed

7 files changed

+62
-19
lines changed

packages/@n8n/config/src/configs/sentry.config.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,35 @@ import { Config, Env } from '../decorators';
22

33
@Config
44
export class SentryConfig {
5-
/** Sentry DSN for the backend. */
5+
/** Sentry DSN (data source name) for the backend. */
66
@Env('N8N_SENTRY_DSN')
77
backendDsn: string = '';
88

9-
/** Sentry DSN for the frontend . */
9+
/** Sentry DSN (data source name) for the frontend. */
1010
@Env('N8N_FRONTEND_SENTRY_DSN')
1111
frontendDsn: string = '';
12+
13+
/**
14+
* Version of the n8n instance
15+
*
16+
* @example '1.73.0'
17+
*/
18+
@Env('N8N_VERSION')
19+
n8nVersion: string = '';
20+
21+
/**
22+
* Environment of the n8n instance.
23+
*
24+
* @example 'production'
25+
*/
26+
@Env('ENVIRONMENT')
27+
environment: string = '';
28+
29+
/**
30+
* Name of the deployment, e.g. cloud account name.
31+
*
32+
* @example 'janober'
33+
*/
34+
@Env('DEPLOYMENT_NAME')
35+
deploymentName: string = '';
1236
}

packages/@n8n/config/test/config.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ describe('GlobalConfig', () => {
236236
sentry: {
237237
backendDsn: '',
238238
frontendDsn: '',
239+
n8nVersion: '',
240+
environment: '',
241+
deploymentName: '',
239242
},
240243
logging: {
241244
level: 'info',

packages/@n8n/task-runner/src/config/sentry-config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Config, Env } from '@n8n/config';
22

33
@Config
44
export class SentryConfig {
5-
/** Sentry DSN */
5+
/** Sentry DSN (data source name) */
66
@Env('N8N_SENTRY_DSN')
7-
sentryDsn: string = '';
7+
dsn: string = '';
88

99
//#region Metadata about the environment
1010

packages/@n8n/task-runner/src/js-task-runner/__tests__/js-task-runner.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('JsTaskRunner', () => {
5050
...jsRunnerOpts,
5151
},
5252
sentryConfig: {
53-
sentryDsn: '',
53+
dsn: '',
5454
deploymentName: '',
5555
environment: '',
5656
n8nVersion: '',

packages/@n8n/task-runner/src/start.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,19 @@ void (async function start() {
5454
defaultTimezone: config.baseRunnerConfig.timezone,
5555
});
5656

57-
if (config.sentryConfig.sentryDsn) {
57+
const { dsn } = config.sentryConfig;
58+
59+
if (dsn) {
5860
const { ErrorReporter } = await import('n8n-core');
5961
errorReporter = Container.get(ErrorReporter);
60-
await errorReporter.init('task_runner', config.sentryConfig.sentryDsn);
62+
const { deploymentName, environment, n8nVersion } = config.sentryConfig;
63+
await errorReporter.init({
64+
serverType: 'task_runner',
65+
dsn,
66+
serverName: deploymentName,
67+
environment,
68+
release: n8nVersion,
69+
});
6170
}
6271

6372
runner = new JsTaskRunner(config);

packages/cli/src/commands/base-command.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ export abstract class BaseCommand extends Command {
6161

6262
async init(): Promise<void> {
6363
this.errorReporter = Container.get(ErrorReporter);
64-
await this.errorReporter.init(
65-
this.instanceSettings.instanceType,
66-
this.globalConfig.sentry.backendDsn,
67-
);
64+
65+
const { backendDsn, n8nVersion, environment, deploymentName } = this.globalConfig.sentry;
66+
await this.errorReporter.init({
67+
serverType: this.instanceSettings.instanceType,
68+
dsn: backendDsn,
69+
environment,
70+
release: n8nVersion,
71+
serverName: deploymentName,
72+
});
6873
initExpressionEvaluator();
6974

7075
process.once('SIGTERM', this.onTerminationSignal('SIGTERM'));

packages/core/src/error-reporter.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { createHash } from 'node:crypto';
99
import type { InstanceType } from './InstanceSettings';
1010
import { Logger } from './logging/logger';
1111

12+
type ErrorReporterInitOptions = {
13+
serverType: InstanceType | 'task_runner';
14+
dsn: string;
15+
release: string;
16+
environment: string;
17+
serverName: string;
18+
};
19+
1220
@Service()
1321
export class ErrorReporter {
1422
/** Hashes of error stack traces, to deduplicate error reports. */
@@ -44,7 +52,7 @@ export class ErrorReporter {
4452
await close(timeoutInMs);
4553
}
4654

47-
async init(instanceType: InstanceType | 'task_runner', dsn: string) {
55+
async init({ dsn, serverType, release, environment, serverName }: ErrorReporterInitOptions) {
4856
process.on('uncaughtException', (error) => {
4957
this.error(error);
5058
});
@@ -54,12 +62,6 @@ export class ErrorReporter {
5462
// Collect longer stacktraces
5563
Error.stackTraceLimit = 50;
5664

57-
const {
58-
N8N_VERSION: release,
59-
ENVIRONMENT: environment,
60-
DEPLOYMENT_NAME: serverName,
61-
} = process.env;
62-
6365
const { init, captureException, setTag } = await import('@sentry/node');
6466
const { requestDataIntegration, rewriteFramesIntegration } = await import('@sentry/node');
6567

@@ -95,7 +97,7 @@ export class ErrorReporter {
9597
],
9698
});
9799

98-
setTag('server_type', instanceType);
100+
setTag('server_type', serverType);
99101

100102
this.report = (error, options) => captureException(error, options);
101103
}

0 commit comments

Comments
 (0)