Repository navigation
feat(core): Add longTaskIntegration to report JS long tasks as metrics #6986
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
326f792
feat(core): Add longTaskIntegration to report JS long tasks as metrics
alwx 8d289e3
docs(core): Link longTaskIntegration changelog entry to its PR
alwx d30d70a
fix(core): Skip long task observer when metrics are off and guard det…
alwx 531ac86
Update packages/core/src/js/integrations/longTask.ts
alwx d0b151e
Update packages/core/src/js/integrations/longTask.ts
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import type { Client, Integration } from '@sentry/core'; | ||
|
|
||
| import { debug, metrics } from '@sentry/core'; | ||
|
|
||
| import { RN_GLOBAL_OBJ } from '../utils/worldwide'; | ||
|
|
||
| const INTEGRATION_NAME = 'LongTask'; | ||
|
|
||
| const METRIC_NAME = 'react_native.long_task.duration'; | ||
| const METRIC_OPTIONS = { unit: 'millisecond', attributes: { 'sentry.origin': 'auto.react_native.long_task' } }; | ||
|
|
||
| /** Options for {@link longTaskIntegration}. */ | ||
| export interface LongTaskOptions { | ||
| /** | ||
| * The minimum duration in milliseconds of a JS task to report it. | ||
| * React Native reports only tasks of 50 ms or longer, so a lower value has no effect. | ||
| * | ||
| * @default 200 | ||
| */ | ||
| minDurationMs?: number; | ||
| /** | ||
| * The probability (0.0 to 1.0) that an app launch reports its long tasks. | ||
| * The SDK decides once per SDK init. A launch reports all of its long tasks or none of them. | ||
| * Sentry does not scale the metric counts by this rate. | ||
| * | ||
| * @default 1 | ||
| */ | ||
| sampleRate?: number; | ||
| } | ||
|
|
||
| /** | ||
| * Reports JS thread long tasks as the `react_native.long_task.duration` distribution metric, | ||
| * independently of tracing. | ||
| * | ||
| * Requires React Native 0.82 or newer on the New Architecture. | ||
| * In other environments, the integration does nothing. | ||
| */ | ||
| export const longTaskIntegration = (options: LongTaskOptions = {}): Integration => { | ||
| const { minDurationMs = 200, sampleRate = 1 } = options; | ||
|
|
||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup(client: Client): void { | ||
| if (client.getOptions().enableMetrics === false) { | ||
| debug.log(`[${INTEGRATION_NAME}] Metrics are disabled, long tasks will not be reported.`); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Reading the lazy global loads the RN module, and `supportedEntryTypes` calls into native code. | ||
| const PerformanceObserver = RN_GLOBAL_OBJ.PerformanceObserver; | ||
| if (!PerformanceObserver?.supportedEntryTypes?.includes('longtask')) { | ||
| debug.log(`[${INTEGRATION_NAME}] Long tasks are not supported in this environment.`); | ||
| return; | ||
| } | ||
|
|
||
| if (Math.random() >= sampleRate) { | ||
| return; | ||
| } | ||
|
|
||
| const observer = new PerformanceObserver(list => { | ||
| for (const { duration } of list.getEntries()) { | ||
| if (duration >= minDurationMs) { | ||
| metrics.distribution(METRIC_NAME, duration, METRIC_OPTIONS); | ||
| } | ||
| } | ||
| }); | ||
| observer.observe({ type: 'longtask', buffered: true }); | ||
| client.on('close', () => observer.disconnect()); | ||
| } catch (error) { | ||
| debug.warn(`[${INTEGRATION_NAME}] Failed to observe long tasks.`, error); | ||
| } | ||
| }, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import type { Metric } from '@sentry/core'; | ||
|
|
||
| import { getClient } from '@sentry/core'; | ||
|
|
||
| import type { LongTaskOptions } from '../../src/js/integrations/longTask'; | ||
| import type { TestClientOptions } from '../mocks/client'; | ||
|
|
||
| import { longTaskIntegration } from '../../src/js/integrations/longTask'; | ||
| import { RN_GLOBAL_OBJ } from '../../src/js/utils/worldwide'; | ||
| import { setupTestClient } from '../mocks/client'; | ||
|
|
||
| type EntriesCallback = (list: { getEntries: () => Array<{ duration: number }> }) => void; | ||
|
|
||
| class FakePerformanceObserver { | ||
| public static supportedEntryTypes: ReadonlyArray<string> = ['longtask']; | ||
| public static instances: FakePerformanceObserver[] = []; | ||
|
|
||
| public observe = jest.fn(); | ||
| public disconnect = jest.fn(); | ||
|
|
||
| public constructor(public callback: EntriesCallback) { | ||
| FakePerformanceObserver.instances.push(this); | ||
| } | ||
|
|
||
| public emit(...durations: number[]): void { | ||
| this.callback({ getEntries: () => durations.map(duration => ({ duration })) }); | ||
| } | ||
| } | ||
|
|
||
| describe('longTaskIntegration', () => { | ||
| let beforeSendMetric: jest.Mock<Metric, [Metric]>; | ||
|
|
||
| const setup = (options?: LongTaskOptions, clientOptions?: Partial<TestClientOptions>): void => { | ||
| setupTestClient({ beforeSendMetric, integrations: [longTaskIntegration(options)], ...clientOptions }); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| beforeSendMetric = jest.fn(metric => metric); | ||
| FakePerformanceObserver.supportedEntryTypes = ['longtask']; | ||
| FakePerformanceObserver.instances = []; | ||
| RN_GLOBAL_OBJ.PerformanceObserver = FakePerformanceObserver; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| getClient()?.close(); | ||
| delete RN_GLOBAL_OBJ.PerformanceObserver; | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('observes buffered long tasks', () => { | ||
| setup(); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(1); | ||
| expect(FakePerformanceObserver.instances[0]?.observe).toHaveBeenCalledWith({ type: 'longtask', buffered: true }); | ||
| }); | ||
|
|
||
| it('emits a duration metric for each long task at or above the default threshold of 200 ms', () => { | ||
| setup(); | ||
|
|
||
| FakePerformanceObserver.instances[0]?.emit(199, 200, 350); | ||
|
|
||
| expect(beforeSendMetric).toHaveBeenCalledTimes(2); | ||
| expect(beforeSendMetric.mock.calls.map(([metric]) => metric)).toEqual([ | ||
| expect.objectContaining({ | ||
| name: 'react_native.long_task.duration', | ||
| type: 'distribution', | ||
| value: 200, | ||
| unit: 'millisecond', | ||
| attributes: expect.objectContaining({ 'sentry.origin': 'auto.react_native.long_task' }), | ||
| }), | ||
| expect.objectContaining({ name: 'react_native.long_task.duration', value: 350 }), | ||
| ]); | ||
| }); | ||
|
|
||
| it('uses a custom minDurationMs', () => { | ||
| setup({ minDurationMs: 100 }); | ||
|
|
||
| FakePerformanceObserver.instances[0]?.emit(99, 100); | ||
|
|
||
| expect(beforeSendMetric).toHaveBeenCalledTimes(1); | ||
| expect(beforeSendMetric).toHaveBeenCalledWith(expect.objectContaining({ value: 100 })); | ||
| }); | ||
|
|
||
| describe('sampleRate', () => { | ||
| it('does not observe when the launch is not sampled', () => { | ||
| jest.spyOn(Math, 'random').mockReturnValue(0.5); | ||
|
|
||
| setup({ sampleRate: 0.5 }); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('observes when the launch is sampled', () => { | ||
| jest.spyOn(Math, 'random').mockReturnValue(0.49); | ||
|
|
||
| setup({ sampleRate: 0.5 }); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('does not observe when sampleRate is 0', () => { | ||
| setup({ sampleRate: 0 }); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when long tasks are not supported', () => { | ||
| it('does nothing when PerformanceObserver is not defined', () => { | ||
| delete RN_GLOBAL_OBJ.PerformanceObserver; | ||
|
|
||
| expect(() => setup()).not.toThrow(); | ||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('does nothing when longtask is not a supported entry type', () => { | ||
| FakePerformanceObserver.supportedEntryTypes = ['mark', 'measure']; | ||
|
|
||
| setup(); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('does nothing when supportedEntryTypes is missing', () => { | ||
| // @ts-expect-error Simulates a runtime without the static property | ||
| FakePerformanceObserver.supportedEntryTypes = undefined; | ||
|
|
||
| setup(); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| it('does not observe when metrics are disabled', () => { | ||
| setup(undefined, { enableMetrics: false }); | ||
|
|
||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('does not throw when reading supportedEntryTypes throws', () => { | ||
| RN_GLOBAL_OBJ.PerformanceObserver = class extends FakePerformanceObserver { | ||
| public static get supportedEntryTypes(): ReadonlyArray<string> { | ||
| throw new Error('native module missing'); | ||
| } | ||
| }; | ||
|
|
||
| expect(() => setup()).not.toThrow(); | ||
| expect(FakePerformanceObserver.instances).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('does not throw when observe throws', () => { | ||
| RN_GLOBAL_OBJ.PerformanceObserver = class extends FakePerformanceObserver { | ||
| public observe = jest.fn(() => { | ||
| throw new Error('observe failed'); | ||
| }); | ||
| }; | ||
|
|
||
| expect(() => setup()).not.toThrow(); | ||
| }); | ||
|
|
||
| it('disconnects the observer when the client closes', async () => { | ||
| setup(); | ||
|
|
||
| await getClient()?.close(); | ||
|
|
||
| expect(FakePerformanceObserver.instances[0]?.disconnect).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.