Skip to content

Commit b1facc2

Browse files
committed
refactor(monitor): update Monitoring component
- Removed unused imports and variables - Reorganized code for better readability - Added optional parameters for path, params, commit, and branch in Monitoring component - Updated getSession function to accept optional commit and branch parameters - Updated flush function to pass commit and branch to getSession - Updated toPayloadInteraction function to include commit and branch in interaction payload - Updated initPerformanceMonitoring function to include commit and branch in performance entry - Updated Session interface to include url, route, commit, and branch properties - Updated Interaction interface to include commit and branch properties - Updated InternalInteraction interface to include commit and branch properties - Updated AstroMonitor component to use path instead of pathname prop - Updated AstroMonitor component to pass path and params to computeRoute function - Updated MonitoringWithoutRouteProps interface to include optional commit and branch properties
1 parent f5dc1ed commit b1facc2

File tree

8 files changed

+68
-23
lines changed

8 files changed

+68
-23
lines changed

packages/scan/src/core/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ export type MonitoringOptions = Pick<
128128

129129
interface Monitor {
130130
pendingRequests: number;
131+
interactions: Array<InternalInteraction>;
132+
session: ReturnType<typeof getSession>;
131133
url: string | null;
132134
apiKey: string | null;
133-
interactions: Array<InternalInteraction>;
134135
route: string | null;
135-
session: ReturnType<typeof getSession>;
136136
path: string | null;
137137
commit: string | null;
138138
branch: string | null;

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@ import { addFiberToSet, isValidFiber, updateFiberRenderData } from '../utils';
1414
import { initPerformanceMonitoring } from './performance';
1515
import { getSession } from './utils';
1616
import { flush } from './network';
17+
import { computeRoute } from './params/utils';
1718

1819
// max retries before the set of components do not get reported (avoid memory leaks of the set of fibers stored on the component aggregation)
1920
const MAX_RETRIES_BEFORE_COMPONENT_GC = 7;
2021

2122
export interface MonitoringProps {
2223
url?: string;
2324
apiKey: string;
24-
path: string;
25-
route: string | null;
26-
commit: string | null;
27-
branch: string | null;
25+
26+
// For Session and Interaction
27+
path?: string | null; // pathname (i.e /foo/bar)
28+
route?: string | null; // computed from path and params
29+
30+
// To compute Route when using Monitoring without framework
31+
// Only used to compute the route
32+
params?: Record<string, string>;
33+
34+
// Tracking regressions across commits and branches
35+
commit?: string | null;
36+
branch?: string | null;
2837
}
2938

3039
export interface MonitoringWithoutRouteProps
@@ -33,28 +42,35 @@ export interface MonitoringWithoutRouteProps
3342
export const Monitoring = ({
3443
url,
3544
apiKey,
36-
path,
37-
route,
38-
commit,
39-
branch,
45+
path = null, // path passed down would be reactive
46+
params,
47+
route = null,
48+
commit = null,
49+
branch = null,
4050
}: MonitoringProps) => {
4151
if (!apiKey)
4252
throw new Error('Please provide a valid API key for React Scan monitoring');
4353
url ??= 'https://monitoring.react-scan.com/api/v1/ingest';
4454

4555
Store.monitor.value ??= {
4656
pendingRequests: 0,
57+
interactions: [],
58+
session: getSession({ commit, branch }).catch(() => null),
4759
url,
4860
apiKey,
49-
interactions: [],
50-
session: getSession().catch(() => null),
5161
route,
5262
path,
5363
commit,
5464
branch,
5565
};
56-
Store.monitor.value.route = route;
57-
Store.monitor.value.path = path;
66+
// When using Monitoring without framework, we need to compute the route from the path and params
67+
if (!route && path && params) {
68+
Store.monitor.value.route = computeRoute(path, params);
69+
} else {
70+
Store.monitor.value.route = route ?? new URL(window.location.toString()).pathname;
71+
}
72+
73+
Store.monitor.value.path = path ?? new URL(window.location.toString()).pathname;
5874

5975
// eslint-disable-next-line import/no-named-as-default-member
6076
React.useEffect(() => {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ const toPayloadInteraction = (interactions: Array<InternalInteraction>) =>
7575
time: interaction.performanceEntry.duration,
7676
timestamp: interaction.performanceEntry.timestamp,
7777
type: interaction.performanceEntry.type,
78-
route: interaction.route,
7978
url: interaction.url,
79+
route: interaction.route,
80+
commit: interaction.commit,
81+
branch: interaction.branch,
8082
uniqueInteractionId: interaction.uniqueInteractionId,
8183
}) satisfies Interaction,
8284
);
@@ -100,7 +102,10 @@ export const flush = async (): Promise<void> => {
100102
return;
101103
}
102104
// idempotent
103-
const session = await getSession().catch(() => null);
105+
const session = await getSession({
106+
commit: monitor.commit,
107+
branch: monitor.branch,
108+
}).catch(() => null);
104109

105110
if (!session) return;
106111

@@ -111,6 +116,8 @@ export const flush = async (): Promise<void> => {
111116
components: aggregatedComponents,
112117
session: {
113118
...session,
119+
url: window.location.toString(),
120+
route: monitor.route, // this might be inaccurate but used to caculate which paths all the unique sessions are coming from without having to join on the interactions table (expensive)
114121
},
115122
};
116123

packages/scan/src/core/monitor/params/astro/Monitoring.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ interface Props {
99
1010
const { apiKey, url } = Astro.props;
1111
12-
const pathname = Astro.url.pathname;
12+
const path = Astro.url.path;
1313
const params = Astro.params;
1414
---
1515

16-
<AstroMonitor apiKey={apiKey} url={url} pathname={pathname} params={params} client:only="react" />
16+
<AstroMonitor apiKey={apiKey} url={url} path={pathname} params={params} client:only="react" />

packages/scan/src/core/monitor/params/astro/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { computeRoute } from '../utils';
55
export function AstroMonitor(props: {
66
url?: string;
77
apiKey: string;
8-
pathname: string;
8+
path: string;
99
params: Record<string, string>;
1010
} & MonitoringWithoutRouteProps) {
11-
const path = props.pathname;
11+
const path = props.path;
1212
const route = computeRoute(path, props.params);
1313

1414
return createElement(BaseMonitoring, {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ export function initPerformanceMonitoring(options?: Partial<PathFilters>) {
195195
componentPath: path,
196196
performanceEntry: entry,
197197
components: new Map(),
198-
route: Store.monitor.value?.route ?? null,
199198
url: window.location.toString(),
199+
route: Store.monitor.value?.route ?? null,
200+
commit: Store.monitor.value?.commit ?? null,
201+
branch: Store.monitor.value?.branch ?? null,
200202
uniqueInteractionId: entry.id,
201203
});
202204
});

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export interface Session {
1515
cpu: number;
1616
gpu: string | null;
1717
mem: number;
18+
url: string;
19+
route: string | null;
20+
commit: string | null;
21+
branch: string | null;
1822
}
1923

2024
export interface Interaction {
@@ -23,8 +27,13 @@ export interface Interaction {
2327
type: string; // type of interaction i.e pointer
2428
time: number; // time of interaction in ms
2529
timestamp: number;
26-
route: string | null; // the computed route that handles dynamic params
2730
url: string;
31+
route: string | null; // the computed route that handles dynamic params
32+
33+
// Regression tracking
34+
commit: string | null;
35+
branch: string | null;
36+
2837
// clickhouse + ingest specific types
2938
projectId?: string;
3039
sessionId?: string;
@@ -51,6 +60,8 @@ export interface InternalInteraction {
5160
componentName: string;
5261
url: string;
5362
route: string | null;
63+
commit: string | null;
64+
branch: string | null;
5465
uniqueInteractionId: string;
5566
componentPath: string;
5667
performanceEntry: PerformanceInteraction;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ const getGpuRenderer = () => {
6161
* DO NOT CALL THIS EVERYTIME
6262
*/
6363
let cachedSession: Session;
64-
export const getSession = async () => {
64+
export const getSession = async ({
65+
commit = null,
66+
branch = null,
67+
}: {
68+
commit?: string | null;
69+
branch?: string | null;
70+
}) => {
6571
if (isSSR) return null;
6672
if (cachedSession) {
6773
return cachedSession;
@@ -102,12 +108,15 @@ export const getSession = async () => {
102108
const session = {
103109
id,
104110
url,
111+
route: null,
105112
device: getDeviceType(),
106113
wifi,
107114
cpu,
108115
mem,
109116
gpu: await gpuRendererPromise,
110117
agent: navigator.userAgent,
118+
commit,
119+
branch,
111120
};
112121
cachedSession = session;
113122
return session;

0 commit comments

Comments
 (0)