Skip to content

Commit c5bf6ba

Browse files
committed
feat(js): Add documentation for setSpanActive(span: Span)
1 parent 92a176a commit c5bf6ba

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

docs/platforms/javascript/common/apis.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,51 @@ See <PlatformLink to="/tracing/instrumentation/">Tracing Instrumentation</Platfo
636636

637637
</SdkApi>
638638

639+
<SdkApi
640+
name="setSpanActive"
641+
signature="function setSpanActive(span: Span): void"
642+
availableSince="10.14.0"
643+
categorySupported={["browser"]}
644+
parameters={[
645+
{
646+
name: "span",
647+
required: true,
648+
type: "Span",
649+
description: "The span to be set active"
650+
}
651+
]}
652+
>
653+
Sets the passed span as the active span on the current scope.
654+
You most likely don't need this functionality but should [use `startActiveSpan`](#startActiveSpan)
655+
instead.
656+
However, in some situations, you might prefer a span being active outside of a callback.
657+
In this case, the combination of [`startInactiveSpan`](#startInactiveSpan) with this function
658+
allows you to start a span, hold a reference to it and keep it active until you end it, without
659+
the active duration being bound to the callback (as opposed to [`startSpanManual`](#startSpanManual)).
660+
661+
662+
<Expandable title='Examples'>
663+
```javascript
664+
let checkoutSpan;
665+
666+
on('startCheckout', () => {
667+
checkoutSpan = Sentry.startInactiveSpan({name: 'checkout-flow'});
668+
Sentry.setSpanActive(checkoutSpan);
669+
})
670+
671+
doSomeWork();
672+
673+
on('endCheckout', () => {
674+
// Ending the span automatically removes it as the active span on the scope
675+
checkoutSpan.end();
676+
})
677+
```
678+
</Expandable>
679+
680+
See <PlatformLink to="/tracing/instrumentation/">Tracing Instrumentation</PlatformLink> for more information on how to work with spans.
681+
682+
</SdkApi>
683+
639684
<SdkApi
640685
name="continueTrace"
641686
signature="function continueTrace<T>(options: TraceOptions, callback: () => T): T"

docs/platforms/javascript/common/tracing/instrumentation/index.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,33 @@ Sometimes, you do not want the span to be ended automatically when the callback
102102

103103
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.
104104

105+
105106
<PlatformContent includePath="performance/start-inactive-span" />
106107

108+
<PlatformCategorySection supported={['browser']}>
109+
110+
In some cases, you may need to start an inactive span and make it the active span.
111+
In contrast to [startSpanManual](#starting-an-active-span-with-manual-end-startspanmanual), the period for which
112+
the span is active is not bound to the callback. Instead, it will remain active until you manually end the span.
113+
114+
```javascript
115+
let checkoutSpan;
116+
117+
on('startCheckout', () => {
118+
checkoutSpan = Sentry.startInactiveSpan({name: 'checkout-flow'});
119+
Sentry.setSpanActive(checkoutSpan);
120+
})
121+
122+
doSomeWork();
123+
124+
on('endCheckout', () => {
125+
// Ending the span automatically removes it as the active span on the scope
126+
checkoutSpan.end();
127+
})
128+
```
129+
130+
</PlatformCategorySection>
131+
107132
## Starting Spans as Children of a Specific Span
108133

109134
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:

0 commit comments

Comments
 (0)