Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/browser-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
50 changes: 50 additions & 0 deletions packages/browser-utils/src/performance/longTasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, GLOBAL_OBJ } from '@sentry/core';
import { isBotUserAgent } from '../isBotUserAgent';
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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined flags re-enable tracking

Low Severity

DEFAULT_FORWARDED_OPTIONS is spread so an explicit undefined for enableLongTask or enableLongAnimationFrame stays undefined, matching the old falsy behavior. Those values are then passed into longTasksIntegration, whose destructuring defaults turn undefined back into true. A mixed setting such as enableLongAnimationFrame: undefined with enableLongTask left at the default therefore tracks LoAF again instead of falling through to long tasks.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 33f13a0. Configure here.


return {
name: INTEGRATION_NAME,
setup() {
Comment thread
logaretm marked this conversation as resolved.
Resolved
if (isBotUserAgent()) {
return;
}

if (
enableLongAnimationFrame &&
GLOBAL_OBJ.PerformanceObserver &&
PerformanceObserver.supportedEntryTypes?.includes('long-animation-frame')
) {
startTrackingLongAnimationFrames();
} else if (enableLongTask) {
startTrackingLongTasks();
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
},
};
}) 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);
26 changes: 26 additions & 0 deletions packages/browser-utils/test/performance/longTasks.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/integrations/longTasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { longTasksIntegration } from '@sentry/browser-utils';
31 changes: 13 additions & 18 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getClient,
getCurrentScope,
getDynamicSamplingContextFromSpan,
GLOBAL_OBJ,
hasSpansEnabled,
hasSpanStreamingEnabled,
NAVIGATION_SPAN_NAME_FALLBACK,
Expand All @@ -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';
Expand Down Expand Up @@ -286,16 +284,17 @@ 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<BrowserTracingOptions, 'enableInp' | 'webVitals' | 'enableElementTiming'>;
export type BrowserTracingCoreOptions = Omit<
BrowserTracingOptions,
'enableInp' | 'webVitals' | 'enableElementTiming' | 'enableLongTask' | 'enableLongAnimationFrame'
>;

const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = {
...TRACING_DEFAULTS,
instrumentNavigation: true,
instrumentBfcacheRestore: true,
instrumentPageLoad: true,
markBackgroundSpan: true,
enableLongTask: true,
enableLongAnimationFrame: true,
ignoreResourceSpans: [],
detectRedirects: true,
linkPreviousTrace: 'in-memory',
Expand All @@ -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<BrowserTracingOptions>;

/**
Expand All @@ -336,6 +337,8 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
// oxlint-disable-next-line typescript/no-deprecated -- still honoured until it is removed
enableInp,
webVitals,
enableLongTask,
enableLongAnimationFrame,
} = { ...DEFAULT_FORWARDED_OPTIONS, ...options };

return _browserTracingIntegration(options, client => {
Expand All @@ -351,6 +354,10 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
ignore: enableInp || ignore.includes('inp') ? ignore : [...ignore, 'inp'],
}),
);

if (enableLongTask || enableLongAnimationFrame) {
client.addIntegration(longTasksIntegration({ enableLongTask, enableLongAnimationFrame }));
}
});
}) satisfies IntegrationFn;

Expand All @@ -375,8 +382,6 @@ function _browserTracingIntegration(
const optionalWindowDocument = WINDOW.document as (typeof WINDOW)['document'] | undefined;

const {
enableLongTask,
enableLongAnimationFrame,
beforeStartSpan,
idleTimeout,
finalTimeout,
Expand Down Expand Up @@ -487,16 +492,6 @@ function _browserTracingIntegration(

registerSpanErrorInstrumentation();

if (
enableLongAnimationFrame &&
GLOBAL_OBJ.PerformanceObserver &&
PerformanceObserver.supportedEntryTypes?.includes('long-animation-frame')
) {
startTrackingLongAnimationFrames();
} else if (enableLongTask) {
startTrackingLongTasks();
}

if (detectRedirects && optionalWindowDocument) {
const interactionHandler = (): void => {
lastInteractionTimestamp = timestampInSeconds();
Expand Down
26 changes: 26 additions & 0 deletions packages/browser/test/tracing/browserTracingIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
Loading