-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
instrumentServer.ts
594 lines (508 loc) · 18.6 KB
/
instrumentServer.ts
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* eslint-disable max-lines */
import {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
getActiveSpan,
getActiveTransaction,
getClient,
getCurrentScope,
getDynamicSamplingContextFromSpan,
hasTracingEnabled,
runWithAsyncContext,
setHttpStatus,
spanToJSON,
spanToTraceHeader,
} from '@sentry/core';
import type { Hub } from '@sentry/node';
import { captureException, getCurrentHub } from '@sentry/node';
import type { Transaction, TransactionSource, WrappedFunction } from '@sentry/types';
import {
addExceptionMechanism,
dynamicSamplingContextToSentryBaggageHeader,
fill,
isNodeEnv,
isPrimitive,
loadModule,
logger,
objectify,
tracingContextFromHeaders,
} from '@sentry/utils';
import { DEBUG_BUILD } from './debug-build';
import { getFutureFlagsServer, getRemixVersionFromBuild } from './futureFlags';
import { loadRemixRouterModule } from './routerLoader';
import {
extractData,
getRequestMatch,
isDeferredData,
isResponse,
isRouteErrorResponse,
json,
matchServerRoutes,
} from './vendor/response';
import type {
AppData,
AppLoadContext,
CreateRequestHandlerFunction,
DataFunction,
DataFunctionArgs,
EntryContext,
FutureConfig,
HandleDocumentRequestFunction,
ReactRouterDomPkg,
RemixRequest,
RequestHandler,
ServerBuild,
ServerRoute,
ServerRouteManifest,
} from './vendor/types';
import { normalizeRemixRequest } from './web-fetch';
let FUTURE_FLAGS: FutureConfig | undefined;
let IS_REMIX_V2: boolean | undefined;
let pkg: ReactRouterDomPkg | undefined;
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
function isRedirectResponse(response: Response): boolean {
return redirectStatusCodes.has(response.status);
}
function isCatchResponse(response: Response): boolean {
return response.headers.get('X-Remix-Catch') != null;
}
async function extractResponseError(response: Response): Promise<unknown> {
const responseData = await extractData(response);
if (typeof responseData === 'string' && responseData.length > 0) {
return new Error(responseData);
}
if (response.statusText) {
return new Error(response.statusText);
}
return responseData;
}
/**
* Sentry utility to be used in place of `handleError` function of Remix v2
* Remix Docs: https://remix.run/docs/en/main/file-conventions/entry.server#handleerror
*
* Should be used in `entry.server` like:
*
* export const handleError = Sentry.wrapRemixHandleError
*/
export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs): void {
// We are skipping thrown responses here as they are handled by
// `captureRemixServerException` at loader / action level
// We don't want to capture them twice.
// This function is only for capturing unhandled server-side exceptions.
// https://remix.run/docs/en/main/file-conventions/entry.server#thrown-responses
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
if (isResponse(err) || isRouteErrorResponse(err)) {
return;
}
captureRemixServerException(err, 'remix.server.handleError', request).then(null, e => {
DEBUG_BUILD && logger.warn('Failed to capture Remix Server exception.', e);
});
}
/**
* Captures an exception happened in the Remix server.
*
* @param err The error to capture.
* @param name The name of the origin function.
* @param request The request object.
*
* @returns A promise that resolves when the exception is captured.
*/
export async function captureRemixServerException(err: unknown, name: string, request: Request): Promise<void> {
// Skip capturing if the thrown error is not a 5xx response
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
if (IS_REMIX_V2 && isRouteErrorResponse(err) && err.status < 500) {
return;
}
if (isResponse(err) && err.status < 500) {
return;
}
// Skip capturing if the request is aborted as Remix docs suggest
// Ref: https://remix.run/docs/en/main/file-conventions/entry.server#handleerror
if (request.signal.aborted) {
DEBUG_BUILD && logger.warn('Skipping capture of aborted request');
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let normalizedRequest: Record<string, unknown> = request as unknown as any;
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
normalizedRequest = normalizeRemixRequest(request as unknown as any);
} catch (e) {
DEBUG_BUILD && logger.warn('Failed to normalize Remix request');
}
const objectifiedErr = objectify(err);
captureException(isResponse(objectifiedErr) ? await extractResponseError(objectifiedErr) : objectifiedErr, scope => {
// eslint-disable-next-line deprecation/deprecation
const transaction = getActiveTransaction();
const activeTransactionName = transaction ? spanToJSON(transaction).description : undefined;
scope.setSDKProcessingMetadata({
request: {
...normalizedRequest,
// When `route` is not defined, `RequestData` integration uses the full URL
route: activeTransactionName
? {
path: activeTransactionName,
}
: undefined,
},
});
scope.addEventProcessor(event => {
addExceptionMechanism(event, {
type: 'instrument',
handled: false,
data: {
function: name,
},
});
return event;
});
return scope;
});
}
function makeWrappedDocumentRequestFunction(remixVersion?: number) {
return function (origDocumentRequestFunction: HandleDocumentRequestFunction): HandleDocumentRequestFunction {
return async function (
this: unknown,
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
context: EntryContext,
loadContext?: Record<string, unknown>,
): Promise<Response> {
let res: Response;
// eslint-disable-next-line deprecation/deprecation
const activeTransaction = getActiveTransaction();
try {
// eslint-disable-next-line deprecation/deprecation
const span = activeTransaction?.startChild({
op: 'function.remix.document_request',
origin: 'auto.function.remix',
description: spanToJSON(activeTransaction).description,
tags: {
method: request.method,
url: request.url,
},
});
res = await origDocumentRequestFunction.call(
this,
request,
responseStatusCode,
responseHeaders,
context,
loadContext,
);
span?.end();
} catch (err) {
const isRemixV1 = !FUTURE_FLAGS?.v2_errorBoundary && remixVersion !== 2;
// This exists to capture the server-side rendering errors on Remix v1
// On Remix v2, we capture SSR errors at `handleError`
// We also skip primitives here, as we can't dedupe them, and also we don't expect any primitive SSR errors.
if (isRemixV1 && !isPrimitive(err)) {
await captureRemixServerException(err, 'documentRequest', request);
}
throw err;
}
return res;
};
};
}
function makeWrappedDataFunction(
origFn: DataFunction,
id: string,
name: 'action' | 'loader',
remixVersion: number,
manuallyInstrumented: boolean,
): DataFunction {
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
if (args.context.__sentry_express_wrapped__ && !manuallyInstrumented) {
return origFn.call(this, args);
}
let res: Response | AppData;
// eslint-disable-next-line deprecation/deprecation
const activeTransaction = getActiveTransaction();
const currentScope = getCurrentScope();
try {
// eslint-disable-next-line deprecation/deprecation
const span = activeTransaction?.startChild({
op: `function.remix.${name}`,
origin: 'auto.ui.remix',
description: id,
tags: {
name,
},
});
if (span) {
// Assign data function to hub to be able to see `db` transactions (if any) as children.
// eslint-disable-next-line deprecation/deprecation
currentScope.setSpan(span);
}
res = await origFn.call(this, args);
// eslint-disable-next-line deprecation/deprecation
currentScope.setSpan(activeTransaction);
span?.end();
} catch (err) {
const isRemixV2 = FUTURE_FLAGS?.v2_errorBoundary || remixVersion === 2;
// On Remix v2, we capture all unexpected errors (except the `Route Error Response`s / Thrown Responses) in `handleError` function.
// This is both for consistency and also avoid duplicates such as primitives like `string` or `number` being captured twice.
// Remix v1 does not have a `handleError` function, so we capture all errors here.
if (isRemixV2 ? isResponse(err) : true) {
await captureRemixServerException(err, name, args.request);
}
throw err;
}
return res;
};
}
const makeWrappedAction =
(id: string, remixVersion: number, manuallyInstrumented: boolean) =>
(origAction: DataFunction): DataFunction => {
return makeWrappedDataFunction(origAction, id, 'action', remixVersion, manuallyInstrumented);
};
const makeWrappedLoader =
(id: string, remixVersion: number, manuallyInstrumented: boolean) =>
(origLoader: DataFunction): DataFunction => {
return makeWrappedDataFunction(origLoader, id, 'loader', remixVersion, manuallyInstrumented);
};
function getTraceAndBaggage(): {
sentryTrace?: string;
sentryBaggage?: string;
} {
// eslint-disable-next-line deprecation/deprecation
const transaction = getActiveTransaction();
if (isNodeEnv() && hasTracingEnabled()) {
const span = getActiveSpan();
if (span && transaction) {
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction);
return {
sentryTrace: spanToTraceHeader(span),
sentryBaggage: dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext),
};
}
}
return {};
}
function makeWrappedRootLoader(remixVersion: number) {
return function (origLoader: DataFunction): DataFunction {
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
const res = await origLoader.call(this, args);
const traceAndBaggage = getTraceAndBaggage();
if (isDeferredData(res)) {
res.data['sentryTrace'] = traceAndBaggage.sentryTrace;
res.data['sentryBaggage'] = traceAndBaggage.sentryBaggage;
res.data['remixVersion'] = remixVersion;
return res;
}
if (isResponse(res)) {
// Note: `redirect` and `catch` responses do not have bodies to extract.
// We skip injection of trace and baggage in those cases.
// For `redirect`, a valid internal redirection target will have the trace and baggage injected.
if (isRedirectResponse(res) || isCatchResponse(res)) {
DEBUG_BUILD && logger.warn('Skipping injection of trace and baggage as the response does not have a body');
return res;
} else {
const data = await extractData(res);
if (typeof data === 'object') {
return json(
{ ...data, ...traceAndBaggage, remixVersion },
{
headers: res.headers,
statusText: res.statusText,
status: res.status,
},
);
} else {
DEBUG_BUILD && logger.warn('Skipping injection of trace and baggage as the response body is not an object');
return res;
}
}
}
return { ...res, ...traceAndBaggage, remixVersion };
};
};
}
/**
* Creates routes from the server route manifest
*
* @param manifest
* @param parentId
*/
export function createRoutes(manifest: ServerRouteManifest, parentId?: string): ServerRoute[] {
return Object.entries(manifest)
.filter(([, route]) => route.parentId === parentId)
.map(([id, route]) => ({
...route,
children: createRoutes(manifest, id),
}));
}
/**
* Starts a new transaction for the given request to be used by different `RequestHandler` wrappers.
*
* @param request
* @param routes
* @param pkg
*/
export function startRequestHandlerTransaction(
hub: Hub,
name: string,
source: TransactionSource,
request: {
headers: {
'sentry-trace': string;
baggage: string;
};
method: string;
},
): Transaction {
// eslint-disable-next-line deprecation/deprecation
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
request.headers['sentry-trace'],
request.headers.baggage,
);
// eslint-disable-next-line deprecation/deprecation
hub.getScope().setPropagationContext(propagationContext);
// TODO: Refactor this to `startSpan()`
// eslint-disable-next-line deprecation/deprecation
const transaction = hub.startTransaction({
name,
op: 'http.server',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.remix',
},
tags: {
method: request.method,
},
...traceparentData,
metadata: {
source,
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
},
});
// eslint-disable-next-line deprecation/deprecation
hub.getScope().setSpan(transaction);
return transaction;
}
/**
* Get transaction name from routes and url
*/
export function getTransactionName(
routes: ServerRoute[],
url: URL,
pkg?: ReactRouterDomPkg,
): [string, TransactionSource] {
const matches = matchServerRoutes(routes, url.pathname, pkg);
const match = matches && getRequestMatch(url, matches);
return match === null ? [url.pathname, 'url'] : [match.route.id, 'route'];
}
function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBuild): RequestHandler {
const routes = createRoutes(build.routes);
return async function (this: unknown, request: RemixRequest, loadContext?: AppLoadContext): Promise<Response> {
// This means that the request handler of the adapter (ex: express) is already wrapped.
// So we don't want to double wrap it.
if (loadContext?.__sentry_express_wrapped__) {
return origRequestHandler.call(this, request, loadContext);
}
if (!pkg) {
pkg = await loadRemixRouterModule();
}
return runWithAsyncContext(async () => {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
const options = getClient()?.getOptions();
const scope = getCurrentScope();
let normalizedRequest: Record<string, unknown> = request;
try {
normalizedRequest = normalizeRemixRequest(request);
} catch (e) {
DEBUG_BUILD && logger.warn('Failed to normalize Remix request');
}
const url = new URL(request.url);
const [name, source] = getTransactionName(routes, url, pkg);
scope.setSDKProcessingMetadata({
request: {
...normalizedRequest,
route: {
path: name,
},
},
});
if (!options || !hasTracingEnabled(options)) {
return origRequestHandler.call(this, request, loadContext);
}
const transaction = startRequestHandlerTransaction(hub, name, source, {
headers: {
'sentry-trace': request.headers.get('sentry-trace') || '',
baggage: request.headers.get('baggage') || '',
},
method: request.method,
});
const res = (await origRequestHandler.call(this, request, loadContext)) as Response;
if (isResponse(res)) {
setHttpStatus(transaction, res.status);
}
transaction.end();
return res;
});
};
}
/**
* Instruments `remix` ServerBuild for performance tracing and error tracking.
*/
export function instrumentBuild(build: ServerBuild, manuallyInstrumented: boolean = false): ServerBuild {
const routes: ServerRouteManifest = {};
const remixVersion = getRemixVersionFromBuild(build);
IS_REMIX_V2 = remixVersion === 2;
const wrappedEntry = { ...build.entry, module: { ...build.entry.module } };
// Not keeping boolean flags like it's done for `requestHandler` functions,
// Because the build can change between build and runtime.
// So if there is a new `loader` or`action` or `documentRequest` after build.
// We should be able to wrap them, as they may not be wrapped before.
const defaultExport = wrappedEntry.module.default as undefined | WrappedFunction;
if (defaultExport && !defaultExport.__sentry_original__) {
fill(wrappedEntry.module, 'default', makeWrappedDocumentRequestFunction(remixVersion));
}
for (const [id, route] of Object.entries(build.routes)) {
const wrappedRoute = { ...route, module: { ...route.module } };
const routeAction = wrappedRoute.module.action as undefined | WrappedFunction;
if (routeAction && !routeAction.__sentry_original__) {
fill(wrappedRoute.module, 'action', makeWrappedAction(id, remixVersion, manuallyInstrumented));
}
const routeLoader = wrappedRoute.module.loader as undefined | WrappedFunction;
if (routeLoader && !routeLoader.__sentry_original__) {
fill(wrappedRoute.module, 'loader', makeWrappedLoader(id, remixVersion, manuallyInstrumented));
}
// Entry module should have a loader function to provide `sentry-trace` and `baggage`
// They will be available for the root `meta` function as `data.sentryTrace` and `data.sentryBaggage`
if (!wrappedRoute.parentId) {
if (!wrappedRoute.module.loader) {
wrappedRoute.module.loader = () => ({});
}
// We want to wrap the root loader regardless of whether it's already wrapped before.
fill(wrappedRoute.module, 'loader', makeWrappedRootLoader(remixVersion));
}
routes[id] = wrappedRoute;
}
return { ...build, routes, entry: wrappedEntry };
}
function makeWrappedCreateRequestHandler(
origCreateRequestHandler: CreateRequestHandlerFunction,
): CreateRequestHandlerFunction {
return function (this: unknown, build: ServerBuild, ...args: unknown[]): RequestHandler {
FUTURE_FLAGS = getFutureFlagsServer(build);
const newBuild = instrumentBuild(build, false);
const requestHandler = origCreateRequestHandler.call(this, newBuild, ...args);
return wrapRequestHandler(requestHandler, newBuild);
};
}
/**
* Monkey-patch Remix's `createRequestHandler` from `@remix-run/server-runtime`
* which Remix Adapters (https://remix.run/docs/en/v1/api/remix) use underneath.
*/
export function instrumentServer(): void {
const pkg = loadModule<{
createRequestHandler: CreateRequestHandlerFunction;
}>('@remix-run/server-runtime');
if (!pkg) {
DEBUG_BUILD && logger.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');
return;
}
fill(pkg, 'createRequestHandler', makeWrappedCreateRequestHandler);
}