From 8589b7bda7ec8f391dc19f6ae435011d67e67aea Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Tue, 10 Nov 2026 17:30:44 +0100 Subject: [PATCH 1/2] feat(browser): Extract long task and LoAF tracking into `longTasksIntegration` `browserTracingIntegration` registers it by default, so nothing changes for its users. `browserTracingCoreIntegration` users only bundle it when they add it. --- packages/browser-utils/src/index.ts | 1 + .../src/performance/longTasks.ts | 45 +++++++++++++++++++ packages/browser/src/index.ts | 1 + .../browser/src/integrations/longTasks.ts | 1 + .../src/tracing/browserTracingIntegration.ts | 31 ++++++------- .../tracing/browserTracingIntegration.test.ts | 26 +++++++++++ 6 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 packages/browser-utils/src/performance/longTasks.ts create mode 100644 packages/browser/src/integrations/longTasks.ts diff --git a/packages/browser-utils/src/index.ts b/packages/browser-utils/src/index.ts index 52da63c3d6b6..ef64f0a5bc70 100644 --- a/packages/browser-utils/src/index.ts +++ b/packages/browser-utils/src/index.ts @@ -24,6 +24,7 @@ export { export { elementTimingIntegration, startTrackingElementTiming } from './performance/elementTiming'; export { interactionsIntegration } from './performance/interactions'; +export { longTasksIntegration } from './performance/longTasks'; export { isBotUserAgent } from './isBotUserAgent'; diff --git a/packages/browser-utils/src/performance/longTasks.ts b/packages/browser-utils/src/performance/longTasks.ts new file mode 100644 index 000000000000..d95d3d59a9fd --- /dev/null +++ b/packages/browser-utils/src/performance/longTasks.ts @@ -0,0 +1,45 @@ +import type { IntegrationFn } from '@sentry/core'; +import { defineIntegration, GLOBAL_OBJ } from '@sentry/core'; +import { startTrackingLongAnimationFrames, startTrackingLongTasks } from './entries'; + +interface LongTasksOptions { + /** + * If true, long tasks are captured as spans when long animation frames are disabled or not supported. + * + * Default: true + */ + enableLongTask?: boolean; + + /** + * If true, long animation frames are captured as spans in browsers that support them. + * + * Default: true + */ + enableLongAnimationFrame?: boolean; +} + +const INTEGRATION_NAME = 'LongTasks'; + +const _longTasksIntegration = ((options: LongTasksOptions = {}) => { + const { enableLongTask = true, enableLongAnimationFrame = true } = options; + + return { + name: INTEGRATION_NAME, + setup() { + if ( + enableLongAnimationFrame && + GLOBAL_OBJ.PerformanceObserver && + PerformanceObserver.supportedEntryTypes?.includes('long-animation-frame') + ) { + startTrackingLongAnimationFrames(); + } else if (enableLongTask) { + startTrackingLongTasks(); + } + }, + }; +}) satisfies IntegrationFn; + +/** + * Captures long animation frames, or long tasks where those are not supported, as spans on the active span. + */ +export const longTasksIntegration = defineIntegration(_longTasksIntegration); diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index ffda26d539ff..0cab1fdf912f 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -51,6 +51,7 @@ export { } from '@sentry/browser-utils'; export type { CachedRouteProvider, RouteProvider } from '@sentry/browser-utils'; export { interactionsIntegration } from './integrations/interactions'; +export { longTasksIntegration } from './integrations/longTasks'; export type { RequestInstrumentationOptions } from './tracing/request'; export { diff --git a/packages/browser/src/integrations/longTasks.ts b/packages/browser/src/integrations/longTasks.ts new file mode 100644 index 000000000000..6801fdbe57fa --- /dev/null +++ b/packages/browser/src/integrations/longTasks.ts @@ -0,0 +1 @@ +export { longTasksIntegration } from '@sentry/browser-utils'; diff --git a/packages/browser/src/tracing/browserTracingIntegration.ts b/packages/browser/src/tracing/browserTracingIntegration.ts index 91d160ed7103..f43ac57d6281 100644 --- a/packages/browser/src/tracing/browserTracingIntegration.ts +++ b/packages/browser/src/tracing/browserTracingIntegration.ts @@ -18,7 +18,6 @@ import { getClient, getCurrentScope, getDynamicSamplingContextFromSpan, - GLOBAL_OBJ, hasSpansEnabled, hasSpanStreamingEnabled, NAVIGATION_SPAN_NAME_FALLBACK, @@ -36,11 +35,10 @@ import { import { _INTERNAL_ensureBrowserSpanStreaming, startIdleSpan, startInactiveSpan } from '@sentry/core/browser'; import { addHistoryInstrumentationHandler, + longTasksIntegration, addPerformanceEntries, getLocationHref, isBotUserAgent, - startTrackingLongAnimationFrames, - startTrackingLongTasks, } from '@sentry/browser-utils'; import { BROWSER_NAVIGATION_TYPE } from '@sentry/conventions/attributes'; import { DEBUG_BUILD } from '../debug-build'; @@ -286,7 +284,10 @@ export interface BrowserTracingOptions { * Options for `browserTracingCoreIntegration`: the options of `browserTracingIntegration`, minus the ones * for features that `browserTracingIntegration` registers as standalone integrations. */ -export type BrowserTracingCoreOptions = Omit; +export type BrowserTracingCoreOptions = Omit< + BrowserTracingOptions, + 'enableInp' | 'webVitals' | 'enableElementTiming' | 'enableLongTask' | 'enableLongAnimationFrame' +>; const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = { ...TRACING_DEFAULTS, @@ -294,8 +295,6 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = { instrumentBfcacheRestore: true, instrumentPageLoad: true, markBackgroundSpan: true, - enableLongTask: true, - enableLongAnimationFrame: true, ignoreResourceSpans: [], detectRedirects: true, linkPreviousTrace: 'in-memory', @@ -311,6 +310,8 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = { const DEFAULT_FORWARDED_OPTIONS = { // oxlint-disable-next-line typescript/no-deprecated -- still honoured until it is removed enableInp: true, + enableLongTask: true, + enableLongAnimationFrame: true, } satisfies Partial; /** @@ -336,6 +337,8 @@ export const browserTracingIntegration = ((options: Partial { @@ -351,6 +354,10 @@ export const browserTracingIntegration = ((options: Partial { lastInteractionTimestamp = timestampInSeconds(); diff --git a/packages/browser/test/tracing/browserTracingIntegration.test.ts b/packages/browser/test/tracing/browserTracingIntegration.test.ts index 394d72d201fd..8986e8c98322 100644 --- a/packages/browser/test/tracing/browserTracingIntegration.test.ts +++ b/packages/browser/test/tracing/browserTracingIntegration.test.ts @@ -224,6 +224,32 @@ describe('browserTracingIntegration', () => { expect(client.getIntegrationByName('WebVitals')).toBeUndefined(); }); + it.each([ + ['registers longTasksIntegration by default', {}, true], + ['registers longTasksIntegration with only long tasks enabled', { enableLongAnimationFrame: false }, true], + [ + 'does not register longTasksIntegration when both options are off', + { enableLongTask: false, enableLongAnimationFrame: false }, + false, + ], + [ + 'does not register longTasksIntegration when both options are explicitly undefined', + { enableLongTask: undefined, enableLongAnimationFrame: undefined }, + false, + ], + ])('%s', (_name, options, registered) => { + const client = new BrowserClient( + getDefaultBrowserClientOptions({ + tracesSampleRate: 1, + integrations: [browserTracingIntegration(options)], + }), + ); + setCurrentClient(client); + client.init(); + + expect(!!client.getIntegrationByName('LongTasks')).toBe(registered); + }); + it('keeps the webVitalsIntegration the user supplies', () => { const userWebVitals = webVitalsModule.webVitalsIntegration({ softNavigations: false }); From 33f13a0dee031c2451f131399b8a5243672d9dda Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Mon, 16 Nov 2026 10:29:13 -0500 Subject: [PATCH 2/2] fix(browser-utils): Skip `longTasksIntegration` setup for bot user agents --- .../src/performance/longTasks.ts | 5 ++++ .../test/performance/longTasks.test.ts | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 packages/browser-utils/test/performance/longTasks.test.ts diff --git a/packages/browser-utils/src/performance/longTasks.ts b/packages/browser-utils/src/performance/longTasks.ts index d95d3d59a9fd..40c1de24cf4f 100644 --- a/packages/browser-utils/src/performance/longTasks.ts +++ b/packages/browser-utils/src/performance/longTasks.ts @@ -1,5 +1,6 @@ import type { IntegrationFn } from '@sentry/core'; import { defineIntegration, GLOBAL_OBJ } from '@sentry/core'; +import { isBotUserAgent } from '../isBotUserAgent'; import { startTrackingLongAnimationFrames, startTrackingLongTasks } from './entries'; interface LongTasksOptions { @@ -26,6 +27,10 @@ const _longTasksIntegration = ((options: LongTasksOptions = {}) => { return { name: INTEGRATION_NAME, setup() { + if (isBotUserAgent()) { + return; + } + if ( enableLongAnimationFrame && GLOBAL_OBJ.PerformanceObserver && diff --git a/packages/browser-utils/test/performance/longTasks.test.ts b/packages/browser-utils/test/performance/longTasks.test.ts new file mode 100644 index 000000000000..4ca3dce01dbb --- /dev/null +++ b/packages/browser-utils/test/performance/longTasks.test.ts @@ -0,0 +1,26 @@ +/** + * @vitest-environment jsdom + */ + +import { afterEach, describe, expect, it, vi } from 'vitest'; +import * as entries from '../../src/performance/entries'; +import { longTasksIntegration } from '../../src/performance/longTasks'; +import { getDefaultClientOptions, TestClient } from '../utils/TestClient'; + +describe('longTasksIntegration', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it.each([ + ['tracks long tasks for normal user agents', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120.0.0.0', 1], + ['does not track long tasks for bot user agents', 'Mozilla/5.0 (compatible; Googlebot/2.1)', 0], + ])('%s', (_name, userAgent, calls) => { + const trackSpy = vi.spyOn(entries, 'startTrackingLongTasks').mockImplementation(() => {}); + vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(userAgent); + + longTasksIntegration().setup?.(new TestClient(getDefaultClientOptions())); + + expect(trackSpy).toHaveBeenCalledTimes(calls); + }); +});