forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrumentServer.ts
313 lines (269 loc) · 10.3 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
import type { RequestEventData, WrappedFunction } from '@sentry/core';
import {
continueTrace,
fill,
getClient,
getTraceData,
hasTracingEnabled,
isNodeEnv,
loadModule,
logger,
winterCGRequestToRequestData,
withIsolationScope,
} from '@sentry/core';
import { DEBUG_BUILD } from './debug-build';
import { captureRemixServerException, errorHandleDataFunction, errorHandleDocumentRequestFunction } from './errors';
import { extractData, isDeferredData, isResponse, isRouteErrorResponse, json } from './vendor/response';
import type {
AppData,
AppLoadContext,
CreateRequestHandlerFunction,
DataFunction,
DataFunctionArgs,
EntryContext,
HandleDocumentRequestFunction,
RemixRequest,
RequestHandler,
ServerBuild,
ServerRouteManifest,
} from './vendor/types';
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;
}
/**
* 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.sentryHandleError
*/
export function sentryHandleError(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
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);
});
}
/**
* Sentry wrapper for Remix's `handleError` function.
* Remix Docs: https://remix.run/docs/en/main/file-conventions/entry.server#handleerror
*/
export function wrapHandleErrorWithSentry(
origHandleError: (err: unknown, args: { request: unknown }) => void,
): (err: unknown, args: { request: unknown }) => void {
return function (this: unknown, err: unknown, args: { request: unknown }): void {
// This is expected to be void but just in case it changes in the future.
const res = origHandleError.call(this, err, args);
sentryHandleError(err, args as DataFunctionArgs);
return res;
};
}
function getTraceAndBaggage(): {
sentryTrace?: string;
sentryBaggage?: string;
} {
if (isNodeEnv()) {
const traceData = getTraceData();
return {
sentryTrace: traceData['sentry-trace'],
sentryBaggage: traceData.baggage,
};
}
return {};
}
function makeWrappedDocumentRequestFunction() {
return function (origDocumentRequestFunction: HandleDocumentRequestFunction): HandleDocumentRequestFunction {
return async function (
this: unknown,
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
context: EntryContext,
loadContext?: Record<string, unknown>,
): Promise<Response> {
return errorHandleDocumentRequestFunction.call(this, origDocumentRequestFunction, {
request,
responseStatusCode,
responseHeaders,
context,
loadContext,
});
};
};
}
function makeWrappedDataFunction(origFn: DataFunction, id: string, name: 'action' | 'loader'): DataFunction {
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
return errorHandleDataFunction.call(this, origFn, name, args);
};
}
const makeWrappedAction =
(id: string) =>
(origAction: DataFunction): DataFunction => {
return makeWrappedDataFunction(origAction, id, 'action');
};
const makeWrappedLoader =
(id: string) =>
(origLoader: DataFunction): DataFunction => {
return makeWrappedDataFunction(origLoader, id, 'loader');
};
function makeWrappedRootLoader() {
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;
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 },
{
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 };
};
};
}
function wrapRequestHandler(origRequestHandler: RequestHandler): RequestHandler {
return async function (this: unknown, request: RemixRequest, loadContext?: AppLoadContext): Promise<Response> {
const upperCaseMethod = request.method.toUpperCase();
// We don't want to wrap OPTIONS and HEAD requests
if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') {
return origRequestHandler.call(this, request, loadContext);
}
return withIsolationScope(async isolationScope => {
const options = getClient()?.getOptions();
let normalizedRequest: RequestEventData = {};
try {
normalizedRequest = winterCGRequestToRequestData(request);
} catch (e) {
DEBUG_BUILD && logger.warn('Failed to normalize Remix request');
}
isolationScope.setSDKProcessingMetadata({ normalizedRequest });
if (!options || !hasTracingEnabled(options)) {
return origRequestHandler.call(this, request, loadContext);
}
return continueTrace(
{
sentryTrace: request.headers.get('sentry-trace') || '',
baggage: request.headers.get('baggage') || '',
},
async () => {
return (await origRequestHandler.call(this, request, loadContext)) as Response;
},
);
});
};
}
function instrumentBuildCallback(build: ServerBuild): ServerBuild {
const routes: ServerRouteManifest = {};
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());
}
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));
}
const routeLoader = wrappedRoute.module.loader as undefined | WrappedFunction;
if (routeLoader && !routeLoader.__sentry_original__) {
fill(wrappedRoute.module, 'loader', makeWrappedLoader(id));
}
// 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());
}
routes[id] = wrappedRoute;
}
return { ...build, routes, entry: wrappedEntry };
}
/**
* Instruments `remix` ServerBuild for performance tracing and error tracking.
*/
export function instrumentBuild(
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
): ServerBuild | (() => ServerBuild | Promise<ServerBuild>) {
if (typeof build === 'function') {
return function () {
const resolvedBuild = build();
if (resolvedBuild instanceof Promise) {
return resolvedBuild.then(build => {
return instrumentBuildCallback(build);
});
} else {
return instrumentBuildCallback(resolvedBuild);
}
};
} else {
return instrumentBuildCallback(build);
}
}
const makeWrappedCreateRequestHandler = () =>
function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction {
return function (
this: unknown,
build: ServerBuild | (() => Promise<ServerBuild>),
...args: unknown[]
): RequestHandler {
const newBuild = instrumentBuild(build);
const requestHandler = origCreateRequestHandler.call(this, newBuild, ...args);
return wrapRequestHandler(requestHandler);
};
};
/**
* 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', module);
if (!pkg) {
DEBUG_BUILD && logger.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');
return;
}
fill(pkg, 'createRequestHandler', makeWrappedCreateRequestHandler());
}