Skip to content

Introduce Async Hooks into the SDK #7691

Closed
@AbhiPrasad

Description

@AbhiPrasad

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:

  1. There must be only one hub management strategy active at a time (no exceptions)
  2. 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)
}
  1. 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.
  2. 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.).

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions