@sentry/browser
Version:
Official Sentry SDK for browsers
51 lines (50 loc) • 1.91 kB
JavaScript
let _sentry_core = require("@sentry/core");
// src/tracing/setActiveSpan.ts
/**
* Sets an inactive span active on the current scope.
*
* This is useful in browser applications, if you want to create a span that cannot be finished
* within its callback. Any spans started while the given span is active, will be children of the span.
*
* If there already was an active span on the scope prior to calling this function, it is replaced
* with the given span and restored after the span ended. Otherwise, the span will simply be
* removed, resulting in no active span on the scope.
*
* IMPORTANT: This function can ONLY be used in the browser! Calling this function in a server
* environment (for example in a server-side rendered component) will result in undefined behaviour
* and is not supported.
* You MUST call `span.end()` manually, otherwise the span will never be finished.
*
* @example
* ```js
* let checkoutSpan;
*
* on('checkoutStarted', () => {
* checkoutSpan = Sentry.startInactiveSpan({ name: 'checkout-flow' });
* Sentry.setActiveSpanInBrowser(checkoutSpan);
* })
*
* // during this time, any spans started will be children of `checkoutSpan`:
* Sentry.startSpan({ name: 'checkout-step-1' }, () => {
* // ... `
* })
*
* on('checkoutCompleted', () => {
* checkoutSpan?.end();
* })
* ```
*
* @param span - the span to set active
*/
function setActiveSpanInBrowser(span) {
const maybePreviousActiveSpan = (0, _sentry_core.getActiveSpan)();
if (maybePreviousActiveSpan === span) return;
const scope = (0, _sentry_core.getCurrentScope)();
span.end = new Proxy(span.end, { apply(target, thisArg, args) {
(0, _sentry_core._INTERNAL_setSpanForScope)(scope, maybePreviousActiveSpan);
return Reflect.apply(target, thisArg, args);
} });
(0, _sentry_core._INTERNAL_setSpanForScope)(scope, span);
}
exports.setActiveSpanInBrowser = setActiveSpanInBrowser;
//# sourceMappingURL=setActiveSpan.js.map