-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(core): Export browser-specific span start APIs #23361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type { Client } from '../client'; | ||
| import { getClient } from '../currentScopes'; | ||
| import { spanStreamingIntegration } from '../integrations/browserSpanStreaming'; | ||
| import type { Span } from '../types/span'; | ||
| import type { StartSpanOptions } from '../types/startSpanOptions'; | ||
| import { startIdleSpan as coreStartIdleSpan } from './idleSpan'; | ||
| import { hasSpanStreamingEnabled } from './spans/hasSpanStreamingEnabled'; | ||
| import { | ||
| startInactiveSpan as coreStartInactiveSpan, | ||
| startSpan as coreStartSpan, | ||
| startSpanManual as coreStartSpanManual, | ||
| } from './trace'; | ||
|
|
||
| /** | ||
| * Browser variants of the span-start APIs. | ||
| * | ||
| * These exist purely so that the span streaming integration is only reachable from code that can | ||
| * actually start a span. `@sentry/browser`'s `init()` deliberately doesn't reference spanStreamingIntegration, | ||
| * so error-only apps tree-shake the entire span streaming graph away without needing | ||
| * the `__SENTRY_TRACING__` flag. | ||
| */ | ||
|
|
||
| const clientsWithIntegration = new WeakSet<Client>(); | ||
|
|
||
| /** | ||
| * Lazily install the browser span streaming integration. | ||
| * | ||
| * Defaults to the current client; pass one explicitly from integration hooks, where the client being | ||
| * set up isn't necessarily the current one. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function _INTERNAL_ensureBrowserSpanStreaming(client: Client | undefined = getClient()): void { | ||
| // The `WeakSet` is an allocation optimization, not a semantic gate — `addIntegration()` is already | ||
| // idempotent by integration name, including against a user-supplied instance. | ||
| if (!client || clientsWithIntegration.has(client) || !hasSpanStreamingEnabled(client)) { | ||
| return; | ||
| } | ||
|
|
||
| clientsWithIntegration.add(client); | ||
| client.addIntegration(spanStreamingIntegration()); | ||
| } | ||
|
|
||
| /** | ||
| * Wraps a function with a span and finishes the span after the function is done. | ||
| * | ||
| * See {@link startSpan} in `@sentry/core` for details. | ||
| */ | ||
| export function startSpan<T>(options: StartSpanOptions, callback: (span: Span) => T): T { | ||
| _INTERNAL_ensureBrowserSpanStreaming(); | ||
| return coreStartSpan(options, callback); | ||
| } | ||
|
|
||
| /** | ||
| * Similar to `startSpan`, but forces the span to be ended manually. | ||
| * | ||
| * See {@link startSpanManual} in `@sentry/core` for details. | ||
| */ | ||
| export function startSpanManual<T>(options: StartSpanOptions, callback: (span: Span, finish: () => void) => T): T { | ||
| _INTERNAL_ensureBrowserSpanStreaming(); | ||
| return coreStartSpanManual(options, callback); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a span that is not set as active. | ||
| * | ||
| * See {@link startInactiveSpan} in `@sentry/core` for details. | ||
| */ | ||
| export function startInactiveSpan(options: StartSpanOptions): Span { | ||
| _INTERNAL_ensureBrowserSpanStreaming(); | ||
| return coreStartInactiveSpan(options); | ||
| } | ||
|
|
||
| /** | ||
| * Starts an idle span that automatically ends once no activity happens for a while. | ||
| * | ||
| * See {@link startIdleSpan} in `@sentry/core` for details. | ||
| * | ||
| * Typed via `typeof` because `IdleSpanOptions` is intentionally not part of the public type surface, | ||
| * and re-declaring the signature here would have to widen it. | ||
| */ | ||
| export const startIdleSpan: typeof coreStartIdleSpan = (startSpanOptions, options) => { | ||
| _INTERNAL_ensureBrowserSpanStreaming(); | ||
| return coreStartIdleSpan(startSpanOptions, options); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ export { | |
| markSpanAsTracerProviderSpan, | ||
| spanIsTracerProviderSpan, | ||
| } from './utils'; | ||
| export { startIdleSpan, TRACING_DEFAULTS } from './idleSpan'; | ||
| export { TRACING_DEFAULTS } from './idleSpan'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Browser span APIs remain ambiguous exportsHigh Severity
Reviewed by Cursor Bugbot for commit 47848cf. Configure here. |
||
| export { SentrySpan } from './sentrySpan'; | ||
| export { _INTERNAL_setDeferSegmentSpanCapture } from './deferSegmentSpanCapture'; | ||
| export { SentryNonRecordingSpan } from './sentryNonRecordingSpan'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import * as browserEntry from '../src/browser'; | ||
| import * as rootEntry from '../src/index'; | ||
| import { spanStreamingIntegration as browserSpanStreamingIntegration } from '../src/integrations/browserSpanStreaming'; | ||
| import { spanStreamingIntegration as plainSpanStreamingIntegration } from '../src/integrations/spanStreaming'; | ||
| import * as serverEntry from '../src/server'; | ||
| import * as browserSpanApi from '../src/tracing/browserSpanApi'; | ||
| import { startIdleSpan as plainStartIdleSpan } from '../src/tracing/idleSpan'; | ||
| import { | ||
| startInactiveSpan as plainStartInactiveSpan, | ||
| startSpan as plainStartSpan, | ||
| startSpanManual as plainStartSpanManual, | ||
| } from '../src/tracing/trace'; | ||
|
|
||
| // `server-exports` and `browser-exports` export these names with different implementations, which | ||
| // makes them ambiguous star exports at the root entry. `index.ts` disambiguates them with explicit | ||
| // re-exports — if someone adds another `export *` that also carries one of these names, or drops the | ||
| // explicit re-export, the root entry silently flips to the wrong variant (or fails to link). | ||
| describe('entry point resolution', () => { | ||
| const cases = [ | ||
| ['startSpan', plainStartSpan, browserSpanApi.startSpan], | ||
| ['startInactiveSpan', plainStartInactiveSpan, browserSpanApi.startInactiveSpan], | ||
| ['startSpanManual', plainStartSpanManual, browserSpanApi.startSpanManual], | ||
| ['startIdleSpan', plainStartIdleSpan, browserSpanApi.startIdleSpan], | ||
| ['spanStreamingIntegration', plainSpanStreamingIntegration, browserSpanStreamingIntegration], | ||
| ] as const; | ||
|
|
||
| it.each(cases)('`%s`: the root entry serves the plain variant', (name, plain) => { | ||
| expect(rootEntry[name]).toBe(plain); | ||
| }); | ||
|
|
||
| it.each(cases)('`%s`: the server entry serves the plain variant', (name, plain) => { | ||
| expect(serverEntry[name]).toBe(plain); | ||
| }); | ||
|
|
||
| it.each(cases)('`%s`: the browser entry serves the browser variant', (name, plain, browser) => { | ||
| expect(browserEntry[name]).toBe(browser); | ||
| expect(browserEntry[name]).not.toBe(plain); | ||
| }); | ||
| }); |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: Did you check that our browser SDKs all use this method and not import from core directly? e.g. svelte still does
import { debug, startInactiveSpan } from '@sentry/core';There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, in the next PR on the stack (#23362)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge the two first PR in sync to avoid any temporary issues