-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcreateClientInstrumentation.ts
More file actions
327 lines (290 loc) · 12.5 KB
/
Copy pathcreateClientInstrumentation.ts
File metadata and controls
327 lines (290 loc) · 12.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { startBrowserTracingNavigationSpan } from '@sentry/browser';
import type { Span } from '@sentry/core';
import {
debug,
getClient,
GLOBAL_OBJ,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { DEBUG_BUILD } from '../common/debug-build';
import type { ClientInstrumentation, InstrumentableRoute, InstrumentableRouter } from '../common/types';
import { captureInstrumentationError, getPathFromRequest, getPattern, normalizeRoutePath } from '../common/utils';
const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
// Tracks active numeric navigation span to prevent duplicate spans when popstate fires
let currentNumericNavigationSpan: Span | undefined;
const SENTRY_CLIENT_INSTRUMENTATION_FLAG = '__sentryReactRouterClientInstrumentationUsed';
// Intentionally never reset - once set, instrumentation API handles all navigations for the session.
const SENTRY_NAVIGATE_HOOK_INVOKED_FLAG = '__sentryReactRouterNavigateHookInvoked';
const SENTRY_POPSTATE_LISTENER_ADDED_FLAG = '__sentryReactRouterPopstateListenerAdded';
type GlobalObjWithFlags = typeof GLOBAL_OBJ & {
[SENTRY_CLIENT_INSTRUMENTATION_FLAG]?: boolean;
[SENTRY_NAVIGATE_HOOK_INVOKED_FLAG]?: boolean;
[SENTRY_POPSTATE_LISTENER_ADDED_FLAG]?: boolean;
};
const GLOBAL_WITH_FLAGS = GLOBAL_OBJ as GlobalObjWithFlags;
/**
* Options for creating Sentry client instrumentation.
*/
export interface CreateSentryClientInstrumentationOptions {
/**
* Whether to capture errors from loaders/actions automatically.
* Set to `false` to avoid duplicates if using custom error handlers.
* @default true
*/
captureErrors?: boolean;
}
/**
* Creates a Sentry client instrumentation for React Router's instrumentation API.
* @experimental
*/
export function createSentryClientInstrumentation(
options: CreateSentryClientInstrumentationOptions = {},
): ClientInstrumentation {
const { captureErrors = true } = options;
DEBUG_BUILD && debug.log('React Router client instrumentation API created.');
return {
router(router: InstrumentableRouter) {
// Set the flag when React Router actually invokes our instrumentation.
// This ensures the flag is only set in Library Mode (where hooks run),
// not in Framework Mode (where hooks are never called).
// See: https://github.com/remix-run/react-router/discussions/13749
GLOBAL_WITH_FLAGS[SENTRY_CLIENT_INSTRUMENTATION_FLAG] = true;
DEBUG_BUILD && debug.log('React Router client instrumentation API router hook registered.');
// Add popstate listener for browser back/forward navigation (persists for session, one listener only)
if (!GLOBAL_WITH_FLAGS[SENTRY_POPSTATE_LISTENER_ADDED_FLAG] && WINDOW.addEventListener) {
GLOBAL_WITH_FLAGS[SENTRY_POPSTATE_LISTENER_ADDED_FLAG] = true;
WINDOW.addEventListener('popstate', () => {
const client = getClient();
if (!client) {
currentNumericNavigationSpan = undefined;
return;
}
const pathname = WINDOW.location?.pathname || '/';
// If there's an active numeric navigation span, update it instead of creating a duplicate
if (currentNumericNavigationSpan) {
if (currentNumericNavigationSpan.isRecording()) {
currentNumericNavigationSpan.updateName(pathname);
}
currentNumericNavigationSpan = undefined;
return;
}
// Only create a new span for actual browser back/forward button clicks
startBrowserTracingNavigationSpan(client, {
name: pathname,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react_router.instrumentation_api',
'navigation.type': 'browser.popstate',
},
});
});
DEBUG_BUILD && debug.log('React Router popstate listener registered for browser back/forward navigation.');
}
router.instrument({
async navigate(callNavigate, info) {
// navigate(0) triggers a page reload - skip span creation, but still capture errors
// (navigation can be rejected before reload, e.g., by a navigation guard)
if (info.to === 0) {
const result = await callNavigate();
if (result.status === 'error' && result.error instanceof Error) {
captureInstrumentationError(result, captureErrors, 'react_router.navigate', {
'http.url': info.currentUrl,
});
}
return;
}
GLOBAL_WITH_FLAGS[SENTRY_NAVIGATE_HOOK_INVOKED_FLAG] = true;
// Handle numeric navigations (navigate(-1), navigate(1), etc.)
if (typeof info.to === 'number') {
const client = getClient();
let navigationSpan;
if (client) {
const navigationType = info.to < 0 ? 'router.back' : 'router.forward';
const currentPathname = WINDOW.location?.pathname || info.currentUrl;
navigationSpan = startBrowserTracingNavigationSpan(client, {
name: currentPathname,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react_router.instrumentation_api',
'navigation.type': navigationType,
},
});
// Store ref so popstate listener can update it instead of creating a duplicate
currentNumericNavigationSpan = navigationSpan;
}
try {
const result = await callNavigate();
if (navigationSpan && WINDOW.location) {
navigationSpan.updateName(WINDOW.location.pathname);
}
if (result.status === 'error' && result.error instanceof Error) {
if (navigationSpan) {
navigationSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}
captureInstrumentationError(result, captureErrors, 'react_router.navigate', {
'http.url': WINDOW.location?.pathname || info.currentUrl,
});
}
} finally {
currentNumericNavigationSpan = undefined;
}
return;
}
// Handle string navigations (e.g., navigate('/about'))
const client = getClient();
const toPath = String(info.to);
let navigationSpan;
if (client) {
navigationSpan = startBrowserTracingNavigationSpan(client, {
name: toPath,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react_router.instrumentation_api',
'navigation.type': 'router.navigate',
},
});
}
const result = await callNavigate();
if (result.status === 'error' && result.error instanceof Error) {
if (navigationSpan) {
navigationSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}
captureInstrumentationError(result, captureErrors, 'react_router.navigate', {
'http.url': toPath,
});
}
return;
},
async fetch(callFetch, info) {
await startSpan(
{
name: `Fetcher ${info.fetcherKey}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.fetcher',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.react_router.instrumentation_api',
},
},
async span => {
const result = await callFetch();
if (result.status === 'error' && result.error instanceof Error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureInstrumentationError(result, captureErrors, 'react_router.fetcher', {
'http.url': info.href,
});
}
},
);
},
});
},
route(route: InstrumentableRoute) {
route.instrument({
async loader(callLoader, info) {
const urlPath = getPathFromRequest(info.request);
const routePattern = normalizeRoutePath(getPattern(info)) || urlPath;
await startSpan(
{
name: routePattern,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.client_loader',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.react_router.instrumentation_api',
},
},
async span => {
const result = await callLoader();
if (result.status === 'error' && result.error instanceof Error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureInstrumentationError(result, captureErrors, 'react_router.client_loader', {
'http.url': urlPath,
});
}
},
);
},
async action(callAction, info) {
const urlPath = getPathFromRequest(info.request);
const routePattern = normalizeRoutePath(getPattern(info)) || urlPath;
await startSpan(
{
name: routePattern,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.client_action',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.react_router.instrumentation_api',
},
},
async span => {
const result = await callAction();
if (result.status === 'error' && result.error instanceof Error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureInstrumentationError(result, captureErrors, 'react_router.client_action', {
'http.url': urlPath,
});
}
},
);
},
async middleware(callMiddleware, info) {
const urlPath = getPathFromRequest(info.request);
const routePattern = normalizeRoutePath(getPattern(info)) || urlPath;
await startSpan(
{
name: routePattern,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.client_middleware',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.react_router.instrumentation_api',
},
},
async span => {
const result = await callMiddleware();
if (result.status === 'error' && result.error instanceof Error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureInstrumentationError(result, captureErrors, 'react_router.client_middleware', {
'http.url': urlPath,
});
}
},
);
},
async lazy(callLazy) {
await startSpan(
{
name: 'Lazy Route Load',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.client_lazy',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.react_router.instrumentation_api',
},
},
async span => {
const result = await callLazy();
if (result.status === 'error' && result.error instanceof Error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureInstrumentationError(result, captureErrors, 'react_router.client_lazy', {});
}
},
);
},
});
},
};
}
/**
* Check if React Router's instrumentation API is being used on the client.
* @experimental
*/
export function isClientInstrumentationApiUsed(): boolean {
return !!GLOBAL_WITH_FLAGS[SENTRY_CLIENT_INSTRUMENTATION_FLAG];
}
/**
* Check if React Router's instrumentation API's navigate hook was invoked.
* @experimental
*/
export function isNavigateHookInvoked(): boolean {
return !!GLOBAL_WITH_FLAGS[SENTRY_NAVIGATE_HOOK_INVOKED_FLAG];
}