Closed
Description
ref: #3660
Let's work toward adding async hooks into the SDK. What we probably want to use is AsyncLocalStorage
, which was made stable in v16.4.0
, but added in v13.10.0, v12.17.0
Important to note that for Node 8, 10, 12 we should still use domains, and then for Node 14 and above we can use AsyncLocalStorage
.
- feat(core): Add async context abstraction #7753
- feat(node): Adds
domain
implementation ofAsyncContextStrategy
#7767 - feat(node): Migrate to domains used through
AsyncContextStrategy
#7779 - fix(node): Fix domain scope inheritance #7799
- feat(node): Add
AsyncLocalStorage
implementation ofAsyncContextStrategy
#7800 - feat(node): Auto-select best
AsyncContextStrategy
for Node.js version #7804 - Test performance differences between ASL and domains
- Expose and document methods for async context management (for manual instrumentation)
- feat(node): Add docs for async context apis sentry-docs#6641
Requirements:
- There must be only one hub management strategy active at a time (no exceptions)
- No breaking changes
Additional Details:
We can get this done with the following steps.
// @sentry/node
import { setAsyncContextStrategy } from '@sentry/core';
interface AsyncContextStrategy {
getCurrentHub(): Hub | undefined;
runWithAsyncContext<T>(callback: (hub: Hub) => T): T;
}
init() {
setHubStrategy(strategy)
}
- Update
getCurrentHub
to reference some global strategy for grabbing the current hub. When you initialize the SDK in Node 8-12, it uses a domain strategy. For Node 14+ it'll use async local storage strategy. - Instead of using domains directly in our code, we should define a
runWithHub
method that creates a new domain/asynclocalstorage/whatever for that code. Then we can go in and replace domain usage withrunWithHub
instead.
The global strategy can just just be registered onto the global object. This means there can only be one global strategy at a time.
We have to add this concept of a strategy to @sentry/core
, so it can also be used by non-node SDKs (think vercel edge, cloudflare workers etc.).