Skip to content

ref: Remove reuseExisting option for ACS #10645

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 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 1 addition & 6 deletions packages/core/src/asyncContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { Hub, Integration } from '@sentry/types';
import { GLOBAL_OBJ } from '@sentry/utils';

export interface RunWithAsyncContextOptions {
/** Whether to reuse an existing async context if one exists. Defaults to false. */
reuseExisting?: boolean;
}

/**
* @private Private API with no semver guarantees!
*
Expand All @@ -20,7 +15,7 @@ export interface AsyncContextStrategy {
/**
* Runs the supplied callback in its own async context.
*/
runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions): T;
runWithAsyncContext<T>(callback: () => T): T;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from '@sentry/types';
import { GLOBAL_OBJ, consoleSandbox, dateTimestampInSeconds, isThenable, logger, uuid4 } from '@sentry/utils';

import type { AsyncContextStrategy, Carrier, RunWithAsyncContextOptions } from './asyncContext';
import type { AsyncContextStrategy, Carrier } from './asyncContext';
import { getMainCarrier, getSentryCarrier } from './asyncContext';
import { DEFAULT_ENVIRONMENT } from './constants';
import { DEBUG_BUILD } from './debug-build';
Expand Down Expand Up @@ -661,12 +661,12 @@ export function getCurrentHub(): HubInterface {
* @param options Options to pass to the async context strategy
* @returns The result of the callback
*/
export function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions = {}): T {
export function runWithAsyncContext<T>(callback: () => T): T {
// Get main carrier (global for every environment)
const carrier = getMainCarrier();

const acs = getAsyncContextStrategy(carrier);
return acs.runWithAsyncContext(callback, options);
return acs.runWithAsyncContext(callback);
}

function getGlobalHub(): HubInterface {
Expand Down Expand Up @@ -752,7 +752,7 @@ export function getAsyncContextStrategy(carrier: Carrier): AsyncContextStrategy
function getHubStackAsyncContextStrategy(): AsyncContextStrategy {
return {
getCurrentHub: getGlobalHub,
runWithAsyncContext: <T>(callback: () => T, _options: RunWithAsyncContextOptions = {}): T => {
runWithAsyncContext: <T>(callback: () => T): T => {
return callback();
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { ClientClass } from './sdk';
export type { Layer } from './hub';
export type { AsyncContextStrategy, Carrier, RunWithAsyncContextOptions } from './asyncContext';
export type { AsyncContextStrategy, Carrier } from './asyncContext';
export type { OfflineStore, OfflineTransportOptions } from './transports/offline';
export type { ServerRuntimeClientOptions } from './server-runtime-client';
export type { RequestDataIntegrationOptions } from './integrations/requestdata';
Expand Down
9 changes: 2 additions & 7 deletions packages/node/src/async/domain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as domain from 'domain';
import type { Carrier, RunWithAsyncContextOptions } from '@sentry/core';
import type { Carrier } from '@sentry/core';
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy, setHubOnCarrier } from '@sentry/core';
import type { Hub } from '@sentry/types';

Expand Down Expand Up @@ -27,14 +27,9 @@ function createNewHub(parent: Hub | undefined): Hub {
return getHubFromCarrier(carrier);
}

function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions): T {
function runWithAsyncContext<T>(callback: () => T): T {
const activeDomain = getActiveDomain<domain.Domain & Carrier>();

if (activeDomain && options?.reuseExisting) {
// We're already in a domain, so we don't need to create a new one, just call the callback with the current hub
return callback();
}

const local = domain.create() as domain.Domain & Carrier;

const parentHub = activeDomain ? getHubFromCarrier(activeDomain) : undefined;
Expand Down
10 changes: 2 additions & 8 deletions packages/node/src/async/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Carrier, RunWithAsyncContextOptions } from '@sentry/core';
import type { Carrier } from '@sentry/core';
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy } from '@sentry/core';
import type { Hub } from '@sentry/types';
import * as async_hooks from 'async_hooks';
Expand Down Expand Up @@ -33,15 +33,9 @@ export function setHooksAsyncContextStrategy(): void {
return getHubFromCarrier(carrier);
}

function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions): T {
function runWithAsyncContext<T>(callback: () => T): T {
const existingHub = getCurrentHub();

if (existingHub && 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();
}

const newHub = createNewHub(existingHub);

return asyncStorage.run(newHub, () => {
Expand Down
15 changes: 0 additions & 15 deletions packages/node/test/async/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,6 @@ describe('setDomainAsyncContextStrategy()', () => {
});
});

test('within a domain reused when requested', () => {
setDomainAsyncContextStrategy();

runWithAsyncContext(() => {
const hub1 = getCurrentHub();
runWithAsyncContext(
() => {
const hub2 = getCurrentHub();
expect(hub1).toBe(hub2);
},
{ reuseExisting: true },
);
});
});

test('concurrent hub contexts', done => {
setDomainAsyncContextStrategy();

Expand Down
15 changes: 0 additions & 15 deletions packages/node/test/async/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,6 @@ describe('setHooksAsyncContextStrategy()', () => {
});
});

test('context within a context reused when requested', () => {
setHooksAsyncContextStrategy();

runWithAsyncContext(() => {
const hub1 = getCurrentHub();
runWithAsyncContext(
() => {
const hub2 = getCurrentHub();
expect(hub1).toBe(hub2);
},
{ reuseExisting: true },
);
});
});

test('concurrent hub contexts', done => {
setHooksAsyncContextStrategy();

Expand Down
12 changes: 2 additions & 10 deletions packages/opentelemetry/src/asyncContextStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as api from '@opentelemetry/api';
import type { Hub, RunWithAsyncContextOptions } from '@sentry/core';
import type { Hub } from '@sentry/core';
import { setAsyncContextStrategy } from '@sentry/core';
import { SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY } from './constants';

Expand All @@ -19,15 +19,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
}

/* This is more or less a NOOP - we rely on the OTEL context manager for this */
function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions): T {
const existingHub = getCurrentHub();

if (existingHub && 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();
}

function runWithAsyncContext<T>(callback: () => T): T {
const ctx = api.context.active();

// We depend on the otelContextManager to handle the context/hub
Expand Down
15 changes: 0 additions & 15 deletions packages/opentelemetry/test/asyncContextStrategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,6 @@ describe('asyncContextStrategy', () => {
});
});

test('context within a context reused when requested', () => {
runWithAsyncContext(() => {
// eslint-disable-next-line deprecation/deprecation
const hub1 = getCurrentHub();
runWithAsyncContext(
() => {
// eslint-disable-next-line deprecation/deprecation
const hub2 = getCurrentHub();
expect(hub1).toBe(hub2);
},
{ reuseExisting: true },
);
});
});

test('concurrent hub contexts', done => {
let d1done = false;
let d2done = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/serverless/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { addExceptionMechanism } from '@sentry/utils';
* @returns function which runs in the newly created domain or in the existing one
*/
export function domainify<A extends unknown[], R>(fn: (...args: A) => R): (...args: A) => R | void {
return (...args) => runWithAsyncContext(() => fn(...args), { reuseExisting: true });
return (...args) => runWithAsyncContext(() => fn(...args));
}

/**
Expand Down
10 changes: 2 additions & 8 deletions packages/vercel-edge/src/async.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Carrier, RunWithAsyncContextOptions } from '@sentry/core';
import type { Carrier } from '@sentry/core';
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy } from '@sentry/core';
import type { Hub } from '@sentry/types';
import { GLOBAL_OBJ, logger } from '@sentry/utils';
Expand Down Expand Up @@ -42,15 +42,9 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
return getHubFromCarrier(carrier);
}

function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions): T {
function runWithAsyncContext<T>(callback: () => T): T {
const existingHub = getCurrentHub();

if (existingHub && 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();
}

const newHub = createNewHub(existingHub);

return asyncStorage.run(newHub, () => {
Expand Down