Title: Expose API to generate active root spans in Browser SDKs · Issue #13495 · getsentry/sentry-javascript · GitHub
Open Graph Title: Expose API to generate active root spans in Browser SDKs · Issue #13495 · getsentry/sentry-javascript
X Title: Expose API to generate active root spans in Browser SDKs · Issue #13495 · getsentry/sentry-javascript
Description: Description Today, we have isomorphic APIs to start spans, which can be active or inactive. Active spans can only be made active in a callback. This makes sense and works well for Node, but in the browser it can be tricky and really does...
Open Graph Description: Description Today, we have isomorphic APIs to start spans, which can be active or inactive. Active spans can only be made active in a callback. This makes sense and works well for Node, but in the ...
X Description: Description Today, we have isomorphic APIs to start spans, which can be active or inactive. Active spans can only be made active in a callback. This makes sense and works well for Node, but in the ...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/13495
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Expose API to generate active root spans in Browser SDKs","articleBody":"### Description\n\nToday, we have isomorphic APIs to start spans, which can be active or inactive.\n\nActive spans can only be made active in a callback. This makes sense and works well for Node, but in the browser it can be tricky and really does not work well in many cases. For the most common use case, we automatically generate active root spans for pageloads and navigations, which are idle spans. However, outside of these, it is not really possible to start generically active root spans in the Browser, that encapsulate anything that happens while the root span is active. Think this:\n\n```js\n// navigation is over, I want to encompass a certain user flow with a transaction\nfunction startObserving() {\n // start root span...\n}\n\nfunction endObserving() {\n // end root span...\n}\n\nonUserClickedButtonA(startObserving);\nonUserClickedButtonB(endObserving);\n```\n\n(for example see https://github.com/getsentry/sentry-javascript/issues/13470)\n\nIn the browser, it is usually not possible to wrap such things in a callback - and, additionally, the isolation does not work anyhow at all in the browser, so it wouldn't _really_ help anyhow.\n\nWe should offer a way to do such things in the browser. There are a few options here:\n\n1. We could expose `startIdleSpan` from the SDKs. Today, this is only exported from `@sentry/core`, but not from e.g. `@sentry/browser`. This is a somewhat stable API that also makes the resulting span active. However, it has a bunch of baggage around it, and may or may not be what users actually want.\n2. We could expose the currently private/unexported `_setSpanOnScope(span, scope)` API, or something like it. However, this will only work in browser, not in Node. It may be confusing for users, or may not be.\n3. Add a new (?) API to browser only to start an active span outside of a callback. E.g. `startActiveDetachedSpan` (naming is hard...). This again could not work on Node, we'd need to think about if this should simply not exist in Node, or be a noop (or start an inactive span there...)\n4. Add an option (maybe just in browser?) to `startInactiveSpan` to make it active. This will obviously be a confusing naming 😅 `startInactiveSpan({ forceActive: true })` lol.\n5. Users _can_ somehow do this manually, kind of, by passing an async function that is ended on-demand. e.g.:\n\n```js\nlet resolve;\nSentry.startSpan({ name: 'my span' }, async () => {\n await new Promise((_resolve) => {\n resolve = _resolve;\n });\n});\n\n// When done, call` resolve()` to end the span (and stop making it active).\n```\n\nHowever this is _very_ intimate in the sense of that this relies on the fact that we don't _actually_ keep the active scope isolated per callback, plus requires some mental gymnastics from users to understand why this is even working. But it _is_ possible.\n\nSo TLDR the options I see are:\n\n1. Export `startIdleSpan` - this is either not exported from node, or starts an inactive span there\n2. Export e.g. `setSpanForScope(span, getCurrentScope())` API - this either is not exported from node or noops there\n3. Export e.g. `const span = startActiveDetachedSpan()` API - this either is not exported from node or starts an inactive span there\n4. Add option like `forceActive: true` to existing `startInactiveSpan()` API - this could noop in Node (?)\n5. Do nothing, forcing users to somehow hack this behavior themselves","author":{"url":"https://github.com/mydea","@type":"Person","name":"mydea"},"datePublished":"2024-08-28T09:46:57Z","url":"https://github.com/getsentry/sentry-javascript/issues/13495"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:18ca1999-c847-e221-86b5-0c235be47530 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D6C2:1A88D4:9A671C:C70E5F:6B051196 |
| html-safe-nonce | 094a1c2daa62f3ad6859cf2601e5434a861a9df3bdd67cda19994cf95a32ea5b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENkMyOjFBODhENDo5QTY3MUM6QzcwRTVGOjZCMDUxMTk2IiwidmlzaXRvcl9pZCI6IjQ5NTY5NDA3NDE1ODgwOTUzODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ef9e6e7b063ef33f03a7293651f1afc46a11c44a951279c91e40f205514a4dd2 |
| hovercard-subject-tag | issue:2491600375 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/13495/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fc305b2a3d88349cbd46fc0f3cb4fb12483fad87ab42a6d914d6b6051f407840/getsentry/sentry-javascript/issues/13495 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fc305b2a3d88349cbd46fc0f3cb4fb12483fad87ab42a6d914d6b6051f407840/getsentry/sentry-javascript/issues/13495 |
| og:image:alt | Description Today, we have isomorphic APIs to start spans, which can be active or inactive. Active spans can only be made active in a callback. This makes sense and works well for Node, but in the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mydea |
| hostname | github.com |
| expected-hostname | github.com |
| None | 854d880890b5e847b3fce9d1b31f032107baeddeebad2aa379c48aa4f1451f6b |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 220e980097d94ce616d5d11fbd5316d208e3fc93 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width