Skip to content
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
37 changes: 37 additions & 0 deletions docs/platforms/javascript/common/apis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,43 @@ See <PlatformLink to="/tracing/instrumentation/">Tracing Instrumentation</Platfo

</SdkApi>

<SdkApi
name="setActiveSpanInBrowser"
signature="function setActiveSpanInBrowser(span: Span): void"
availableSince="10.15.0"
categorySupported={["browser"]}
>
Sets the passed span as the active span on the current scope.
You most likely don't need this functionality and should [use `startSpan`](#startSpan)
instead.
However, in some situations, you might prefer a span being active outside of a callback.
In this case, the combination of [`startInactiveSpan`](#startInactiveSpan) with this function
allows you to start a span, hold a reference to it and keep it active until you end it, without
the active duration being bound to the callback (as opposed to [`startSpanManual`](#startSpanManual)).


<Expandable title='Examples'>
```javascript
let checkoutSpan;

on('startCheckout', () => {
checkoutSpan = Sentry.startInactiveSpan({name: 'checkout-flow'});
Sentry.setActiveSpanInBrowser(checkoutSpan);
})

doSomeWork();

on('endCheckout', () => {
// Ending the span automatically removes it as the active span on the scope
checkoutSpan.end();
})
```
</Expandable>

See <PlatformLink to="/tracing/instrumentation/">Tracing Instrumentation</PlatformLink> for more information on how to work with spans.

</SdkApi>

<SdkApi
name="continueTrace"
signature="function continueTrace<T>(options: TraceOptions, callback: () => T): T"
Expand Down
38 changes: 38 additions & 0 deletions docs/platforms/javascript/common/tracing/instrumentation/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,46 @@ Sometimes, you do not want the span to be ended automatically when the callback

To add spans that aren't active, you can create independent spans. This is useful when you have work that is grouped together under a single parent span, but is independent from the currently active span. However, in most cases you'll want to create and use the [startSpan](#starting-an-active-span-startspan) API from above.


<PlatformContent includePath="performance/start-inactive-span" />

<PlatformCategorySection supported={['browser']}>

### Setting an inactive span active (browser only)

_Available Since: `10.15.0`_

In browser environments, you might run into use cases, where the callback-based span APIs are not sufficient.
In such cases (see example below), you can use `startInactiveSpan` to start an initially inactive span and then
set it active until you manually end the span.

```javascript
let checkoutSpan;

on('startCheckout', () => {
checkoutSpan = Sentry.startInactiveSpan({name: 'checkout-flow'});
Sentry.setActiveSpanInBrowser(checkoutSpan);
})

doSomeWork();

on('endCheckout', () => {
// Ending the span automatically removes it as the active span
checkoutSpan.end();
})
```

Using `startInactiveSpan` in combination with `setActiveSpanInBrowser` allows you to create an inactive span that can be made active until you manually end it. In contrast, [`startSpanManual`](#starting-an-active-span-with-manual-end-startspanmanual) allows you to end the span manually at any point, but it only remains active within the callback.

<Alert>

Note that `setActiveSpanInBrowser` is only available in browser environments! If you're running code in the server
(for example for server-side rendering), make sure to guard this call to only run in the browser.

</Alert>

</PlatformCategorySection>

## Starting Spans as Children of a Specific Span

By default, any span that is started will be the child of the currently active span. If you want to have a different behavior, you can force spans to be the children of a specific span with the `parentSpan` option:
Expand Down
Loading