Skip to content

Commit 46eff72

Browse files
lforstmydea
andauthored
perf(core): Add short-circuits to eventFilters integration (#15752)
Ref: #15725 - Only does the options merging once - Adds short circuiting for event types to avoid doing things multiple times/unnecessarily --------- Co-authored-by: Francesco Gringl-Novy <[email protected]>
1 parent 7100519 commit 46eff72

File tree

1 file changed

+71
-65
lines changed

1 file changed

+71
-65
lines changed

packages/core/src/integrations/eventFilters.ts

+71-65
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ export interface EventFiltersOptions {
3535

3636
const INTEGRATION_NAME = 'EventFilters';
3737

38-
const _eventFiltersIntegration = ((options: Partial<EventFiltersOptions> = {}) => {
39-
return {
40-
name: INTEGRATION_NAME,
41-
processEvent(event, _hint, client) {
42-
const clientOptions = client.getOptions();
43-
const mergedOptions = _mergeOptions(options, clientOptions);
44-
return _shouldDropEvent(event, mergedOptions) ? null : event;
45-
},
46-
};
47-
}) satisfies IntegrationFn;
48-
4938
/**
5039
* An integration that filters out events (errors and transactions) based on:
5140
*
@@ -59,7 +48,23 @@ const _eventFiltersIntegration = ((options: Partial<EventFiltersOptions> = {}) =
5948
*
6049
* Events filtered by this integration will not be sent to Sentry.
6150
*/
62-
export const eventFiltersIntegration = defineIntegration(_eventFiltersIntegration);
51+
export const eventFiltersIntegration = defineIntegration((options: Partial<EventFiltersOptions> = {}) => {
52+
let mergedOptions: Partial<EventFiltersOptions> | undefined;
53+
return {
54+
name: INTEGRATION_NAME,
55+
setup(client) {
56+
const clientOptions = client.getOptions();
57+
mergedOptions = _mergeOptions(options, clientOptions);
58+
},
59+
processEvent(event, _hint, client) {
60+
if (!mergedOptions) {
61+
const clientOptions = client.getOptions();
62+
mergedOptions = _mergeOptions(options, clientOptions);
63+
}
64+
return _shouldDropEvent(event, mergedOptions) ? null : event;
65+
},
66+
};
67+
});
6368

6469
/**
6570
* An integration that filters out events (errors and transactions) based on:
@@ -102,66 +107,72 @@ function _mergeOptions(
102107
}
103108

104109
function _shouldDropEvent(event: Event, options: Partial<EventFiltersOptions>): boolean {
105-
if (options.ignoreInternal && _isSentryError(event)) {
106-
DEBUG_BUILD &&
107-
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
108-
return true;
109-
}
110-
if (_isIgnoredError(event, options.ignoreErrors)) {
111-
DEBUG_BUILD &&
112-
logger.warn(
113-
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
114-
);
115-
return true;
116-
}
117-
if (_isUselessError(event)) {
118-
DEBUG_BUILD &&
119-
logger.warn(
120-
`Event dropped due to not having an error message, error type or stacktrace.\nEvent: ${getEventDescription(
121-
event,
122-
)}`,
123-
);
124-
return true;
125-
}
126-
if (_isIgnoredTransaction(event, options.ignoreTransactions)) {
127-
DEBUG_BUILD &&
128-
logger.warn(
129-
`Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`,
130-
);
131-
return true;
132-
}
133-
if (_isDeniedUrl(event, options.denyUrls)) {
134-
DEBUG_BUILD &&
135-
logger.warn(
136-
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
137-
event,
138-
)}.\nUrl: ${_getEventFilterUrl(event)}`,
139-
);
140-
return true;
141-
}
142-
if (!_isAllowedUrl(event, options.allowUrls)) {
143-
DEBUG_BUILD &&
144-
logger.warn(
145-
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
146-
event,
147-
)}.\nUrl: ${_getEventFilterUrl(event)}`,
148-
);
149-
return true;
110+
if (!event.type) {
111+
// Filter errors
112+
113+
if (options.ignoreInternal && _isSentryError(event)) {
114+
DEBUG_BUILD &&
115+
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
116+
return true;
117+
}
118+
if (_isIgnoredError(event, options.ignoreErrors)) {
119+
DEBUG_BUILD &&
120+
logger.warn(
121+
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
122+
);
123+
return true;
124+
}
125+
if (_isUselessError(event)) {
126+
DEBUG_BUILD &&
127+
logger.warn(
128+
`Event dropped due to not having an error message, error type or stacktrace.\nEvent: ${getEventDescription(
129+
event,
130+
)}`,
131+
);
132+
return true;
133+
}
134+
if (_isDeniedUrl(event, options.denyUrls)) {
135+
DEBUG_BUILD &&
136+
logger.warn(
137+
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
138+
event,
139+
)}.\nUrl: ${_getEventFilterUrl(event)}`,
140+
);
141+
return true;
142+
}
143+
if (!_isAllowedUrl(event, options.allowUrls)) {
144+
DEBUG_BUILD &&
145+
logger.warn(
146+
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
147+
event,
148+
)}.\nUrl: ${_getEventFilterUrl(event)}`,
149+
);
150+
return true;
151+
}
152+
} else if (event.type === 'transaction') {
153+
// Filter transactions
154+
155+
if (_isIgnoredTransaction(event, options.ignoreTransactions)) {
156+
DEBUG_BUILD &&
157+
logger.warn(
158+
`Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`,
159+
);
160+
return true;
161+
}
150162
}
151163
return false;
152164
}
153165

154166
function _isIgnoredError(event: Event, ignoreErrors?: Array<string | RegExp>): boolean {
155-
// If event.type, this is not an error
156-
if (event.type || !ignoreErrors || !ignoreErrors.length) {
167+
if (!ignoreErrors?.length) {
157168
return false;
158169
}
159170

160171
return getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));
161172
}
162173

163174
function _isIgnoredTransaction(event: Event, ignoreTransactions?: Array<string | RegExp>): boolean {
164-
if (event.type !== 'transaction' || !ignoreTransactions || !ignoreTransactions.length) {
175+
if (!ignoreTransactions?.length) {
165176
return false;
166177
}
167178

@@ -223,11 +234,6 @@ function _getEventFilterUrl(event: Event): string | null {
223234
}
224235

225236
function _isUselessError(event: Event): boolean {
226-
if (event.type) {
227-
// event is not an error
228-
return false;
229-
}
230-
231237
// We only want to consider events for dropping that actually have recorded exception values.
232238
if (!event.exception?.values?.length) {
233239
return false;

0 commit comments

Comments
 (0)