Skip to content

Commit 49009ba

Browse files
committed
[docs] Add basic rate limiter for sentry
1 parent fe0dd10 commit 49009ba

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

docs/common/sentry-utilities.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ const ERRORS_TO_DISCARD = [
1919

2020
const REPORTED_ERRORS_KEY = 'sentry:reportedErrors';
2121
const TIMESTAMP_KEY = 'sentry:errorReportingInit';
22-
const ONE_DAY_MS = 60 * 60 * 24 * 1000;
22+
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
23+
const HALF_HOUR_MS = 0.5 * 60 * 60 * 1000;
2324

2425
export function preprocessSentryError(event: Event) {
2526
const message = getMessage(event);
2627

28+
// Check if it's rate limited to avoid sending the same error over and over
29+
if (isRateLimited(message || 'empty')) {
30+
return null;
31+
}
32+
2733
// If we don't know about this particular type of event then just pass it along
2834
if (!message) {
2935
return event;
@@ -73,6 +79,17 @@ function isLocalStorageAvailable(): boolean {
7379
}
7480
}
7581

82+
// https://github.com/getsentry/sentry-javascript/issues/435
83+
const rateLimiter: Record<string, number> = {};
84+
function isRateLimited(message: string) {
85+
if (rateLimiter[message] && rateLimiter[message] > Date.now()) {
86+
return true;
87+
}
88+
89+
rateLimiter[message] = Date.now() + HALF_HOUR_MS;
90+
return false;
91+
}
92+
7693
// Extract a stable event error message out of the Sentry event object
7794
function getMessage(event: Event) {
7895
if (event.message) {

0 commit comments

Comments
 (0)