Skip to content

feat(replay): Stop without retry when receiving bad API response #6773

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 1 commit into from
Jan 16, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<button onclick="console.log('Test log')">Click me</button>
</body>
</html>
37 changes: 37 additions & 0 deletions packages/integration-tests/suites/replay/errorResponse/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../utils/fixtures';
import { getReplaySnapshot } from '../../../utils/helpers';

sentryTest('errorResponse', async ({ getLocalTestPath, page }) => {
// Currently bundle tests are not supported for replay
if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle_')) {
sentryTest.skip();
}

let called = 0;

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
called++;

return route.fulfill({
status: 400,
});
});

const url = await getLocalTestPath({ testDir: __dirname });
await page.goto(url);

await page.click('button');
await page.waitForTimeout(300);

expect(called).toBe(1);

// Should immediately skip retrying and just cancel, no backoff
await page.waitForTimeout(5001);

expect(called).toBe(1);
const replay = await getReplaySnapshot(page);

expect(replay['_isEnabled']).toBe(false);
});
3 changes: 2 additions & 1 deletion packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export class ReplayContainer implements ReplayContainerInterface {
this._stopRecording?.();
this.eventBuffer?.destroy();
this.eventBuffer = null;
this._debouncedFlush.cancel();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small thing I noticed while debugging tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

} catch (err) {
this._handleException(err);
}
Expand Down Expand Up @@ -907,7 +908,7 @@ export class ReplayContainer implements ReplayContainerInterface {
if (rateLimitDuration > 0) {
__DEBUG_BUILD__ && logger.warn('[Replay]', `Rate limit hit, pausing replay for ${rateLimitDuration}ms`);
this.pause();
this._debouncedFlush && this._debouncedFlush.cancel();
this._debouncedFlush.cancel();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is set in the constructor, so no need to check for existence here.


setTimeout(() => {
__DEBUG_BUILD__ && logger.info('[Replay]', 'Resuming replay after rate limit');
Expand Down
4 changes: 2 additions & 2 deletions packages/replay/src/util/sendReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { captureException, setContext } from '@sentry/core';

import { RETRY_BASE_INTERVAL, RETRY_MAX_COUNT, UNABLE_TO_SEND_REPLAY } from '../constants';
import type { SendReplayData } from '../types';
import { RateLimitError, sendReplayRequest } from './sendReplayRequest';
import { RateLimitError, sendReplayRequest, TransportStatusCodeError } from './sendReplayRequest';

/**
* Finalize and send the current replay event to Sentry
Expand All @@ -25,7 +25,7 @@ export async function sendReplay(
await sendReplayRequest(replayData);
return true;
} catch (err) {
if (err instanceof RateLimitError) {
if (err instanceof RateLimitError || err instanceof TransportStatusCodeError) {
throw err;
}

Expand Down
27 changes: 22 additions & 5 deletions packages/replay/src/util/sendReplayRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,20 @@ export async function sendReplayRequest({
}

// TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore
if (response) {
const rateLimits = updateRateLimits({}, response);
if (isRateLimited(rateLimits, 'replay')) {
throw new RateLimitError(rateLimits);
}
if (!response) {
return response;
}

const rateLimits = updateRateLimits({}, response);
if (isRateLimited(rateLimits, 'replay')) {
throw new RateLimitError(rateLimits);
}

// If the status code is invalid, we want to immediately stop & not retry
if (typeof response.statusCode === 'number' && (response.statusCode < 200 || response.statusCode >= 300)) {
throw new TransportStatusCodeError(response.statusCode);
}

return response;
}

Expand All @@ -136,3 +144,12 @@ export class RateLimitError extends Error {
this.rateLimits = rateLimits;
}
}

/**
* This error indicates that the transport returned an invalid status code.
*/
export class TransportStatusCodeError extends Error {
public constructor(statusCode: number) {
super(`Transport returned status code ${statusCode}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('Integration | coreHandlers | handleGlobalEvent', () => {
replaysSessionSampleRate: 0.0,
replaysOnErrorSampleRate: 1.0,
},
autoStart: false,
}));
});

Expand Down
3 changes: 2 additions & 1 deletion packages/replay/test/mocks/resetSdkMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { DomHandler } from './../types';
import type { MockSdkParams } from './mockSdk';
import { mockSdk } from './mockSdk';

export async function resetSdkMock({ replayOptions, sentryOptions }: MockSdkParams): Promise<{
export async function resetSdkMock({ replayOptions, sentryOptions, autoStart }: MockSdkParams): Promise<{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that this was not actually passed through.

domHandler: DomHandler;
mockRecord: RecordMock;
replay: ReplayContainer;
Expand All @@ -30,6 +30,7 @@ export async function resetSdkMock({ replayOptions, sentryOptions }: MockSdkPara
const { replay } = await mockSdk({
replayOptions,
sentryOptions,
autoStart,
});

// XXX: This is needed to ensure `domHandler` is set
Expand Down