@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
138 lines (137 loc) • 4.05 kB
JavaScript
import { getDefaultCurrentScope, getDefaultIsolationScope } from "../defaultScopes.js";
import { Scope } from "../scope.js";
import { SUPPRESS_TRACING_KEY } from "../tracing/constants.js";
import { chainAndCopyPromiseLike } from "../utils/chain-and-copy-promiselike.js";
import { isThenable } from "../utils/is.js";
import { getMainCarrier, getSentryCarrier } from "../carrier.js";
// src/asyncContext/stackStrategy.ts
/**
* This is an object that holds a stack of scopes.
*/
var AsyncContextStack = class {
constructor(scope, isolationScope) {
let assignedScope;
if (!scope) assignedScope = new Scope();
else assignedScope = scope;
let assignedIsolationScope;
if (!isolationScope) assignedIsolationScope = new Scope();
else assignedIsolationScope = isolationScope;
this._stack = [{ scope: assignedScope }];
this._isolationScope = assignedIsolationScope;
}
/**
* Fork a scope for the stack.
*/
withScope(callback) {
const scope = this._pushScope();
let maybePromiseResult;
try {
maybePromiseResult = callback(scope);
} catch (e) {
this._popScope();
throw e;
}
if (isThenable(maybePromiseResult)) return chainAndCopyPromiseLike(maybePromiseResult, () => this._popScope(), () => this._popScope());
this._popScope();
return maybePromiseResult;
}
/**
* Get the client of the stack.
*/
getClient() {
return this.getStackTop().client;
}
/**
* Returns the scope of the top stack.
*/
getScope() {
return this.getStackTop().scope;
}
/**
* Get the isolation scope for the stack.
*/
getIsolationScope() {
return this._isolationScope;
}
/**
* Returns the topmost scope layer in the order domain > local > process.
*/
getStackTop() {
return this._stack[this._stack.length - 1];
}
/**
* Push a scope to the stack.
*/
_pushScope() {
const scope = this.getScope().clone();
this._stack.push({
client: this.getClient(),
scope
});
return scope;
}
/**
* Pop a scope from the stack.
*/
_popScope() {
if (this._stack.length <= 1) return false;
return !!this._stack.pop();
}
};
/**
* Get the global async context stack.
* This will be removed during the v8 cycle and is only here to make migration easier.
*/
function getAsyncContextStack() {
const registry = getMainCarrier();
const sentry = getSentryCarrier(registry);
return sentry.stack = sentry.stack || new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope());
}
function withScope(callback) {
return getAsyncContextStack().withScope(callback);
}
function withSetScope(scope, callback) {
const stack = getAsyncContextStack();
return stack.withScope(() => {
stack.getStackTop().scope = scope;
return callback(scope);
});
}
function withIsolationScope(callback) {
return getAsyncContextStack().withScope(() => {
return callback(getAsyncContextStack().getIsolationScope());
});
}
/**
* Get the stack-based async context strategy.
*/
function getStackAsyncContextStrategy() {
return {
suppressTracing: suppressTracingInStack,
withIsolationScope,
withScope,
withSetScope,
withSetIsolationScope: (_isolationScope, callback) => {
return withIsolationScope(callback);
},
getCurrentScope: () => getAsyncContextStack().getScope(),
getIsolationScope: () => getAsyncContextStack().getIsolationScope()
};
}
/**
* In stack-based ACS, we do not wait for the callback to finish before we reset the metadata
* the reason for this is that otherwise, in the stack this can lead to very weird behavior
* as there is only a single top scope, if the callback takes longer to finish,
* other, unrelated spans may also be suppressed, which we do not want
* so instead, we only suppress tracing synchronoysly in the stack.
*/
function suppressTracingInStack(callback) {
return withScope((scope) => {
scope.setSDKProcessingMetadata({ [SUPPRESS_TRACING_KEY]: true });
const res = callback();
scope.setSDKProcessingMetadata({ [SUPPRESS_TRACING_KEY]: void 0 });
return res;
});
}
export { AsyncContextStack, getStackAsyncContextStrategy };
//# sourceMappingURL=stackStrategy.js.map