Skip to content

Commit 2d58238

Browse files
committed
metrics rate limits
1 parent ad2685a commit 2d58238

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/utils/src/ratelimit.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function updateRateLimits(
6767
* rate limit headers are of the form
6868
* <header>,<header>,..
6969
* where each <header> is of the form
70-
* <retry_after>: <categories>: <scope>: <reason_code>
70+
* <retry_after>: <categories>: <scope>: <reason_code>: <namespaces>
7171
* where
7272
* <retry_after> is a delay in seconds
7373
* <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form
@@ -85,7 +85,16 @@ export function updateRateLimits(
8585
updatedRateLimits.all = now + delay;
8686
} else {
8787
for (const category of categories.split(';')) {
88-
updatedRateLimits[category] = now + delay;
88+
if (category === 'metric_bucket') {
89+
const namespaces = limit.split(':', 5)[4] ? limit.split(':', 5)[4].split(';') : [];
90+
91+
if (!namespaces || namespaces.includes('custom')) {
92+
// back off transmitting metrics from the SDK
93+
updatedRateLimits[category] = now + delay;
94+
}
95+
} else {
96+
updatedRateLimits[category] = now + delay;
97+
}
8998
}
9099
}
91100
}

packages/utils/test/ratelimit.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,35 @@ describe('updateRateLimits()', () => {
197197
expect(updatedRateLimits.all).toEqual(60_000);
198198
});
199199
});
200+
201+
describe('data category "metric_bucket"', () => {
202+
test('should add limit for `metric_bucket` category when namespaces contain "custom"', () => {
203+
const rateLimits: RateLimits = {};
204+
const headers = {
205+
'retry-after': null,
206+
'x-sentry-rate-limits': '42:metric_bucket:::custom',
207+
};
208+
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
209+
expect(updatedRateLimits.metric_bucket).toEqual(42 * 1000);
210+
});
211+
212+
test('should not add limit for `metric_bucket` category when namespaces do not contain "custom"', () => {
213+
const rateLimits: RateLimits = {};
214+
const headers = {
215+
'retry-after': null,
216+
'x-sentry-rate-limits': '42:metric_bucket:::namespace1;namespace2',
217+
};
218+
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
219+
expect(updatedRateLimits.metric_bucket).toBeUndefined();
220+
});
221+
222+
test('should add limit for `metric_bucket` category when namespaces are empty', () => {
223+
const rateLimits: RateLimits = {};
224+
const headers = {
225+
'retry-after': null,
226+
'x-sentry-rate-limits': '42:metric_bucket',
227+
};
228+
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
229+
expect(updatedRateLimits.metric_bucket).toEqual(42 * 1000);
230+
});
231+
});

0 commit comments

Comments
 (0)