@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
99 lines (98 loc) • 3.43 kB
JavaScript
import { getAsyncContextStrategy } from "./asyncContext/index.js";
import { getGlobalSingleton, getMainCarrier, getSentryCarrier } from "./carrier.js";
import { Scope } from "./scope.js";
import { generateSpanId } from "./utils/propagationContext.js";
// src/currentScopes.ts
/**
* Register an external propagation context provider function.
* When registered, trace context will be read from the external source (e.g. OpenTelemetry)
* instead of from the Sentry scope's propagation context.
*/
function registerExternalPropagationContext(fn) {
getSentryCarrier(getMainCarrier()).externalPropagationContextProvider = fn;
}
/**
* Get the external propagation context, if a provider has been registered.
*/
function getExternalPropagationContext() {
return getSentryCarrier(getMainCarrier()).externalPropagationContextProvider?.();
}
/**
* Check if an external propagation context provider has been registered.
*/
function hasExternalPropagationContext() {
return getSentryCarrier(getMainCarrier()).externalPropagationContextProvider !== void 0;
}
/**
* Get the currently active scope.
*/
function getCurrentScope() {
const carrier = getMainCarrier();
return getAsyncContextStrategy(carrier).getCurrentScope();
}
/**
* Get the currently active isolation scope.
* The isolation scope is active for the current execution context.
*/
function getIsolationScope() {
const carrier = getMainCarrier();
return getAsyncContextStrategy(carrier).getIsolationScope();
}
/**
* Get the global scope.
* This scope is applied to _all_ events.
*/
function getGlobalScope() {
return getGlobalSingleton("globalScope", () => new Scope());
}
/**
* Either creates a new active scope, or sets the given scope as active scope in the given callback.
*/
function withScope(...rest) {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
if (rest.length === 2) {
const [scope, callback] = rest;
if (!scope) return acs.withScope(callback);
return acs.withSetScope(scope, callback);
}
return acs.withScope(rest[0]);
}
/**
* Either creates a new active isolation scope, or sets the given isolation scope as active scope in the given callback.
*/
function withIsolationScope(...rest) {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
if (rest.length === 2) {
const [isolationScope, callback] = rest;
if (!isolationScope) return acs.withIsolationScope(callback);
return acs.withSetIsolationScope(isolationScope, callback);
}
return acs.withIsolationScope(rest[0]);
}
/**
* Get the currently active client.
*/
function getClient() {
return getCurrentScope().getClient();
}
/**
* Get a trace context for the given scope.
*/
function getTraceContextFromScope(scope) {
const externalContext = getExternalPropagationContext();
if (externalContext) return {
trace_id: externalContext.traceId,
span_id: externalContext.spanId
};
const { traceId, parentSpanId, propagationSpanId } = scope.getPropagationContext();
const traceContext = {
trace_id: traceId,
span_id: propagationSpanId || generateSpanId()
};
if (parentSpanId) traceContext.parent_span_id = parentSpanId;
return traceContext;
}
export { getClient, getCurrentScope, getExternalPropagationContext, getGlobalScope, getIsolationScope, getTraceContextFromScope, hasExternalPropagationContext, registerExternalPropagationContext, withIsolationScope, withScope };
//# sourceMappingURL=currentScopes.js.map