Skip to content
Closed
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
23 changes: 14 additions & 9 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export interface BrowserTracingOptions {
/**
* If true, Sentry will capture long animation frames and add them to the corresponding transaction.
*
* Default: false
* Default: true
*/
enableLongAnimationFrame: boolean;

Expand Down Expand Up @@ -274,6 +274,9 @@ export interface BrowserTracingOptions {
onRequestSpanEnd?(span: Span, responseInformation: ResponseHookInfo): void;
}

// Bundlers can define this as `true` to tree-shake long task and long animation frame tracking
declare const __SENTRY_EXCLUDE_LONG_TASKS__: boolean | undefined;

const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
...TRACING_DEFAULTS,
instrumentNavigation: true,
Expand Down Expand Up @@ -433,14 +436,16 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption

registerSpanErrorInstrumentation();

if (
enableLongAnimationFrame &&
GLOBAL_OBJ.PerformanceObserver &&
PerformanceObserver.supportedEntryTypes?.includes('long-animation-frame')
) {
startTrackingLongAnimationFrames();
} else if (enableLongTask) {
startTrackingLongTasks();
if (typeof __SENTRY_EXCLUDE_LONG_TASKS__ === 'undefined' || !__SENTRY_EXCLUDE_LONG_TASKS__) {
if (
enableLongAnimationFrame &&
GLOBAL_OBJ.PerformanceObserver &&
PerformanceObserver.supportedEntryTypes?.includes('long-animation-frame')
) {
startTrackingLongAnimationFrames();
} else if (enableLongTask) {
startTrackingLongTasks();
}
}

if (detectRedirects && optionalWindowDocument) {
Expand Down
40 changes: 40 additions & 0 deletions packages/browser/test/tracing/browserTracingIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getSpanDescendants,
} from '@sentry/core';
import { startInactiveSpan } from '@sentry/core/browser';
import * as browserUtils from '@sentry/browser-utils';
import { JSDOM } from 'jsdom';
import { TextDecoder, TextEncoder } from 'util';
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -163,6 +164,45 @@ describe('browserTracingIntegration', () => {
});
});

describe('long task tracking', () => {
afterEach(() => {
vi.unstubAllGlobals();
});

it('starts long task tracking by default', () => {
const startTrackingLongTasksSpy = vi.spyOn(browserUtils, 'startTrackingLongTasks');

const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);
client.init();

expect(startTrackingLongTasksSpy).toHaveBeenCalledOnce();
});

it("doesn't start long task or long animation frame tracking if `__SENTRY_EXCLUDE_LONG_TASKS__` is true", () => {
vi.stubGlobal('__SENTRY_EXCLUDE_LONG_TASKS__', true);
const startTrackingLongTasksSpy = vi.spyOn(browserUtils, 'startTrackingLongTasks');
const startTrackingLongAnimationFramesSpy = vi.spyOn(browserUtils, 'startTrackingLongAnimationFrames');

const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);
client.init();

expect(startTrackingLongTasksSpy).not.toHaveBeenCalled();
expect(startTrackingLongAnimationFramesSpy).not.toHaveBeenCalled();
});
});
Comment thread
sentry-junior[bot] marked this conversation as resolved.
Resolved

it('works with tracing enabled', () => {
const client = new BrowserClient(
getDefaultBrowserClientOptions({
Expand Down
Loading