Skip to content

Commit 1d6c45b

Browse files
committed
chore(monitor): add debug meta for long interaction times
1 parent 2b5b313 commit 1d6c45b

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

packages/scan/src/core/monitor/network.ts

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import {
66
MAX_PENDING_REQUESTS,
77
} from './constants';
88
import { getSession } from './utils';
9-
import type { Interaction, IngestRequest, InternalInteraction, Component } from './types';
9+
import type {
10+
Interaction,
11+
IngestRequest,
12+
InternalInteraction,
13+
Component,
14+
} from './types';
1015

1116
const INTERACTION_TIME_TILL_COMPLETED = 4000;
1217

@@ -55,18 +60,77 @@ export const flush = async (): Promise<void> => {
5560
for (let i = 0; i < completedInteractions.length; i++) {
5661
const interaction = completedInteractions[i];
5762

63+
// META INFORMATION IS FOR DEBUGGING THIS MUST BE REMOVED SOON
64+
const {
65+
duration,
66+
entries,
67+
id,
68+
inputDelay,
69+
latency,
70+
presentationDelay,
71+
processingDuration,
72+
processingEnd,
73+
processingStart,
74+
referrer,
75+
startTime,
76+
target,
77+
timeOrigin,
78+
timeSinceTabInactive,
79+
timestamp,
80+
type,
81+
visibilityState,
82+
} = interaction.performanceEntry;
5883
aggregatedInteractions.push({
5984
id: i,
6085
path: interaction.componentPath,
6186
name: interaction.componentName,
62-
time: interaction.performanceEntry.duration,
63-
timestamp: interaction.performanceEntry.timestamp,
64-
type: interaction.performanceEntry.type,
87+
time: duration,
88+
timestamp,
89+
type,
90+
// fixme: we can aggregate around url|route|commit|branch better to compress payload
6591
url: interaction.url,
6692
route: interaction.route,
6793
commit: interaction.commit,
6894
branch: interaction.branch,
6995
uniqueInteractionId: interaction.uniqueInteractionId,
96+
meta: {
97+
performanceEntry: {
98+
id,
99+
inputDelay,
100+
latency,
101+
presentationDelay,
102+
processingDuration,
103+
processingEnd,
104+
processingStart,
105+
referrer,
106+
startTime,
107+
target,
108+
timeOrigin,
109+
timeSinceTabInactive,
110+
visibilityState,
111+
duration: duration,
112+
entries: entries.map((entry) => {
113+
const {
114+
duration,
115+
entryType,
116+
interactionId,
117+
name,
118+
processingEnd,
119+
processingStart,
120+
startTime,
121+
} = entry;
122+
return {
123+
duration,
124+
entryType,
125+
interactionId,
126+
name,
127+
processingEnd,
128+
processingStart,
129+
startTime,
130+
};
131+
}),
132+
},
133+
},
70134
});
71135

72136
const components = Array.from(interaction.components.entries());

packages/scan/src/core/monitor/performance.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { type Fiber } from 'react-reconciler';
32
import { getDisplayName } from 'bippy';
43
import { Store } from '../..';
@@ -127,7 +126,10 @@ function getCleanComponentName(component: any): string {
127126
const name = getDisplayName(component);
128127
if (!name) return '';
129128

130-
return name.replace(/^(?:Memo|Forward(?:Ref)?|With.*?)\((?<inner>.*?)\)$/, '$<inner>');
129+
return name.replace(
130+
/^(?:Memo|Forward(?:Ref)?|With.*?)\((?<inner>.*?)\)$/,
131+
'$<inner>',
132+
);
131133
}
132134

133135
export function normalizePath(path: Array<string>): string {
@@ -162,6 +164,25 @@ const getFirstNamedAncestorCompositeFiber = (element: Element) => {
162164
}
163165
return parentCompositeFiber;
164166
};
167+
168+
let unsubscribeTrackVisibilityChange: () => void;
169+
// fixme: compress me if this stays here for bad interaction time checks
170+
let lastVisibilityHiddenAt: number | 'never-hidden' = 'never-hidden';
171+
172+
const trackVisibilityChange = () => {
173+
trackVisibilityChange();
174+
const onVisibilityChange = () => {
175+
if (document.hidden) {
176+
lastVisibilityHiddenAt = Date.now();
177+
}
178+
};
179+
document.addEventListener('visibilitychange', onVisibilityChange);
180+
181+
unsubscribeTrackVisibilityChange = () => {
182+
document.removeEventListener('visibilitychange', onVisibilityChange);
183+
};
184+
};
185+
165186
// todo: update monitoring api to expose filters for component names
166187
export function initPerformanceMonitoring(options?: Partial<PathFilters>) {
167188
const filters = { ...DEFAULT_FILTERS, ...options };
@@ -211,6 +232,7 @@ export function initPerformanceMonitoring(options?: Partial<PathFilters>) {
211232
const setupPerformanceListener = (
212233
onEntry: (interaction: PerformanceInteraction) => void,
213234
) => {
235+
trackVisibilityChange();
214236
const longestInteractionList: Array<PerformanceInteraction> = [];
215237
const longestInteractionMap = new Map<string, PerformanceInteraction>();
216238
const interactionTargetMap = new Map<string, Element>();
@@ -258,6 +280,10 @@ const setupPerformanceListener = (
258280
presentationDelay:
259281
entry.duration - (entry.processingEnd - entry.startTime),
260282
timestamp: Date.now(),
283+
timeSinceTabInactive: lastVisibilityHiddenAt === 'never-hidden' ? 'never-hidden' : Date.now() - lastVisibilityHiddenAt,
284+
visibilityState: document.visibilityState,
285+
timeOrigin: performance.timeOrigin,
286+
referrer: document.referrer,
261287
};
262288
longestInteractionMap.set(interaction.id, interaction);
263289
longestInteractionList.push(interaction);

packages/scan/src/core/monitor/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export enum Device {
66
MOBILE = 2,
77
}
88

9-
109
export interface Session {
1110
id: string;
1211
device: Device;
@@ -39,6 +38,8 @@ export interface Interaction {
3938
projectId?: string;
4039
sessionId?: string;
4140
uniqueInteractionId: string;
41+
42+
meta?: unknown;
4243
}
4344

4445
export interface Component {
@@ -102,4 +103,8 @@ export interface PerformanceInteraction {
102103
processingDuration: number;
103104
presentationDelay: number;
104105
timestamp: number;
106+
timeSinceTabInactive: number | 'never-hidden';
107+
visibilityState: DocumentVisibilityState;
108+
timeOrigin: number;
109+
referrer: string;
105110
}

0 commit comments

Comments
 (0)