Skip to content
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

feat(core): Extend AsyncContextStrategy to allow reuse of existing context #7778

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 18 additions & 4 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ const DEFAULT_BREADCRUMBS = 100;
* Strategy used to track async context.
*/
export interface AsyncContextStrategy {
/**
* Gets the current async context. Returns undefined if there is no current async context.
*/
getCurrentHub: () => Hub | undefined;
runWithAsyncContext<T>(callback: (hub: Hub) => T, ...args: unknown[]): T;
/**
* Runs the supplied callback in its own async context.
* @param reuseExisting Whether to reuse an existing async context if one exists.
* @param args Instances that should be referenced and retained in the new context.
*/
runWithAsyncContext<T>(callback: (hub: Hub) => T, reuseExisting: boolean, ...args: unknown[]): T;
}

/**
Expand Down Expand Up @@ -583,13 +591,19 @@ export function setAsyncContextStrategy(strategy: AsyncContextStrategy | undefin
/**
* @private Private API with no semver guarantees!
*
* Runs the given callback function with the global async context strategy
* Runs the supplied callback in its own async context.
* @param reuseExisting Whether to reuse an existing async context if one exists. Defaults to false.
* @param args Instances that should be referenced and retained in the new context.
*/
export function runWithAsyncContext<T>(callback: (hub: Hub) => T, ...args: unknown[]): T {
export function runWithAsyncContext<T>(
callback: (hub: Hub) => T,
reuseExisting: boolean = false,
...args: unknown[]
): T {
const registry = getMainCarrier();

if (registry.__SENTRY__ && registry.__SENTRY__.acs) {
return registry.__SENTRY__.acs.runWithAsyncContext(callback, ...args);
return registry.__SENTRY__.acs.runWithAsyncContext(callback, reuseExisting, ...args);
}

// if there was no strategy, fallback to just calling the callback
Expand Down
12 changes: 8 additions & 4 deletions packages/node/src/async/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import {
import * as domain from 'domain';
import { EventEmitter } from 'events';

function getCurrentHub(): Hub | undefined {
function getActiveDomain<T>(): T | undefined {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
const activeDomain = (domain as any).active as Carrier;
return (domain as any).active as T | undefined;
}

function getCurrentHub(): Hub | undefined {
const activeDomain = getActiveDomain<Carrier>();

// If there's no active domain, just return undefined and the global hub will be used
if (!activeDomain) {
Expand All @@ -22,8 +26,8 @@ function getCurrentHub(): Hub | undefined {
return getHubFromCarrier(activeDomain);
}

function runWithAsyncContext<T, A>(callback: (hub: Hub) => T, ...args: A[]): T {
const local = domain.create();
function runWithAsyncContext<T, A>(callback: (hub: Hub) => T, reuseExisting: boolean, ...args: A[]): T {
const local = reuseExisting ? getActiveDomain<domain.Domain>() || domain.create() : domain.create();

for (const emitter of args) {
if (emitter instanceof EventEmitter) {
Expand Down