Skip to content
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

fix(core): Exclude client reports from offline queuing #7226

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 3 additions & 14 deletions packages/core/src/transports/offline.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import type { Envelope, InternalBaseTransportOptions, Transport, TransportMakeRequestResponse } from '@sentry/types';
import { forEachEnvelopeItem, logger, parseRetryAfterHeader } from '@sentry/utils';
import { envelopeContainsItemType, logger, parseRetryAfterHeader } from '@sentry/utils';

export const MIN_DELAY = 100; // 100 ms
export const START_DELAY = 5_000; // 5 seconds
const MAX_DELAY = 3.6e6; // 1 hour

function isReplayEnvelope(envelope: Envelope): boolean {
let isReplay = false;

forEachEnvelopeItem(envelope, (_, type) => {
if (type === 'replay_event') {
isReplay = true;
}
});

return isReplay;
}

function log(msg: string, error?: Error): void {
__DEBUG_BUILD__ && logger.info(`[Offline]: ${msg}`, error);
}
Expand Down Expand Up @@ -74,7 +62,8 @@ export function makeOfflineTransport<TO>(
// We don't queue Session Replay envelopes because they are:
// - Ordered and Replay relies on the response status to know when they're successfully sent.
// - Likely to fill the queue quickly and block other events from being sent.
if (isReplayEnvelope(env)) {
// We also want to drop client reports because they can be generated when we retry sending events while offline.
if (envelopeContainsItemType(env, ['replay_event', 'replay_recording', 'client_report'])) {
return false;
}

Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/lib/transports/offline.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
ClientReport,
Envelope,
EventEnvelope,
EventItem,
Expand All @@ -9,6 +10,7 @@ import type {
TransportMakeRequestResponse,
} from '@sentry/types';
import {
createClientReportEnvelope,
createEnvelope,
createEventEnvelopeHeaders,
dsnFromString,
Expand Down Expand Up @@ -54,6 +56,25 @@ const RELAY_ENVELOPE = createEnvelope<ReplayEnvelope>(
],
);

const DEFAULT_DISCARDED_EVENTS: ClientReport['discarded_events'] = [
{
reason: 'before_send',
category: 'error',
quantity: 30,
},
{
reason: 'network_error',
category: 'transaction',
quantity: 23,
},
];

const CLIENT_REPORT_ENVELOPE = createClientReportEnvelope(
DEFAULT_DISCARDED_EVENTS,
'https://public@dsn.ingest.sentry.io/1337',
123456,
);

const transportOptions = {
recordDroppedEvent: () => undefined, // noop
textEncoder: new TextEncoder(),
Expand Down Expand Up @@ -288,6 +309,23 @@ describe('makeOfflineTransport', () => {
expect(getCalls()).toEqual([]);
});

it('should not store client report envelopes on send failure', async () => {
const { getCalls, store } = createTestStore();
const { getSendCount, baseTransport } = createTestTransport(new Error());
const queuedCount = 0;
const transport = makeOfflineTransport(baseTransport)({
...transportOptions,
createStore: store,
shouldStore: () => true,
});
const result = transport.send(CLIENT_REPORT_ENVELOPE);

await expect(result).rejects.toBeInstanceOf(Error);
expect(queuedCount).toEqual(0);
expect(getSendCount()).toEqual(0);
expect(getCalls()).toEqual([]);
});

it(
'Follows the Retry-After header',
async () => {
Expand Down
27 changes: 21 additions & 6 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
DataCategory,
DsnComponents,
Envelope,
EnvelopeItem,
EnvelopeItemType,
Event,
EventEnvelopeHeaders,
Expand Down Expand Up @@ -41,16 +40,32 @@ export function addItemToEnvelope<E extends Envelope>(envelope: E, newItem: E[1]
/**
* Convenience function to loop through the items and item types of an envelope.
* (This function was mostly created because working with envelope types is painful at the moment)
*
* If the callback returns true, the rest of the items will be skipped.
*/
export function forEachEnvelopeItem<E extends Envelope>(
envelope: Envelope,
callback: (envelopeItem: E[1][number], envelopeItemType: E[1][number][0]['type']) => void,
): void {
callback: (envelopeItem: E[1][number], envelopeItemType: E[1][number][0]['type']) => boolean | void,
): boolean {
const envelopeItems = envelope[1];
envelopeItems.forEach((envelopeItem: EnvelopeItem) => {

for (const envelopeItem of envelopeItems) {
const envelopeItemType = envelopeItem[0].type;
callback(envelopeItem, envelopeItemType);
});
const result = callback(envelopeItem, envelopeItemType);

if (result) {
return true;
}
}

return false;
}

/**
* Returns true if the envelope contains any of the given envelope item types
*/
export function envelopeContainsItemType(envelope: Envelope, types: EnvelopeItemType[]): boolean {
return forEachEnvelopeItem(envelope, (_, type) => types.includes(type));
}

/**
Expand Down