forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add sentry for task runner (no-changelog) (n8n-io#11683)
Co-authored-by: Iván Ovejero <[email protected]>
- Loading branch information
Showing
13 changed files
with
240 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/@n8n/task-runner/src/__tests__/error-reporter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.