-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ratelimits): Add metric buckets rate limit #11506
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ export function updateRateLimits( | |
* rate limit headers are of the form | ||
* <header>,<header>,.. | ||
* where each <header> is of the form | ||
* <retry_after>: <categories>: <scope>: <reason_code> | ||
* <retry_after>: <categories>: <scope>: <reason_code>: <namespaces> | ||
* where | ||
* <retry_after> is a delay in seconds | ||
* <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form | ||
|
@@ -85,7 +85,16 @@ export function updateRateLimits( | |
updatedRateLimits.all = now + delay; | ||
} else { | ||
for (const category of categories.split(';')) { | ||
updatedRateLimits[category] = now + delay; | ||
if (category === 'metric_bucket') { | ||
const namespaces = limit.split(':', 5)[4] ? limit.split(':', 5)[4].split(';') : null; | ||
|
||
if (!namespaces || namespaces.includes('custom')) { | ||
// back off transmitting metrics from the SDK | ||
updatedRateLimits[category] = now + delay; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the same as line 96, can we merge the logic somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added all in one |
||
} | ||
} else { | ||
updatedRateLimits[category] = now + delay; | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,3 +197,35 @@ describe('updateRateLimits()', () => { | |
expect(updatedRateLimits.all).toEqual(60_000); | ||
}); | ||
}); | ||
|
||
describe('data category "metric_bucket"', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add another test case for |
||
test('should add limit for `metric_bucket` category when namespaces contain "custom"', () => { | ||
const rateLimits: RateLimits = {}; | ||
const headers = { | ||
'retry-after': null, | ||
'x-sentry-rate-limits': '42:metric_bucket:::custom', | ||
}; | ||
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0); | ||
expect(updatedRateLimits.metric_bucket).toEqual(42 * 1000); | ||
}); | ||
|
||
test('should not add limit for `metric_bucket` category when namespaces do not contain "custom"', () => { | ||
const rateLimits: RateLimits = {}; | ||
const headers = { | ||
'retry-after': null, | ||
'x-sentry-rate-limits': '42:metric_bucket:::namespace1;namespace2', | ||
}; | ||
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0); | ||
expect(updatedRateLimits.metric_bucket).toBeUndefined(); | ||
}); | ||
|
||
test('should add limit for `metric_bucket` category when namespaces are empty', () => { | ||
const rateLimits: RateLimits = {}; | ||
const headers = { | ||
'retry-after': null, | ||
'x-sentry-rate-limits': '42:metric_bucket', | ||
}; | ||
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0); | ||
expect(updatedRateLimits.metric_bucket).toEqual(42 * 1000); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we combine this
limit.split
call with the one above inconst [retryAfter, categories]
?also generally we prefer against using
null
,undefined
is returned by default when array access returns nothing, and it helps reduce bundle size generally.