-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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(browser): Add setBrowserErrorFrameAsyncContextStrategy
#7807
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { Carrier, Hub, RunWithAsyncContextOptions } from '@sentry/core'; | ||
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy } from '@sentry/core'; | ||
|
||
/** */ | ||
export function setBrowserErrorFrameAsyncContextStrategy(): void { | ||
let id = 0; | ||
const hubs = new Map<number, Hub>(); | ||
|
||
/** */ | ||
function getCurrentHub(): Hub | undefined { | ||
const stackId = _getHubIdFromStack(); | ||
return stackId === undefined ? undefined : hubs.get(stackId); | ||
} | ||
|
||
/** */ | ||
function createNewHub(parent: Hub | undefined): Hub { | ||
const carrier: Carrier = {}; | ||
ensureHubOnCarrier(carrier, parent); | ||
return getHubFromCarrier(carrier); | ||
} | ||
|
||
/** */ | ||
function runWithAsyncContext<T>(callback: (hub: Hub) => T, options: RunWithAsyncContextOptions): T { | ||
const existingHub = getCurrentHub(); | ||
|
||
if (existingHub && options && options.reuseExisting) { | ||
// We're already in an async context, so we don't need to create a new one | ||
// just call the callback with the current hub | ||
return callback(existingHub); | ||
} | ||
|
||
const newHub = createNewHub(existingHub); | ||
|
||
const hubId = id++; | ||
const fnName = `SENTRY_HUB_ID_${hubId}`; | ||
|
||
return { | ||
[fnName]: (cb: (hub: Hub) => T) => { | ||
hubs.set(hubId, newHub); | ||
return cb(newHub); | ||
}, | ||
}[fnName](callback); | ||
} | ||
|
||
setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext }); | ||
} | ||
|
||
function _getHubIdFromStack(): number | undefined { | ||
const e = new Error(); | ||
const key = (e.stack && e.stack.match(/(?<=SENTRY_HUB_ID_)(?:\d+)/)) || []; | ||
const value = Number.parseInt(key[0], 10); | ||
return Number.isNaN(value) ? undefined : value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { getCurrentHub, Hub, runWithAsyncContext, setAsyncContextStrategy } from '@sentry/core'; | ||
|
||
import { setBrowserErrorFrameAsyncContextStrategy } from '../../src/async'; | ||
|
||
describe('async browser context', () => { | ||
afterAll(() => { | ||
// clear the strategy | ||
setAsyncContextStrategy(undefined); | ||
}); | ||
|
||
test('without context', () => { | ||
const hub = getCurrentHub(); | ||
expect(hub).toEqual(new Hub()); | ||
}); | ||
|
||
test('without strategy hubs should be equal', () => { | ||
runWithAsyncContext(hub1 => { | ||
runWithAsyncContext(hub2 => { | ||
expect(hub1).toBe(hub2); | ||
}); | ||
}); | ||
}); | ||
|
||
test('hub scope inheritance', () => { | ||
setBrowserErrorFrameAsyncContextStrategy(); | ||
|
||
const globalHub = getCurrentHub(); | ||
globalHub.setExtra('a', 'b'); | ||
|
||
runWithAsyncContext(hub1 => { | ||
expect(hub1).toEqual(globalHub); | ||
|
||
hub1.setExtra('c', 'd'); | ||
expect(hub1).not.toEqual(globalHub); | ||
|
||
runWithAsyncContext(hub2 => { | ||
expect(hub2).toEqual(hub1); | ||
expect(hub2).not.toEqual(globalHub); | ||
|
||
hub2.setExtra('e', 'f'); | ||
expect(hub2).not.toEqual(hub1); | ||
}); | ||
}); | ||
}); | ||
|
||
test('hub scope getCurrentHub', () => { | ||
setBrowserErrorFrameAsyncContextStrategy(); | ||
|
||
const globalHub = getCurrentHub(); | ||
globalHub.setExtra('a', 'b'); | ||
|
||
runWithAsyncContext(hub1 => { | ||
expect(getCurrentHub()).toBe(hub1); | ||
runWithAsyncContext(hub2 => { | ||
expect(getCurrentHub()).toBe(hub2); | ||
runWithAsyncContext(hub3 => { | ||
expect(getCurrentHub()).toBe(hub3); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
test('context single instance', () => { | ||
setBrowserErrorFrameAsyncContextStrategy(); | ||
|
||
runWithAsyncContext(hub => { | ||
expect(hub).toBe(getCurrentHub()); | ||
}); | ||
}); | ||
|
||
test('context within a context not reused', () => { | ||
setBrowserErrorFrameAsyncContextStrategy(); | ||
|
||
runWithAsyncContext(hub1 => { | ||
runWithAsyncContext(hub2 => { | ||
expect(hub1).not.toBe(hub2); | ||
}); | ||
}); | ||
}); | ||
|
||
test('context within a context reused when requested', () => { | ||
setBrowserErrorFrameAsyncContextStrategy(); | ||
|
||
runWithAsyncContext(hub1 => { | ||
runWithAsyncContext( | ||
hub2 => { | ||
expect(hub1).toBe(hub2); | ||
}, | ||
{ reuseExisting: true }, | ||
); | ||
}); | ||
}); | ||
|
||
test('concurrent hub contexts', done => { | ||
setBrowserErrorFrameAsyncContextStrategy(); | ||
|
||
let d1done = false; | ||
let d2done = false; | ||
|
||
runWithAsyncContext(hub => { | ||
hub.getStack().push({ client: 'process' } as any); | ||
expect(hub.getStack()[1]).toEqual({ client: 'process' }); | ||
// Just in case so we don't have to worry which one finishes first | ||
// (although it always should be d2) | ||
setTimeout(() => { | ||
d1done = true; | ||
if (d2done) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
|
||
runWithAsyncContext(hub => { | ||
hub.getStack().push({ client: 'local' } as any); | ||
expect(hub.getStack()[1]).toEqual({ client: 'local' }); | ||
setTimeout(() => { | ||
d2done = true; | ||
if (d1done) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hubs
will grow in size indefinitely. You need to remove the entry after the callback is called and also in the case of error.