Skip to content

feat(vercel-edge): Stop using hub #11539

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

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Changes from all 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
93 changes: 45 additions & 48 deletions packages/vercel-edge/src/async.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Hub as HubClass, getGlobalHub } from '@sentry/core';
import { setAsyncContextStrategy } from '@sentry/core';
import {
getCurrentHubShim,
getDefaultCurrentScope,
getDefaultIsolationScope,
setAsyncContextStrategy,
} from '@sentry/core';
import type { Hub, Scope } from '@sentry/types';
import { GLOBAL_OBJ, logger } from '@sentry/utils';

Expand All @@ -11,7 +15,7 @@ interface AsyncLocalStorage<T> {
run<R, TArgs extends any[]>(store: T, callback: (...args: TArgs) => R, ...args: TArgs): R;
}

let asyncStorage: AsyncLocalStorage<Hub>;
let asyncStorage: AsyncLocalStorage<{ scope: Scope; isolationScope: Scope }>;

/**
* Sets the async context strategy to use AsyncLocalStorage which should be available in the edge runtime.
Expand All @@ -32,68 +36,63 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
asyncStorage = new MaybeGlobalAsyncLocalStorage();
}

function getCurrentAsyncStorageHub(): Hub | undefined {
return asyncStorage.getStore();
function getScopes(): { scope: Scope; isolationScope: Scope } {
const scopes = asyncStorage.getStore();

if (scopes) {
return scopes;
}

// fallback behavior:
// if, for whatever reason, we can't find scopes on the context here, we have to fix this somehow
return {
scope: getDefaultCurrentScope(),
isolationScope: getDefaultIsolationScope(),
};
}

function getCurrentHub(): Hub {
return getCurrentAsyncStorageHub() || getGlobalHub();
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHubShim();
return {
...hub,
getScope: () => {
const scopes = getScopes();
return scopes.scope;
},
getIsolationScope: () => {
const scopes = getScopes();
return scopes.isolationScope;
},
};
}

function withScope<T>(callback: (scope: Scope) => T): T {
const parentHub = getCurrentHub();

/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const scope = parentHub.getScope().clone();
const isolationScope = parentHub.getIsolationScope();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */

return asyncStorage.run(newHub, () => {
const scope = getScopes().scope.clone();
const isolationScope = getScopes().isolationScope;
return asyncStorage.run({ scope, isolationScope }, () => {
return callback(scope);
});
}

function withSetScope<T>(scope: Scope, callback: (scope: Scope) => T): T {
const parentHub = getCurrentHub();

/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const isolationScope = parentHub.getIsolationScope();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */

return asyncStorage.run(newHub, () => {
const isolationScope = getScopes().isolationScope.clone();
return asyncStorage.run({ scope, isolationScope }, () => {
return callback(scope);
});
}

function withIsolationScope<T>(callback: (isolationScope: Scope) => T): T {
const parentHub = getCurrentHub();

/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const scope = parentHub.getScope().clone();
const isolationScope = parentHub.getIsolationScope().clone();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */

return asyncStorage.run(newHub, () => {
const scope = getScopes().scope;
const isolationScope = getScopes().isolationScope.clone();
return asyncStorage.run({ scope, isolationScope }, () => {
return callback(isolationScope);
});
}

function withSetIsolationScope<T>(isolationScope: Scope, callback: (isolationScope: Scope) => T): T {
const parentHub = getCurrentHub();

/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const scope = parentHub.getScope().clone();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */

return asyncStorage.run(newHub, () => {
const scope = getScopes().scope;
return asyncStorage.run({ scope, isolationScope }, () => {
return callback(isolationScope);
});
}
Expand All @@ -104,9 +103,7 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
withSetScope,
withIsolationScope,
withSetIsolationScope,
// eslint-disable-next-line deprecation/deprecation
getCurrentScope: () => getCurrentHub().getScope(),
// eslint-disable-next-line deprecation/deprecation
getIsolationScope: () => getCurrentHub().getIsolationScope(),
getCurrentScope: () => getScopes().scope,
getIsolationScope: () => getScopes().isolationScope,
});
}