GeezerVison is showing github.com — feat(browser): Extract long task and LoAF tracking into `longTasksIntegration` by logaretm · Pull Request #26260 · getsentry/sentry-javascript
feat(browser): Extract long task and LoAF tracking into longTasksIntegration
#26260
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.ts
(7)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
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
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;
Comment thread
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 .
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)
Reviewed by Cursor Bugbot for commit 33f13a0 . Configure here .
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
return {
name: INTEGRATION_NAME,
setup() {
Comment thread
logaretm marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
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
Uh oh!
There was an error while loading. Please reload this page.
},
};
}) 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);
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);
});
});
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
Original file line number
Diff line number
Diff line change
@@ -0,0 +1 @@
export { longTasksIntegration } from '@sentry/browser-utils';
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
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.