Skip to content

Commit

Permalink
feat(core): Add client outcomes for breadcrumbs buffer (#15082)
Browse files Browse the repository at this point in the history
ref getsentry/team-sdks#116

This PR implements a new client discard reason for `buffer_overflow`.
This will be used to track when the internal breadcrumbs buffer
overflows for the new logs product that we are working on. This is
documented in develop here:
getsentry/sentry-docs#12395

Note: The reason we have `buffer_overflow` as a separate item to
`queue_overflow` is that in the future when we send log items in
envelopes we'll increment `queue_overflow` for the transport queue. We
want to differentiate between the transport queue and the internal
buffer explicitly.
  • Loading branch information
AbhiPrasad committed Jan 23, 2025
1 parent d5f80af commit 6c848af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ class ScopeClass implements ScopeInterface {
...breadcrumb,
};

const breadcrumbs = this._breadcrumbs;
breadcrumbs.push(mergedBreadcrumb);
this._breadcrumbs = breadcrumbs.length > maxCrumbs ? breadcrumbs.slice(-maxCrumbs) : breadcrumbs;
this._breadcrumbs.push(mergedBreadcrumb);
if (this._breadcrumbs.length > maxCrumbs) {
this._breadcrumbs = this._breadcrumbs.slice(-maxCrumbs);
this._client?.recordDroppedEvent('buffer_overflow', 'log_item');
}

this._notifyScopeListeners();

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types-hoist/clientreport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export type EventDropReason =
| 'ratelimit_backoff'
| 'sample_rate'
| 'send_error'
| 'internal_sdk_error';
| 'internal_sdk_error'
| 'buffer_overflow';

export type Outcome = {
reason: EventDropReason;
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/types-hoist/datacategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type DataCategory =
| 'replay'
// Events with `event_type` csp, hpkp, expectct, expectstaple
| 'security'
// Attachment bytes stored (unused for rate limiting
// Attachment bytes stored (unused for rate limiting)
| 'attachment'
// Session update events
| 'session'
Expand All @@ -30,5 +30,9 @@ export type DataCategory =
| 'metric_bucket'
// Span
| 'span'
// Log event
| 'log_item'
// Log bytes stored (unused for rate limiting)
| 'log_byte'
// Unknown data category
| 'unknown';
16 changes: 16 additions & 0 deletions packages/core/test/lib/baseclient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ describe('BaseClient', () => {
expect(isolationScopeBreadcrumbs).toEqual([{ message: 'hello3', timestamp: expect.any(Number) }]);
});

test('it records `buffer_overflow` client discard reason when buffer overflows', () => {
const options = getDefaultTestClientOptions({ maxBreadcrumbs: 1 });
const client = new TestClient(options);
const recordLostEventSpy = jest.spyOn(client, 'recordDroppedEvent');
setCurrentClient(client);
getIsolationScope().setClient(client);
client.init();

addBreadcrumb({ message: 'hello1' });
addBreadcrumb({ message: 'hello2' });
addBreadcrumb({ message: 'hello3' });

expect(recordLostEventSpy).toHaveBeenCalledTimes(2);
expect(recordLostEventSpy).toHaveBeenLastCalledWith('buffer_overflow', 'log_item');
});

test('calls `beforeBreadcrumb` and adds the breadcrumb without any changes', () => {
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
Expand Down

0 comments on commit 6c848af

Please sign in to comment.