Skip to content

Commit 06a9927

Browse files
Lms24sfanahata
andauthored
feat(js): Add documentation for setActiveSpanInBrowser(span: Span) (#15007)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR Adds documentation for `Sentry. setActiveSpanInBrowser(span)` which is added via getsentry/sentry-javascript#17714. To be merged only after the feature was released. This is still based on #15006 because I need the `availableSince` prop on the `SdkApi` component. closes https://linear.app/getsentry/issue/FE-608/docs-for-setspanactive ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: Shannon Anahata <shannon.anahata@gmail.com>
1 parent 7b83d92 commit 06a9927

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

docs/platforms/javascript/common/apis.mdx

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

637637
</SdkApi>
638638

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

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,46 @@ 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+
### Setting an inactive span active (browser only)
111+
112+
_Available Since: `10.15.0`_
113+
114+
In browser environments, you might run into use cases, where the callback-based span APIs are not sufficient.
115+
In such cases (see example below), you can use `startInactiveSpan` to start an initially inactive span and then
116+
set it active until you manually end the span.
117+
118+
```javascript
119+
let checkoutSpan;
120+
121+
on('startCheckout', () => {
122+
checkoutSpan = Sentry.startInactiveSpan({name: 'checkout-flow'});
123+
Sentry.setActiveSpanInBrowser(checkoutSpan);
124+
})
125+
126+
doSomeWork();
127+
128+
on('endCheckout', () => {
129+
// Ending the span automatically removes it as the active span
130+
checkoutSpan.end();
131+
})
132+
```
133+
134+
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.
135+
136+
<Alert>
137+
138+
Note that `setActiveSpanInBrowser` is only available in browser environments! If you're running code in the server
139+
(for example for server-side rendering), make sure to guard this call to only run in the browser.
140+
141+
</Alert>
142+
143+
</PlatformCategorySection>
144+
107145
## Starting Spans as Children of a Specific Span
108146

109147
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)