Skip to content

Commit 19c8527

Browse files
author
Luca Forstner
authored
feat(core): Make global addBreadcrumb write to the isolation scope instead of current scope (#10586)
1 parent 3ef44a4 commit 19c8527

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

packages/core/src/hub.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ export class Hub implements HubInterface {
402402
*/
403403
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void {
404404
// eslint-disable-next-line deprecation/deprecation
405-
const { scope, client } = this.getStackTop();
405+
const { client } = this.getStackTop();
406406

407407
if (!client) return;
408408

@@ -421,15 +421,8 @@ export class Hub implements HubInterface {
421421

422422
client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint);
423423

424-
// TODO(v8): I know this comment doesn't make much sense because the hub will be deprecated but I still wanted to
425-
// write it down. In theory, we would have to add the breadcrumbs to the isolation scope here, however, that would
426-
// duplicate all of the breadcrumbs. There was the possibility of adding breadcrumbs to both, the isolation scope
427-
// and the normal scope, and deduplicating it down the line in the event processing pipeline. However, that would
428-
// have been very fragile, because the breadcrumb objects would have needed to keep their identity all throughout
429-
// the event processing pipeline.
430-
// In the new implementation, the top level `Sentry.addBreadcrumb()` should ONLY write to the isolation scope.
431-
432-
scope.addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
424+
// eslint-disable-next-line deprecation/deprecation
425+
this.getIsolationScope().addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
433426
}
434427

435428
/**

packages/core/test/lib/scope.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,38 @@ describe('Scope', () => {
473473
);
474474
});
475475
});
476+
477+
describe('addBreadcrumb()', () => {
478+
test('adds a breadcrumb', () => {
479+
const scope = new Scope();
480+
481+
scope.addBreadcrumb({ message: 'hello world' }, 100);
482+
483+
expect((scope as any)._breadcrumbs[0].message).toEqual('hello world');
484+
});
485+
486+
test('adds a timestamp to new breadcrumbs', () => {
487+
const scope = new Scope();
488+
489+
scope.addBreadcrumb({ message: 'hello world' }, 100);
490+
491+
expect((scope as any)._breadcrumbs[0].timestamp).toEqual(expect.any(Number));
492+
});
493+
494+
test('overrides the `maxBreadcrumbs` defined in client options', () => {
495+
const options = getDefaultTestClientOptions({ maxBreadcrumbs: 1 });
496+
const client = new TestClient(options);
497+
const scope = new Scope();
498+
499+
scope.setClient(client);
500+
501+
scope.addBreadcrumb({ message: 'hello' }, 100);
502+
scope.addBreadcrumb({ message: 'world' }, 100);
503+
scope.addBreadcrumb({ message: '!' }, 100);
504+
505+
expect((scope as any)._breadcrumbs).toHaveLength(3);
506+
});
507+
});
476508
});
477509

478510
describe('isolation scope', () => {

0 commit comments

Comments
 (0)