-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathperformance.tsx
More file actions
239 lines (196 loc) · 7.19 KB
/
performance.tsx
File metadata and controls
239 lines (196 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import type { Client, StartSpanOptions } from '@sentry/core';
import {
debug,
getActiveSpan,
getCurrentScope,
getRootSpan,
isNodeEnv,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from '@sentry/core';
import type { BrowserClient, browserTracingIntegration as originalBrowserTracingIntegration } from '@sentry/react';
import { getClient, startBrowserTracingNavigationSpan, startBrowserTracingPageLoadSpan, WINDOW } from '@sentry/react';
import * as React from 'react';
import { DEBUG_BUILD } from '../utils/debug-build';
import { hasManifest, maybeParameterizeRemixRoute } from './remixRouteParameterization';
export type Params<Key extends string = string> = {
readonly [key in Key]: string | undefined;
};
export interface RouteMatch<ParamKey extends string = string> {
params: Params<ParamKey>;
pathname: string;
id: string;
handle: unknown;
}
export type UseEffect = (cb: () => void, deps: unknown[]) => void;
export type UseLocation = () => {
pathname: string;
search?: string;
hash?: string;
state?: unknown;
key?: unknown;
};
export type UseMatches = () => RouteMatch[] | null;
export type RemixBrowserTracingIntegrationOptions = Partial<Parameters<typeof originalBrowserTracingIntegration>[0]> & {
useEffect?: UseEffect;
useLocation?: UseLocation;
useMatches?: UseMatches;
};
let _useEffect: UseEffect | undefined;
let _useLocation: UseLocation | undefined;
let _useMatches: UseMatches | undefined;
let _instrumentNavigation: boolean | undefined;
function getInitPathName(): string | undefined {
if (WINDOW.location) {
return WINDOW.location.pathname;
}
return undefined;
}
/**
* Determines the transaction name and source for a route.
* Handles three cases:
* 1. Dynamic routes with manifest (Vite apps): Use parameterized path with source 'route'
* 2. Static routes with manifest (Vite apps): Use pathname with source 'url'
* 3. Legacy apps without manifest: Use route ID with source 'route'
*/
function getTransactionNameAndSource(
pathname: string | undefined,
routeId: string,
): { name: string; source: 'route' | 'url' } {
const parameterizedRoute = pathname ? maybeParameterizeRemixRoute(pathname) : undefined;
if (parameterizedRoute) {
// We have a parameterized route from the manifest (dynamic route)
return { name: parameterizedRoute, source: 'route' };
}
if (hasManifest()) {
// We have a manifest but no parameterization (static route)
// Use the pathname with source 'url'
return { name: pathname || routeId, source: 'url' };
}
// No manifest available (legacy app without Vite plugin)
// Fall back to route ID for backward compatibility
return { name: routeId, source: 'route' };
}
export function startPageloadSpan(client: Client): void {
const initPathName = getInitPathName();
if (!initPathName) {
return;
}
// Try to parameterize the route using the route manifest
const parameterizedRoute = maybeParameterizeRemixRoute(initPathName);
const spanName = parameterizedRoute || initPathName;
const source = parameterizedRoute ? 'route' : 'url';
const spanContext: StartSpanOptions = {
name: spanName,
op: 'pageload',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.remix',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
};
startBrowserTracingPageLoadSpan(client, spanContext);
}
function startNavigationSpan(matches: RouteMatch<string>[], location: ReturnType<UseLocation>): void {
const lastMatch = matches[matches.length - 1];
const client = getClient<BrowserClient>();
if (!client || !lastMatch) {
return;
}
const { name, source } = getTransactionNameAndSource(location.pathname, lastMatch.id);
const spanContext: StartSpanOptions = {
name,
op: 'navigation',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.remix',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
};
startBrowserTracingNavigationSpan(client, spanContext);
}
/**
* Wraps a remix `root` (see: https://remix.run/docs/en/main/start/quickstart#the-root-route)
* To enable pageload/navigation tracing on every route.
*
* @param OrigApp The Remix root to wrap
* @param useEffect The `useEffect` hook from `react`
* @param useLocation The `useLocation` hook from `@remix-run/react`
* @param useMatches The `useMatches` hook from `@remix-run/react`
* @param instrumentNavigation Whether to instrument navigation spans. Defaults to `true`.
*/
export function withSentry<P extends Record<string, unknown>, R extends React.ComponentType<P>>(
OrigApp: R,
useEffect?: UseEffect,
useLocation?: UseLocation,
useMatches?: UseMatches,
instrumentNavigation?: boolean,
): R {
const SentryRoot: React.FC<P> = (props: P) => {
setGlobals({ useEffect, useLocation, useMatches, instrumentNavigation: instrumentNavigation || true });
// Early return when any of the required functions is not available.
if (!_useEffect || !_useLocation || !_useMatches) {
DEBUG_BUILD &&
!isNodeEnv() &&
debug.warn('Remix SDK was unable to wrap your root because of one or more missing parameters.');
// @ts-expect-error Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params
return <OrigApp {...props} />;
}
let isBaseLocation: boolean = false;
const location = _useLocation();
const matches = _useMatches();
_useEffect(() => {
const lastMatch = matches?.[matches.length - 1];
if (lastMatch) {
const { name, source } = getTransactionNameAndSource(location.pathname, lastMatch.id);
getCurrentScope().setTransactionName(name);
const activeRootSpan = getActiveSpan();
if (activeRootSpan) {
const transaction = getRootSpan(activeRootSpan);
if (transaction) {
transaction.updateName(name);
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
}
}
}
isBaseLocation = true;
}, []);
_useEffect(() => {
const activeRootSpan = getActiveSpan();
if (isBaseLocation) {
if (activeRootSpan) {
activeRootSpan.end();
}
return;
}
if (_instrumentNavigation && matches?.length) {
if (activeRootSpan) {
activeRootSpan.end();
}
startNavigationSpan(matches, location);
}
}, [location]);
isBaseLocation = false;
// @ts-expect-error Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params
return <OrigApp {...props} />;
};
// @ts-expect-error Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params
return SentryRoot;
}
export function setGlobals({
useEffect,
useLocation,
useMatches,
instrumentNavigation,
}: {
useEffect?: UseEffect;
useLocation?: UseLocation;
useMatches?: UseMatches;
instrumentNavigation?: boolean;
}): void {
_useEffect = useEffect || _useEffect;
_useLocation = useLocation || _useLocation;
_useMatches = useMatches || _useMatches;
_instrumentNavigation = instrumentNavigation ?? _instrumentNavigation;
}