Skip to content

Commit fea30a2

Browse files
authored
fix(core): Drop sampled-out errors before they take a promise buffer slot (#25751)
Since #22671 sampled-out errors run the whole event pipeline while holding a client buffer slot, so error bursts on Node fill it up and real errors get dropped as queue_overflow, fixed by dropping sampled-out errors right away when there is no ok session for them to update. I added tests showing sampled-out errors no longer overflow the buffer without a session and still go through the pipeline when one exists.
1 parent 70b6218 commit fea30a2

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

‎packages/core/src/client.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { makePromiseBuffer, type PromiseBuffer, SENTRY_BUFFER_FULL_ERROR } from
5252
import { safeMathRandom } from './utils/randomSafeContext';
5353
import { safeCallback } from './utils/safeCallback';
5454
import { reparentChildSpans, shouldIgnoreSpan } from './utils/should-ignore-span';
55+
import { rejectedSyncPromise } from './utils/syncpromise';
5556
import { safeUnref } from './utils/timer';
5657
import { convertSpanJsonToTransactionEvent, convertTransactionEventToSpanJson } from './utils/transactionEvent';
5758
import { maybeWarnAboutIgnoredTransactionOptions } from './utils/warnAboutIgnoredTransactionOptions';
@@ -105,6 +106,12 @@ function _makeDoNotSendEventError(message: string): DoNotSendEventError {
105106
};
106107
}
107108

109+
function _makeSampledOutError(sampleRate: ClientOptions['sampleRate']): DoNotSendEventError {
110+
return _makeDoNotSendEventError(
111+
`Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`,
112+
);
113+
}
114+
108115
function _isInternalError(error: unknown): error is InternalError {
109116
return isObjectLike(error) && INTERNAL_ERROR_SYMBOL in error;
110117
}
@@ -1550,11 +1557,20 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
15501557
// 0.0 === 0% events are sent
15511558
// Sampling for transaction happens somewhere else
15521559
const parsedSampleRate = typeof sampleRate === 'undefined' ? undefined : parseSampleRate(sampleRate);
1560+
const isSampledOut = isError && typeof parsedSampleRate === 'number' && safeMathRandom() > parsedSampleRate;
15531561
const dataCategory = getDataCategoryByType(event.type);
15541562
// Spans that event processors removed are already recorded in `prepareEvent`, so all later
15551563
// span outcomes are relative to the span count of the prepared event.
15561564
let preparedSpanCount = 0;
15571565

1566+
const session = currentScope.getSession() || isolationScope.getSession();
1567+
// A sampled-out error only runs the pipeline to update the session. Without a session it could update,
1568+
// drop it right away so it doesn't hold a promise buffer slot while event processors run.
1569+
if (isSampledOut && session?.status !== 'ok') {
1570+
this.recordDroppedEvent('sample_rate', 'error');
1571+
return rejectedSyncPromise(_makeSampledOutError(sampleRate));
1572+
}
1573+
15581574
return this._prepareEvent(event, hint, currentScope, isolationScope)
15591575
.then(prepared => {
15601576
if (prepared === null) {
@@ -1602,16 +1618,13 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
16021618
throw _makeDoNotSendEventError(`${dropMessage}, will not send event.`);
16031619
}
16041620

1605-
const session = currentScope.getSession() || isolationScope.getSession();
16061621
if (isError && session) {
16071622
this._updateSessionFromEvent(session, processedEvent);
16081623
}
16091624

1610-
if (isError && typeof parsedSampleRate === 'number' && safeMathRandom() > parsedSampleRate) {
1625+
if (isSampledOut) {
16111626
this.recordDroppedEvent('sample_rate', 'error');
1612-
throw _makeDoNotSendEventError(
1613-
`Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`,
1614-
);
1627+
throw _makeSampledOutError(sampleRate);
16151628
}
16161629

16171630
if (isTransaction) {

‎packages/core/test/lib/client.test.ts‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,20 +552,21 @@ describe('Client', () => {
552552
expect(eventId).toEqual(lastEventId());
553553
});
554554

555-
test('sets lastEventId when an error is sampled out', () => {
555+
test('does not set lastEventId when an error is sampled out without a session', () => {
556556
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, sampleRate: 0 });
557557
const client = new TestClient(options);
558558

559-
const eventId = client.captureException(new Error('sampled-out exception'));
559+
client.captureException(new Error('sampled-out exception'));
560560

561-
expect(eventId).toEqual(lastEventId());
561+
expect(lastEventId()).toBeUndefined();
562562
expect(TestClient.instance!.event).toBeUndefined();
563563
});
564564

565-
test('(known limitation) replaces lastEventId with a sampled-out error ID', () => {
565+
test('(known limitation) replaces lastEventId with a sampled-out error ID when a session exists', () => {
566566
// After a successfully sent error, a subsequent sampled-out error replaces lastEventId() even though that new ID has no corresponding event in Sentry.
567-
// The `setLastEventId` call in `_prepareEvent` now executes before the `sampleRate` check
567+
// With a session, the `setLastEventId` call in `_prepareEvent` executes before the `sampleRate` check
568568
const client = new TestClient(getDefaultTestClientOptions({ dsn: PUBLIC_DSN }));
569+
getCurrentScope().setSession(makeSession());
569570

570571
client.captureException(new Error('sent exception'), { event_id: 'sent-event-id' });
571572
client.getOptions().sampleRate = 0;
@@ -4531,6 +4532,41 @@ describe('Client', () => {
45314532
]);
45324533
});
45334534

4535+
it('drops sampled-out errors without a session before they take a buffer slot', async () => {
4536+
const options = getDefaultTestClientOptions({
4537+
dsn: PUBLIC_DSN,
4538+
sampleRate: 0,
4539+
transportOptions: { bufferSize: 1 },
4540+
});
4541+
const client = new TestClient(options);
4542+
4543+
client.addIntegration(new AsyncTestIntegration());
4544+
4545+
client.captureException(new Error('first'));
4546+
client.captureException(new Error('second'));
4547+
client.captureException(new Error('third'));
4548+
4549+
expect(client._clearOutcomes()).toEqual([{ reason: 'sample_rate', category: 'error', quantity: 3 }]);
4550+
});
4551+
4552+
it('keeps processing sampled-out errors that can update the session', async () => {
4553+
const options = getDefaultTestClientOptions({
4554+
dsn: PUBLIC_DSN,
4555+
sampleRate: 0,
4556+
transportOptions: { bufferSize: 1 },
4557+
});
4558+
const client = new TestClient(options);
4559+
setCurrentClient(client);
4560+
getCurrentScope().setSession(makeSession());
4561+
4562+
client.addIntegration(new AsyncTestIntegration());
4563+
4564+
client.captureException(new Error('first'));
4565+
client.captureException(new Error('second'));
4566+
4567+
expect(client._clearOutcomes()).toEqual([{ reason: 'queue_overflow', category: 'error', quantity: 1 }]);
4568+
});
4569+
45344570
it('should skip the promise buffer with sync integrations', async () => {
45354571
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, transportOptions: { bufferSize: 1 } });
45364572
const client = new TestClient(options);

0 commit comments

Comments
 (0)