Skip to content

Commit 232e285

Browse files
committed
ref(replay): Extract createPerformanceSpans out
1 parent 58efc93 commit 232e285

File tree

7 files changed

+45
-33
lines changed

7 files changed

+45
-33
lines changed

packages/replay/src/coreHandlers/handleFetch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
22
import { ReplayContainer } from '../replay';
3+
import { createPerformanceSpans } from '../util/createPerformanceSpans';
34
import { isIngestHost } from '../util/isIngestHost';
45

56
interface FetchHandlerData {
@@ -57,7 +58,7 @@ export function handleFetchSpanListener(replay: ReplayContainer): (handlerData:
5758
}
5859

5960
replay.addUpdate(() => {
60-
void replay.createPerformanceSpans([result]);
61+
void createPerformanceSpans(replay, [result]);
6162
// Returning true will cause `addUpdate` to not flush
6263
// We do not want network requests to cause a flush. This will prevent
6364
// recurring/polling requests from keeping the replay session alive.

packages/replay/src/coreHandlers/handleHistory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
22
import { ReplayContainer } from '../replay';
3+
import { createPerformanceSpans } from '../util/createPerformanceSpans';
34

45
interface HistoryHandlerData {
56
from: string;
@@ -39,7 +40,7 @@ export function handleHistorySpanListener(replay: ReplayContainer): (handlerData
3940
replay.triggerUserActivity();
4041

4142
replay.addUpdate(() => {
42-
void replay.createPerformanceSpans([result]);
43+
void createPerformanceSpans(replay, [result]);
4344
// Returning false to flush
4445
return false;
4546
});

packages/replay/src/coreHandlers/handleXhr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
22
import { ReplayContainer } from '../replay';
3+
import { createPerformanceSpans } from '../util/createPerformanceSpans';
34
import { isIngestHost } from '../util/isIngestHost';
45

56
// From sentry-javascript
@@ -76,7 +77,7 @@ export function handleXhrSpanListener(replay: ReplayContainer): (handlerData: Xh
7677
}
7778

7879
replay.addUpdate(() => {
79-
void replay.createPerformanceSpans([result]);
80+
void createPerformanceSpans(replay, [result]);
8081
// Returning true will cause `addUpdate` to not flush
8182
// We do not want network requests to cause a flush. This will prevent
8283
// recurring/polling requests from keeping the replay session alive.

packages/replay/src/replay.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { handleGlobalEventListener } from './coreHandlers/handleGlobalEvent';
1919
import { handleHistorySpanListener } from './coreHandlers/handleHistory';
2020
import { handleXhrSpanListener } from './coreHandlers/handleXhr';
2121
import { setupPerformanceObserver } from './coreHandlers/performanceObserver';
22-
import { createMemoryEntry, createPerformanceEntries, ReplayPerformanceEntry } from './createPerformanceEntry';
22+
import { createMemoryEntry, createPerformanceEntries } from './createPerformanceEntry';
2323
import { createEventBuffer, EventBuffer } from './eventBuffer';
2424
import { deleteSession } from './session/deleteSession';
2525
import { getSession } from './session/getSession';
@@ -35,13 +35,14 @@ import type {
3535
ReplayPluginOptions,
3636
SendReplay,
3737
} from './types';
38+
import { addEvent } from './util/addEvent';
3839
import { captureInternalException } from './util/captureInternalException';
3940
import { createBreadcrumb } from './util/createBreadcrumb';
4041
import { createPayload } from './util/createPayload';
42+
import { createPerformanceSpans } from './util/createPerformanceSpans';
4143
import { isExpired } from './util/isExpired';
4244
import { isSessionExpired } from './util/isSessionExpired';
4345
import { overwriteRecordDroppedEvent, restoreRecordDroppedEvent } from './util/monkeyPatchRecordDroppedEvent';
44-
import { addEvent } from './util/addEvent';
4546

4647
/**
4748
* Returns true to return control to calling function, otherwise continue with normal batching
@@ -686,30 +687,6 @@ export class ReplayContainer {
686687
});
687688
}
688689

689-
/**
690-
* Create a "span" for each performance entry. The parent transaction is `this.replayEvent`.
691-
*/
692-
createPerformanceSpans(entries: ReplayPerformanceEntry[]): Promise<void[]> {
693-
return Promise.all(
694-
entries.map(({ type, start, end, name, data }) =>
695-
addEvent(this, {
696-
type: EventType.Custom,
697-
timestamp: start,
698-
data: {
699-
tag: 'performanceSpan',
700-
payload: {
701-
op: type,
702-
description: name,
703-
startTimestamp: start,
704-
endTimestamp: end,
705-
data,
706-
},
707-
},
708-
}),
709-
),
710-
);
711-
}
712-
713690
/**
714691
* Observed performance events are added to `this.performanceEvents`. These
715692
* are included in the replay event before it is finished and sent to Sentry.
@@ -719,7 +696,7 @@ export class ReplayContainer {
719696
const entries = [...this.performanceEvents];
720697
this.performanceEvents = [];
721698

722-
return this.createPerformanceSpans(createPerformanceEntries(entries));
699+
return createPerformanceSpans(this, createPerformanceEntries(entries));
723700
}
724701

725702
/**
@@ -733,7 +710,7 @@ export class ReplayContainer {
733710
return;
734711
}
735712

736-
return this.createPerformanceSpans([
713+
return createPerformanceSpans(this, [
737714
// @ts-ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
738715
createMemoryEntry(WINDOW.performance.memory),
739716
]);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { EventType } from 'rrweb';
2+
3+
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
4+
import { ReplayContainer } from '../replay';
5+
import { addEvent } from './addEvent';
6+
7+
/**
8+
* Create a "span" for each performance entry. The parent transaction is `this.replayEvent`.
9+
*/
10+
export function createPerformanceSpans(replay: ReplayContainer, entries: ReplayPerformanceEntry[]): Promise<void[]> {
11+
return Promise.all(
12+
entries.map(({ type, start, end, name, data }) =>
13+
addEvent(replay, {
14+
type: EventType.Custom,
15+
timestamp: start,
16+
data: {
17+
tag: 'performanceSpan',
18+
payload: {
19+
op: type,
20+
description: name,
21+
startTimestamp: start,
22+
endTimestamp: end,
23+
data,
24+
},
25+
},
26+
}),
27+
),
28+
);
29+
}

packages/replay/test/unit/flush.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as SentryUtils from '@sentry/utils';
22

33
import { SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
4+
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
45
import { createPerformanceEntries } from './../../src/createPerformanceEntry';
56
import { ReplayContainer } from './../../src/replay';
67
import { useFakeTimers } from './../../test/utils/use-fake-timers';
@@ -179,7 +180,8 @@ it('long first flush enqueues following events', async () => {
179180

180181
// Add this to test that segment ID increases
181182
mockAddPerformanceEntries.mockImplementationOnce(async () => {
182-
return replay.createPerformanceSpans(
183+
return createPerformanceSpans(
184+
replay,
183185
createPerformanceEntries([
184186
{
185187
name: 'https://sentry.io/foo.js',

packages/replay/test/unit/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MAX_SESSION_LIFE, REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW
88
import { ReplayContainer } from '../../src/replay';
99
import { RecordingEvent } from '../../src/types';
1010
import { addEvent } from '../../src/util/addEvent';
11+
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
1112
import { useFakeTimers } from '../utils/use-fake-timers';
1213
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
1314
import { BASE_TIMESTAMP, RecordMock } from './../index';
@@ -461,7 +462,7 @@ describe('Replay', () => {
461462
// performance events can still be collected while recording is stopped
462463
// TODO: we may want to prevent `addEvent` from adding to buffer when user is inactive
463464
replay.addUpdate(() => {
464-
replay.createPerformanceSpans([
465+
createPerformanceSpans(replay, [
465466
{
466467
type: 'navigation.navigate',
467468
name: 'foo',

0 commit comments

Comments
 (0)