Skip to content

feat(core): Make global addBreadcrumb write to the isolation scope instead of current scope #10586

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 4 commits into from
Feb 12, 2024
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
13 changes: 3 additions & 10 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export class Hub implements HubInterface {
*/
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void {
// eslint-disable-next-line deprecation/deprecation
const { scope, client } = this.getStackTop();
const { client } = this.getStackTop();

if (!client) return;

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

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

// TODO(v8): I know this comment doesn't make much sense because the hub will be deprecated but I still wanted to
// write it down. In theory, we would have to add the breadcrumbs to the isolation scope here, however, that would
// duplicate all of the breadcrumbs. There was the possibility of adding breadcrumbs to both, the isolation scope
// and the normal scope, and deduplicating it down the line in the event processing pipeline. However, that would
// have been very fragile, because the breadcrumb objects would have needed to keep their identity all throughout
// the event processing pipeline.
// In the new implementation, the top level `Sentry.addBreadcrumb()` should ONLY write to the isolation scope.

scope.addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,38 @@ describe('Scope', () => {
);
});
});

describe('addBreadcrumb()', () => {
test('adds a breadcrumb', () => {
const scope = new Scope();

scope.addBreadcrumb({ message: 'hello world' }, 100);

expect((scope as any)._breadcrumbs[0].message).toEqual('hello world');
});

test('adds a timestamp to new breadcrumbs', () => {
const scope = new Scope();

scope.addBreadcrumb({ message: 'hello world' }, 100);

expect((scope as any)._breadcrumbs[0].timestamp).toEqual(expect.any(Number));
});

test('overrides the `maxBreadcrumbs` defined in client options', () => {
const options = getDefaultTestClientOptions({ maxBreadcrumbs: 1 });
const client = new TestClient(options);
const scope = new Scope();

scope.setClient(client);

scope.addBreadcrumb({ message: 'hello' }, 100);
scope.addBreadcrumb({ message: 'world' }, 100);
scope.addBreadcrumb({ message: '!' }, 100);

expect((scope as any)._breadcrumbs).toHaveLength(3);
});
});
});

describe('isolation scope', () => {
Expand Down