Skip to content

Commit 7548e64

Browse files
committed
ref(replay): Extract performanceObserver out
1 parent 4a266ab commit 7548e64

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ReplayContainer } from '../replay';
2+
import { AllPerformanceEntry } from '../types';
3+
import { dedupePerformanceEntries } from '../util/dedupePerformanceEntries';
4+
5+
export function setupPerformanceObserver(replay: ReplayContainer): PerformanceObserver {
6+
const performanceObserverHandler = (list: PerformanceObserverEntryList): void => {
7+
// For whatever reason the observer was returning duplicate navigation
8+
// entries (the other entry types were not duplicated).
9+
const newPerformanceEntries = dedupePerformanceEntries(
10+
replay.performanceEvents,
11+
list.getEntries() as AllPerformanceEntry[],
12+
);
13+
replay.performanceEvents = newPerformanceEntries;
14+
};
15+
16+
const performanceObserver = new PerformanceObserver(performanceObserverHandler);
17+
18+
[
19+
'element',
20+
'event',
21+
'first-input',
22+
'largest-contentful-paint',
23+
'layout-shift',
24+
'longtask',
25+
'navigation',
26+
'paint',
27+
'resource',
28+
].forEach(type => {
29+
try {
30+
performanceObserver.observe({
31+
type,
32+
buffered: true,
33+
});
34+
} catch {
35+
// This can throw if an entry type is not supported in the browser.
36+
// Ignore these errors.
37+
}
38+
});
39+
40+
return performanceObserver;
41+
}

packages/replay/src/replay.ts

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { addGlobalEventProcessor, getCurrentHub, Scope, setContext } from '@sent
33
import { Breadcrumb, Client, Event } from '@sentry/types';
44
import { addInstrumentationHandler, createEnvelope, logger } from '@sentry/utils';
55
import debounce from 'lodash.debounce';
6-
import { PerformanceObserverEntryList } from 'perf_hooks';
76
import { EventType, record } from 'rrweb';
87

98
import {
@@ -19,6 +18,7 @@ import { handleFetchSpanListener } from './coreHandlers/handleFetch';
1918
import { handleGlobalEventListener } from './coreHandlers/handleGlobalEvent';
2019
import { handleHistorySpanListener } from './coreHandlers/handleHistory';
2120
import { handleXhrSpanListener } from './coreHandlers/handleXhr';
21+
import { setupPerformanceObserver } from './coreHandlers/performanceObserver';
2222
import { createMemoryEntry, createPerformanceEntries, ReplayPerformanceEntry } from './createPerformanceEntry';
2323
import { createEventBuffer, EventBuffer } from './eventBuffer';
2424
import { deleteSession } from './session/deleteSession';
@@ -38,7 +38,6 @@ import type {
3838
import { captureInternalException } from './util/captureInternalException';
3939
import { createBreadcrumb } from './util/createBreadcrumb';
4040
import { createPayload } from './util/createPayload';
41-
import { dedupePerformanceEntries } from './util/dedupePerformanceEntries';
4241
import { isExpired } from './util/isExpired';
4342
import { isSessionExpired } from './util/isSessionExpired';
4443
import { overwriteRecordDroppedEvent, restoreRecordDroppedEvent } from './util/monkeyPatchRecordDroppedEvent';
@@ -335,30 +334,7 @@ export class ReplayContainer {
335334
return;
336335
}
337336

338-
this._performanceObserver = new PerformanceObserver(this.handlePerformanceObserver);
339-
340-
// Observe almost everything for now (no mark/measure)
341-
[
342-
'element',
343-
'event',
344-
'first-input',
345-
'largest-contentful-paint',
346-
'layout-shift',
347-
'longtask',
348-
'navigation',
349-
'paint',
350-
'resource',
351-
].forEach(type => {
352-
try {
353-
this._performanceObserver?.observe({
354-
type,
355-
buffered: true,
356-
});
357-
} catch {
358-
// This can throw if an entry type is not supported in the browser.
359-
// Ignore these errors.
360-
}
361-
});
337+
this._performanceObserver = setupPerformanceObserver(this);
362338
}
363339

364340
/**
@@ -570,19 +546,6 @@ export class ReplayContainer {
570546
});
571547
};
572548

573-
/**
574-
* Keep a list of performance entries that will be sent with a replay
575-
*/
576-
handlePerformanceObserver: (list: PerformanceObserverEntryList) => void = (list: PerformanceObserverEntryList) => {
577-
// For whatever reason the observer was returning duplicate navigation
578-
// entries (the other entry types were not duplicated).
579-
const newPerformanceEntries = dedupePerformanceEntries(
580-
this.performanceEvents,
581-
list.getEntries() as AllPerformanceEntry[],
582-
);
583-
this.performanceEvents = newPerformanceEntries;
584-
};
585-
586549
/**
587550
* Tasks to run when we consider a page to be hidden (via blurring and/or visibility)
588551
*/

0 commit comments

Comments
 (0)