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.
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 with runWithHub 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.).
ref: #3660
Let's work toward adding async hooks into the SDK. What we probably want to use is
AsyncLocalStorage, which was made stable inv16.4.0, but added inv13.10.0, v12.17.0Important to note that for Node 8, 10, 12 we should still use domains, and then for Node 14 and above we can use
AsyncLocalStorage.domainimplementation ofAsyncContextStrategy#7767AsyncContextStrategy#7779AsyncLocalStorageimplementation ofAsyncContextStrategy#7800AsyncContextStrategyfor Node.js version #7804Requirements:
Additional Details:
We can get this done with the following steps.
getCurrentHubto 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.runWithHubmethod that creates a new domain/asynclocalstorage/whatever for that code. Then we can go in and replace domain usage withrunWithHubinstead.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.).