Skip to content

fix: Add SentryRequestType to RateLimitingCategory mapping #3328

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

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/browser/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import {
} from '@sentry/types';
import { logger, parseRetryAfterHeader, PromiseBuffer, SentryError } from '@sentry/utils';

const CATEGORY_MAPPING: {
[key in SentryRequestType]: string;
} = {
event: 'error',
transaction: 'transaction',
session: 'session',
};

/** Base Transport class implementation */
export abstract class BaseTransport implements Transport {
/**
Expand Down Expand Up @@ -80,15 +88,16 @@ export abstract class BaseTransport implements Transport {
/**
* Gets the time that given category is disabled until for rate limiting
*/
protected _disabledUntil(category: string): Date {
protected _disabledUntil(requestType: SentryRequestType): Date {
const category = CATEGORY_MAPPING[requestType];
return this._rateLimits[category] || this._rateLimits.all;
}

/**
* Checks if a category is rate limited
*/
protected _isRateLimited(category: string): boolean {
return this._disabledUntil(category) > new Date(Date.now());
protected _isRateLimited(requestType: SentryRequestType): boolean {
return this._disabledUntil(requestType) > new Date(Date.now());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/test/unit/transports/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error:scope`);
fetch.returns(Promise.resolve({ status: 429, headers }));

try {
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event;transaction:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error;transaction:scope`);
fetch.returns(Promise.resolve({ status: 429, headers }));

try {
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event;transaction:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error;transaction:scope`);
fetch.returns(Promise.resolve({ status: 429, headers }));

try {
Expand Down Expand Up @@ -433,7 +433,7 @@ describe('FetchTransport', () => {
.returns(afterLimit);

const headers = new Headers();
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:event;transaction:scope`);
headers.set('X-Sentry-Rate-Limits', `${retryAfterSeconds}:error;transaction:scope`);
fetch.returns(Promise.resolve({ status: 200, headers }));

let eventRes = await transport.sendEvent(eventPayload);
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/test/unit/transports/xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('XHRTransport', () => {
const withinLimit = beforeLimit + (retryAfterSeconds / 2) * 1000;
const afterLimit = beforeLimit + retryAfterSeconds * 1000;

server.respondWith('POST', storeUrl, [429, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:event:scope` }, '']);
server.respondWith('POST', storeUrl, [429, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:error:scope` }, '']);
server.respondWith('POST', envelopeUrl, [200, {}, '']);

const dateStub = stub(Date, 'now')
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('XHRTransport', () => {

server.respondWith('POST', storeUrl, [
429,
{ 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:event;transaction:scope` },
{ 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:error;transaction:scope` },
'',
]);
server.respondWith('POST', envelopeUrl, [200, {}, '']);
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('XHRTransport', () => {
const withinLimit = beforeLimit + (retryAfterSeconds / 2) * 1000;
const afterLimit = beforeLimit + retryAfterSeconds * 1000;

server.respondWith('POST', storeUrl, [200, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:event:scope` }, '']);
server.respondWith('POST', storeUrl, [200, { 'X-Sentry-Rate-Limits': `${retryAfterSeconds}:error:scope` }, '']);

const dateStub = stub(Date, 'now')
// 1st event - _isRateLimited - false
Expand Down