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

feat(core): Add withIsolationScope #10141

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
trace,
withScope,
withIsolationScope,
autoDiscoverNodePerformanceMonitoringIntegrations,
makeNodeTransport,
defaultIntegrations,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export {
setTags,
setUser,
withScope,
withIsolationScope,
FunctionToString,
InboundFilters,
metrics,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
trace,
withScope,
withIsolationScope,
captureCheckIn,
withMonitor,
setMeasurement,
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { GLOBAL_OBJ, isThenable, logger, timestampInSeconds, uuid4 } from '@sent
import { DEFAULT_ENVIRONMENT } from './constants';
import { DEBUG_BUILD } from './debug-build';
import type { Hub } from './hub';
import { runWithAsyncContext } from './hub';
import { getCurrentHub, getIsolationScope } from './hub';
import type { Scope } from './scope';
import { closeSession, makeSession, updateSession } from './session';
Expand Down Expand Up @@ -208,6 +209,15 @@ export function withScope<T>(
return getCurrentHub().withScope(rest[0]);
}

/**
* TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's still something missing 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

*/
export function withIsolationScope<T>(callback: (isolationScope: Scope) => T): T {
return runWithAsyncContext(() => {
return callback(getIsolationScope());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to sanity check: runWithAsyncContext forks the hub for the new async local storage and hence isolation and current scope, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically the answer to your question is no. runWithAsyncContext only forks the hub when there is an async context strategy set. In the browser this is not the case so the isolation scope will always be the same. This may seem odd, but as a side-effect will prevent users, who think async context keeping works in the browser, from running into race conditions.

(Side note: This is also the reason why I didn't add any forking tests in the core package, simply because we cannot make this assumption.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right sorry, it's the async context strategy that takes care of it.

});
}

/**
* Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation.
*
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
setTags,
setUser,
withScope,
withIsolationScope,
getClient,
getCurrentScope,
startSession,
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/lib/async-context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getCurrentHub, runWithAsyncContext } from '../../src';

describe('runWithAsyncContext()', () => {
it('without strategy hubs should be equal', () => {
runWithAsyncContext(() => {
const hub1 = getCurrentHub();
runWithAsyncContext(() => {
const hub2 = getCurrentHub();
expect(hub1).toBe(hub2);
});
});
});
});
40 changes: 39 additions & 1 deletion packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Attachment, Breadcrumb, Client, Event } from '@sentry/types';
import { applyScopeDataToEvent } from '../../src';
import { applyScopeDataToEvent, getCurrentScope, getIsolationScope, withIsolationScope } from '../../src';
import { Scope, getGlobalScope, setGlobalScope } from '../../src/scope';

describe('Scope', () => {
Expand Down Expand Up @@ -465,3 +465,41 @@ describe('Scope', () => {
});
});
});

describe('isolation scope', () => {
describe('withIsolationScope()', () => {
it('will pass an isolation scope without Sentry.init()', done => {
expect.assertions(1);
withIsolationScope(scope => {
expect(scope).toBeDefined();
done();
});
});

it('will make the passed isolation scope the active isolation scope within the callback', done => {
expect.assertions(1);
withIsolationScope(scope => {
expect(getIsolationScope()).toBe(scope);
done();
});
});

it('will pass an isolation scope that is different from the current active scope', done => {
expect.assertions(1);
withIsolationScope(scope => {
expect(getCurrentScope()).not.toBe(scope);
done();
});
});

it('will always make the inner most passed scope the current scope when nesting calls', done => {
expect.assertions(1);
withIsolationScope(_scope1 => {
withIsolationScope(scope2 => {
expect(getIsolationScope()).toBe(scope2);
done();
});
});
});
});
});
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
trace,
withScope,
withIsolationScope,
captureCheckIn,
withMonitor,
setMeasurement,
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
trace,
withScope,
withIsolationScope,
captureCheckIn,
withMonitor,
setMeasurement,
Expand Down
Loading