From cdb7265a36883eacea9cb0e5c804dac0fad1303c Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Thu, 20 Mar 2025 12:24:42 +0100 Subject: [PATCH 1/2] perf(core): Add short-circuits to `eventFilters` integration --- .../core/src/integrations/eventFilters.ts | 136 +++++++++--------- 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/packages/core/src/integrations/eventFilters.ts b/packages/core/src/integrations/eventFilters.ts index fbf9475feca2..fdd86232bec1 100644 --- a/packages/core/src/integrations/eventFilters.ts +++ b/packages/core/src/integrations/eventFilters.ts @@ -35,17 +35,6 @@ export interface EventFiltersOptions { const INTEGRATION_NAME = 'EventFilters'; -const _eventFiltersIntegration = ((options: Partial = {}) => { - return { - name: INTEGRATION_NAME, - processEvent(event, _hint, client) { - const clientOptions = client.getOptions(); - const mergedOptions = _mergeOptions(options, clientOptions); - return _shouldDropEvent(event, mergedOptions) ? null : event; - }, - }; -}) satisfies IntegrationFn; - /** * An integration that filters out events (errors and transactions) based on: * @@ -59,7 +48,23 @@ const _eventFiltersIntegration = ((options: Partial = {}) = * * Events filtered by this integration will not be sent to Sentry. */ -export const eventFiltersIntegration = defineIntegration(_eventFiltersIntegration); +export const eventFiltersIntegration = defineIntegration((options: Partial = {}) => { + let mergedOptions: Partial | undefined; + return { + name: INTEGRATION_NAME, + setup(client) { + const clientOptions = client.getOptions(); + mergedOptions = _mergeOptions(options, clientOptions); + }, + processEvent(event, _hint, client) { + if (!mergedOptions) { + const clientOptions = client.getOptions(); + mergedOptions = _mergeOptions(options, clientOptions); + } + return _shouldDropEvent(event, mergedOptions) ? null : event; + }, + }; +}); /** * An integration that filters out events (errors and transactions) based on: @@ -102,58 +107,64 @@ function _mergeOptions( } function _shouldDropEvent(event: Event, options: Partial): boolean { - if (options.ignoreInternal && _isSentryError(event)) { - DEBUG_BUILD && - logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`); - return true; - } - if (_isIgnoredError(event, options.ignoreErrors)) { - DEBUG_BUILD && - logger.warn( - `Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`, - ); - return true; - } - if (_isUselessError(event)) { - DEBUG_BUILD && - logger.warn( - `Event dropped due to not having an error message, error type or stacktrace.\nEvent: ${getEventDescription( - event, - )}`, - ); - return true; - } - if (_isIgnoredTransaction(event, options.ignoreTransactions)) { - DEBUG_BUILD && - logger.warn( - `Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`, - ); - return true; - } - if (_isDeniedUrl(event, options.denyUrls)) { - DEBUG_BUILD && - logger.warn( - `Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription( - event, - )}.\nUrl: ${_getEventFilterUrl(event)}`, - ); - return true; - } - if (!_isAllowedUrl(event, options.allowUrls)) { - DEBUG_BUILD && - logger.warn( - `Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription( - event, - )}.\nUrl: ${_getEventFilterUrl(event)}`, - ); - return true; + if (event.type === undefined) { + // Filter errors + + if (options.ignoreInternal && _isSentryError(event)) { + DEBUG_BUILD && + logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`); + return true; + } + if (_isIgnoredError(event, options.ignoreErrors)) { + DEBUG_BUILD && + logger.warn( + `Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`, + ); + return true; + } + if (_isUselessError(event)) { + DEBUG_BUILD && + logger.warn( + `Event dropped due to not having an error message, error type or stacktrace.\nEvent: ${getEventDescription( + event, + )}`, + ); + return true; + } + if (_isDeniedUrl(event, options.denyUrls)) { + DEBUG_BUILD && + logger.warn( + `Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription( + event, + )}.\nUrl: ${_getEventFilterUrl(event)}`, + ); + return true; + } + if (!_isAllowedUrl(event, options.allowUrls)) { + DEBUG_BUILD && + logger.warn( + `Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription( + event, + )}.\nUrl: ${_getEventFilterUrl(event)}`, + ); + return true; + } + } else if (event.type === 'transaction') { + // Filter transactions + + if (_isIgnoredTransaction(event, options.ignoreTransactions)) { + DEBUG_BUILD && + logger.warn( + `Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`, + ); + return true; + } } return false; } function _isIgnoredError(event: Event, ignoreErrors?: Array): boolean { - // If event.type, this is not an error - if (event.type || !ignoreErrors || !ignoreErrors.length) { + if (!ignoreErrors?.length) { return false; } @@ -161,7 +172,7 @@ function _isIgnoredError(event: Event, ignoreErrors?: Array): b } function _isIgnoredTransaction(event: Event, ignoreTransactions?: Array): boolean { - if (event.type !== 'transaction' || !ignoreTransactions || !ignoreTransactions.length) { + if (!ignoreTransactions?.length) { return false; } @@ -223,11 +234,6 @@ function _getEventFilterUrl(event: Event): string | null { } function _isUselessError(event: Event): boolean { - if (event.type) { - // event is not an error - return false; - } - // We only want to consider events for dropping that actually have recorded exception values. if (!event.exception?.values?.length) { return false; From 37f972428ffbe2d01f7a3c42283c1c1b83194c13 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Thu, 20 Mar 2025 13:54:50 +0100 Subject: [PATCH 2/2] Update packages/core/src/integrations/eventFilters.ts Co-authored-by: Francesco Gringl-Novy --- packages/core/src/integrations/eventFilters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/integrations/eventFilters.ts b/packages/core/src/integrations/eventFilters.ts index fdd86232bec1..95bce0d758c8 100644 --- a/packages/core/src/integrations/eventFilters.ts +++ b/packages/core/src/integrations/eventFilters.ts @@ -107,7 +107,7 @@ function _mergeOptions( } function _shouldDropEvent(event: Event, options: Partial): boolean { - if (event.type === undefined) { + if (!event.type) { // Filter errors if (options.ignoreInternal && _isSentryError(event)) {