Skip to content

Commit

Permalink
feat(core): Add sentry for task runner (no-changelog) (n8n-io#11683)
Browse files Browse the repository at this point in the history
Co-authored-by: Iván Ovejero <[email protected]>
  • Loading branch information
tomi and ivov authored Nov 15, 2024
1 parent f4f0b51 commit f1ca8c1
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 49 deletions.
6 changes: 5 additions & 1 deletion docker/images/n8n/n8n-task-runners.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
"N8N_RUNNERS_MAX_CONCURRENCY",
"NODE_FUNCTION_ALLOW_BUILTIN",
"NODE_FUNCTION_ALLOW_EXTERNAL",
"NODE_OPTIONS"
"NODE_OPTIONS",
"N8N_SENTRY_DSN",
"N8N_VERSION",
"ENVIRONMENT",
"DEPLOYMENT_NAME"
],
"uid": 2000,
"gid": 2000
Expand Down
2 changes: 2 additions & 0 deletions packages/@n8n/task-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
},
"dependencies": {
"@n8n/config": "workspace:*",
"@sentry/integrations": "catalog:",
"@sentry/node": "catalog:",
"acorn": "8.14.0",
"acorn-walk": "8.3.4",
"n8n-core": "workspace:*",
Expand Down
31 changes: 31 additions & 0 deletions packages/@n8n/task-runner/src/__tests__/error-reporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mock } from 'jest-mock-extended';
import { ApplicationError } from 'n8n-workflow';

import { ErrorReporter } from '../error-reporter';

describe('ErrorReporter', () => {
const errorReporting = new ErrorReporter(mock());

describe('beforeSend', () => {
it('should return null if originalException is an ApplicationError with level warning', () => {
const hint = { originalException: new ApplicationError('Test error', { level: 'warning' }) };
expect(errorReporting.beforeSend(mock(), hint)).toBeNull();
});

it('should return event if originalException is an ApplicationError with level error', () => {
const hint = { originalException: new ApplicationError('Test error', { level: 'error' }) };
expect(errorReporting.beforeSend(mock(), hint)).not.toBeNull();
});

it('should return null if originalException is an Error with a non-unique stack', () => {
const hint = { originalException: new Error('Test error') };
errorReporting.beforeSend(mock(), hint);
expect(errorReporting.beforeSend(mock(), hint)).toBeNull();
});

it('should return event if originalException is an Error with a unique stack', () => {
const hint = { originalException: new Error('Test error') };
expect(errorReporting.beforeSend(mock(), hint)).not.toBeNull();
});
});
});
4 changes: 4 additions & 0 deletions packages/@n8n/task-runner/src/config/main-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Config, Nested } from '@n8n/config';

import { BaseRunnerConfig } from './base-runner-config';
import { JsRunnerConfig } from './js-runner-config';
import { SentryConfig } from './sentry-config';

@Config
export class MainConfig {
Expand All @@ -10,4 +11,7 @@ export class MainConfig {

@Nested
jsRunnerConfig!: JsRunnerConfig;

@Nested
sentryConfig!: SentryConfig;
}
21 changes: 21 additions & 0 deletions packages/@n8n/task-runner/src/config/sentry-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Config, Env } from '@n8n/config';

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

//#region Metadata about the environment

@Env('N8N_VERSION')
n8nVersion: string = '';

@Env('ENVIRONMENT')
environment: string = '';

@Env('DEPLOYMENT_NAME')
deploymentName: string = '';

//#endregion
}
93 changes: 93 additions & 0 deletions packages/@n8n/task-runner/src/error-reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { RewriteFrames } from '@sentry/integrations';
import { init, setTag, captureException, close } from '@sentry/node';
import type { ErrorEvent, EventHint } from '@sentry/types';
import * as a from 'assert/strict';
import { createHash } from 'crypto';
import { ApplicationError } from 'n8n-workflow';

import type { SentryConfig } from '@/config/sentry-config';

/**
* Handles error reporting using Sentry
*/
export class ErrorReporter {
private isInitialized = false;

/** Hashes of error stack traces, to deduplicate error reports. */
private readonly seenErrors = new Set<string>();

private get dsn() {
return this.sentryConfig.sentryDsn;
}

constructor(private readonly sentryConfig: SentryConfig) {
a.ok(this.dsn, 'Sentry DSN is required to initialize Sentry');
}

async start() {
if (this.isInitialized) return;

// Collect longer stacktraces
Error.stackTraceLimit = 50;

process.on('uncaughtException', captureException);

const ENABLED_INTEGRATIONS = [
'InboundFilters',
'FunctionToString',
'LinkedErrors',
'OnUnhandledRejection',
'ContextLines',
];

setTag('server_type', 'task_runner');

init({
dsn: this.dsn,
release: this.sentryConfig.n8nVersion,
environment: this.sentryConfig.environment,
enableTracing: false,
serverName: this.sentryConfig.deploymentName,
beforeBreadcrumb: () => null,
beforeSend: async (event, hint) => await this.beforeSend(event, hint),
integrations: (integrations) => [
...integrations.filter(({ name }) => ENABLED_INTEGRATIONS.includes(name)),
new RewriteFrames({ root: process.cwd() }),
],
});

this.isInitialized = true;
}

async stop() {
if (!this.isInitialized) {
return;
}

await close(1000);
}

async beforeSend(event: ErrorEvent, { originalException }: EventHint) {
if (!originalException) return null;

if (originalException instanceof Promise) {
originalException = await originalException.catch((error) => error as Error);
}

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 (this.seenErrors.has(eventHash)) return null;
this.seenErrors.add(eventHash);
}

return event;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('JsTaskRunner', () => {
...defaultConfig.jsRunnerConfig,
...opts,
},
sentryConfig: {
sentryDsn: '',
deploymentName: '',
environment: '',
n8nVersion: '',
},
});

const defaultTaskRunner = createRunnerWithOpts();
Expand Down
14 changes: 14 additions & 0 deletions packages/@n8n/task-runner/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { ensureError } from 'n8n-workflow';
import Container from 'typedi';

import { MainConfig } from './config/main-config';
import type { ErrorReporter } from './error-reporter';
import { JsTaskRunner } from './js-task-runner/js-task-runner';

let runner: JsTaskRunner | undefined;
let isShuttingDown = false;
let errorReporter: ErrorReporter | undefined;

function createSignalHandler(signal: string) {
return async function onSignal() {
Expand All @@ -21,10 +23,16 @@ function createSignalHandler(signal: string) {
await runner.stop();
runner = undefined;
}

if (errorReporter) {
await errorReporter.stop();
errorReporter = undefined;
}
} catch (e) {
const error = ensureError(e);
console.error('Error stopping task runner', { error });
} finally {
console.log('Task runner stopped');
process.exit(0);
}
};
Expand All @@ -33,6 +41,12 @@ function createSignalHandler(signal: string) {
void (async function start() {
const config = Container.get(MainConfig);

if (config.sentryConfig.sentryDsn) {
const { ErrorReporter } = await import('@/error-reporter');
errorReporter = new ErrorReporter(config.sentryConfig);
await errorReporter.start();
}

runner = new JsTaskRunner(config);

process.on('SIGINT', createSignalHandler('SIGINT'));
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
"@n8n_io/license-sdk": "2.13.1",
"@oclif/core": "4.0.7",
"@rudderstack/rudder-sdk-node": "2.0.9",
"@sentry/integrations": "7.87.0",
"@sentry/node": "7.87.0",
"@sentry/integrations": "catalog:",
"@sentry/node": "catalog:",
"aws4": "1.11.0",
"axios": "catalog:",
"bcryptjs": "2.4.3",
Expand Down
39 changes: 22 additions & 17 deletions packages/cli/src/runners/__tests__/task-runner-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,28 @@ describe('TaskRunnerProcess', () => {
taskRunnerProcess = new TaskRunnerProcess(logger, runnerConfig, authService);
});

test.each(['PATH', 'NODE_FUNCTION_ALLOW_BUILTIN', 'NODE_FUNCTION_ALLOW_EXTERNAL'])(
'should propagate %s from env as is',
async (envVar) => {
jest.spyOn(authService, 'createGrantToken').mockResolvedValue('grantToken');
process.env[envVar] = 'custom value';

await taskRunnerProcess.start();

// @ts-expect-error The type is not correct
const options = spawnMock.mock.calls[0][2] as SpawnOptions;
expect(options.env).toEqual(
expect.objectContaining({
[envVar]: 'custom value',
}),
);
},
);
test.each([
'PATH',
'NODE_FUNCTION_ALLOW_BUILTIN',
'NODE_FUNCTION_ALLOW_EXTERNAL',
'N8N_SENTRY_DSN',
'N8N_VERSION',
'ENVIRONMENT',
'DEPLOYMENT_NAME',
])('should propagate %s from env as is', async (envVar) => {
jest.spyOn(authService, 'createGrantToken').mockResolvedValue('grantToken');
process.env[envVar] = 'custom value';

await taskRunnerProcess.start();

// @ts-expect-error The type is not correct
const options = spawnMock.mock.calls[0][2] as SpawnOptions;
expect(options.env).toEqual(
expect.objectContaining({
[envVar]: 'custom value',
}),
);
});

it('should pass NODE_OPTIONS env if maxOldSpaceSize is configured', async () => {
jest.spyOn(authService, 'createGrantToken').mockResolvedValue('grantToken');
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/runners/task-runner-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export class TaskRunnerProcess extends TypedEmitter<TaskRunnerProcessEventMap> {
'PATH',
'NODE_FUNCTION_ALLOW_BUILTIN',
'NODE_FUNCTION_ALLOW_EXTERNAL',
'N8N_SENTRY_DSN',
// Metadata about the environment
'N8N_VERSION',
'ENVIRONMENT',
'DEPLOYMENT_NAME',
] as const;

constructor(
Expand Down
Loading

0 comments on commit f1ca8c1

Please sign in to comment.