From 075de70aeb51b4f5f8dc850415a9044c71da467b Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Thu, 7 Jul 2022 17:37:19 +0100 Subject: [PATCH 1/2] fix(remix): Wrap `handleDocumentRequest` functions. --- packages/remix/src/utils/instrumentServer.ts | 83 +++++++++++++++----- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index f11eb2186ab8..fdb40cdd1b98 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -69,6 +69,65 @@ interface DataFunction { (args: DataFunctionArgs): Promise | Response | Promise | AppData; } +function captureRemixServerException(err: Error, name: string): void { + configureScope(scope => { + scope.addEventProcessor(event => { + addExceptionMechanism(event, { + type: 'instrument', + handled: true, + data: { + function: name, + }, + }); + + return event; + }); + }); + + captureException(err); +} + +function makeWrappedDocumentRequestFunction( + origDocumentRequestFunction: HandleDocumentRequestFunction, +): HandleDocumentRequestFunction { + return async function ( + this: unknown, + request: Request, + responseStatusCode: number, + responseHeaders: Headers, + context: Record, + ): Promise { + let res: Response; + + const activeTransaction = getActiveTransaction(); + const currentScope = getCurrentHub().getScope(); + + if (!activeTransaction || !currentScope) { + return origDocumentRequestFunction.call(this, request, responseStatusCode, responseHeaders, context); + } + + try { + const span = activeTransaction.startChild({ + op: 'remix.server.documentRequest', + description: activeTransaction.name, + tags: { + method: request.method, + url: request.url, + }, + }); + + res = await origDocumentRequestFunction.call(this, request, responseStatusCode, responseHeaders, context); + + span.finish(); + } catch (err) { + captureRemixServerException(err, name); + throw err; + } + + return res; + }; +} + function makeWrappedDataFunction(origFn: DataFunction, name: 'action' | 'loader'): DataFunction { return async function (this: unknown, args: DataFunctionArgs): Promise { let res: Response | AppData; @@ -98,23 +157,7 @@ function makeWrappedDataFunction(origFn: DataFunction, name: 'action' | 'loader' currentScope.setSpan(activeTransaction); span.finish(); } catch (err) { - configureScope(scope => { - scope.addEventProcessor(event => { - addExceptionMechanism(event, { - type: 'instrument', - handled: true, - data: { - function: name, - }, - }); - - return event; - }); - }); - - captureException(err); - - // Rethrow for other handlers + captureRemixServerException(err, name); throw err; } @@ -160,6 +203,10 @@ function makeWrappedCreateRequestHandler( return function (this: unknown, build: ServerBuild, mode: string | undefined): RequestHandler { const routes: ServerRouteManifest = {}; + const wrappedEntry = { ...build.entry, module: { ...build.entry.module } }; + + fill(wrappedEntry.module, 'default', makeWrappedDocumentRequestFunction); + for (const [id, route] of Object.entries(build.routes)) { const wrappedRoute = { ...route, module: { ...route.module } }; @@ -174,7 +221,7 @@ function makeWrappedCreateRequestHandler( routes[id] = wrappedRoute; } - const requestHandler = origCreateRequestHandler.call(this, { ...build, routes }, mode); + const requestHandler = origCreateRequestHandler.call(this, { ...build, routes, entry: wrappedEntry }, mode); return wrapRequestHandler(requestHandler); }; From 9ee81902cb68a756d3b76fba6d7dc8cb1a3bc4d3 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 8 Jul 2022 15:01:18 +0100 Subject: [PATCH 2/2] Update packages/remix/src/utils/instrumentServer.ts Co-authored-by: Abhijeet Prasad --- packages/remix/src/utils/instrumentServer.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index fdb40cdd1b98..662fbf775ebd 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -1,4 +1,4 @@ -import { captureException, configureScope, getCurrentHub, startTransaction } from '@sentry/node'; +import { captureException, getCurrentHub, startTransaction } from '@sentry/node'; import { getActiveTransaction } from '@sentry/tracing'; import { addExceptionMechanism, fill, loadModule, logger, stripUrlQueryAndFragment } from '@sentry/utils'; @@ -70,7 +70,7 @@ interface DataFunction { } function captureRemixServerException(err: Error, name: string): void { - configureScope(scope => { + captureException(err, scope => { scope.addEventProcessor(event => { addExceptionMechanism(event, { type: 'instrument', @@ -82,9 +82,9 @@ function captureRemixServerException(err: Error, name: string): void { return event; }); - }); - captureException(err); + return scope; + }); } function makeWrappedDocumentRequestFunction(