|
| 1 | +import { captureException, configureScope, getCurrentHub, startTransaction } from '@sentry/node'; |
| 2 | +import { getActiveTransaction } from '@sentry/tracing'; |
| 3 | +import { addExceptionMechanism, fill, loadModule, logger } from '@sentry/utils'; |
| 4 | + |
| 5 | +// Types vendored from @remix-run/[email protected]: |
| 6 | +// https://github.com/remix-run/remix/blob/f3691d51027b93caa3fd2cdfe146d7b62a6eb8f2/packages/remix-server-runtime/server.ts |
| 7 | +type AppLoadContext = unknown; |
| 8 | +type AppData = unknown; |
| 9 | +type RequestHandler = (request: Request, loadContext?: AppLoadContext) => Promise<Response>; |
| 10 | +type CreateRequestHandlerFunction = (build: ServerBuild, mode?: string) => RequestHandler; |
| 11 | +type ServerRouteManifest = RouteManifest<Omit<ServerRoute, 'children'>>; |
| 12 | +type Params<Key extends string = string> = { |
| 13 | + readonly [key in Key]: string | undefined; |
| 14 | +}; |
| 15 | + |
| 16 | +interface Route { |
| 17 | + index?: boolean; |
| 18 | + caseSensitive?: boolean; |
| 19 | + id: string; |
| 20 | + parentId?: string; |
| 21 | + path?: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface ServerRouteModule { |
| 25 | + action?: DataFunction; |
| 26 | + headers?: unknown; |
| 27 | + loader?: DataFunction; |
| 28 | +} |
| 29 | + |
| 30 | +interface ServerRoute extends Route { |
| 31 | + children: ServerRoute[]; |
| 32 | + module: ServerRouteModule; |
| 33 | +} |
| 34 | + |
| 35 | +interface RouteManifest<Route> { |
| 36 | + [routeId: string]: Route; |
| 37 | +} |
| 38 | + |
| 39 | +interface ServerBuild { |
| 40 | + entry: { |
| 41 | + module: ServerEntryModule; |
| 42 | + }; |
| 43 | + routes: ServerRouteManifest; |
| 44 | + assets: unknown; |
| 45 | +} |
| 46 | + |
| 47 | +interface HandleDocumentRequestFunction { |
| 48 | + (request: Request, responseStatusCode: number, responseHeaders: Headers, context: Record<symbol, unknown>): |
| 49 | + | Promise<Response> |
| 50 | + | Response; |
| 51 | +} |
| 52 | + |
| 53 | +interface HandleDataRequestFunction { |
| 54 | + (response: Response, args: DataFunctionArgs): Promise<Response> | Response; |
| 55 | +} |
| 56 | + |
| 57 | +interface ServerEntryModule { |
| 58 | + default: HandleDocumentRequestFunction; |
| 59 | + handleDataRequest?: HandleDataRequestFunction; |
| 60 | +} |
| 61 | + |
| 62 | +interface DataFunctionArgs { |
| 63 | + request: Request; |
| 64 | + context: AppLoadContext; |
| 65 | + params: Params; |
| 66 | +} |
| 67 | + |
| 68 | +interface DataFunction { |
| 69 | + (args: DataFunctionArgs): Promise<Response> | Response | Promise<AppData> | AppData; |
| 70 | +} |
| 71 | + |
| 72 | +function makeWrappedDataFunction(origFn: DataFunction, name: 'action' | 'loader'): DataFunction { |
| 73 | + return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> { |
| 74 | + let res: Response | AppData; |
| 75 | + const activeTransaction = getActiveTransaction(); |
| 76 | + const currentScope = getCurrentHub().getScope(); |
| 77 | + |
| 78 | + if (!activeTransaction || !currentScope) { |
| 79 | + return origFn.call(this, args); |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + const span = activeTransaction.startChild({ |
| 84 | + op: `remix.server.${name}`, |
| 85 | + description: activeTransaction.name, |
| 86 | + tags: { |
| 87 | + name, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + if (span) { |
| 92 | + // Assign data function to hub to be able to see `db` transactions (if any) as children. |
| 93 | + currentScope.setSpan(span); |
| 94 | + } |
| 95 | + |
| 96 | + res = await origFn.call(this, args); |
| 97 | + |
| 98 | + currentScope.setSpan(activeTransaction); |
| 99 | + span.finish(); |
| 100 | + } catch (err) { |
| 101 | + configureScope(scope => { |
| 102 | + scope.addEventProcessor(event => { |
| 103 | + addExceptionMechanism(event, { |
| 104 | + type: 'instrument', |
| 105 | + handled: true, |
| 106 | + data: { |
| 107 | + function: name, |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + return event; |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + captureException(err); |
| 116 | + |
| 117 | + // Rethrow for other handlers |
| 118 | + throw err; |
| 119 | + } |
| 120 | + |
| 121 | + return res; |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +function makeWrappedAction(origAction: DataFunction): DataFunction { |
| 126 | + return makeWrappedDataFunction(origAction, 'action'); |
| 127 | +} |
| 128 | + |
| 129 | +function makeWrappedLoader(origAction: DataFunction): DataFunction { |
| 130 | + return makeWrappedDataFunction(origAction, 'loader'); |
| 131 | +} |
| 132 | + |
| 133 | +function wrapRequestHandler(origRequestHandler: RequestHandler): RequestHandler { |
| 134 | + return async function (this: unknown, request: Request, loadContext?: unknown): Promise<Response> { |
| 135 | + const currentScope = getCurrentHub().getScope(); |
| 136 | + const transaction = startTransaction({ |
| 137 | + name: request.url, |
| 138 | + op: 'http.server', |
| 139 | + tags: { |
| 140 | + method: request.method, |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + if (transaction) { |
| 145 | + currentScope?.setSpan(transaction); |
| 146 | + } |
| 147 | + |
| 148 | + const res = (await origRequestHandler.call(this, request, loadContext)) as Response; |
| 149 | + |
| 150 | + transaction?.setHttpStatus(res.status); |
| 151 | + transaction?.finish(); |
| 152 | + |
| 153 | + return res; |
| 154 | + }; |
| 155 | +} |
| 156 | + |
| 157 | +function makeWrappedCreateRequestHandler( |
| 158 | + origCreateRequestHandler: CreateRequestHandlerFunction, |
| 159 | +): CreateRequestHandlerFunction { |
| 160 | + return function (this: unknown, build: ServerBuild, mode: string | undefined): RequestHandler { |
| 161 | + const routes: ServerRouteManifest = {}; |
| 162 | + |
| 163 | + for (const [id, route] of Object.entries(build.routes)) { |
| 164 | + const wrappedRoute = { ...route, module: { ...route.module } }; |
| 165 | + |
| 166 | + if (wrappedRoute.module.action) { |
| 167 | + fill(wrappedRoute.module, 'action', makeWrappedAction); |
| 168 | + } |
| 169 | + |
| 170 | + if (wrappedRoute.module.loader) { |
| 171 | + fill(wrappedRoute.module, 'loader', makeWrappedLoader); |
| 172 | + } |
| 173 | + |
| 174 | + routes[id] = wrappedRoute; |
| 175 | + } |
| 176 | + |
| 177 | + const requestHandler = origCreateRequestHandler.call(this, { ...build, routes }, mode); |
| 178 | + |
| 179 | + return wrapRequestHandler(requestHandler); |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Monkey-patch Remix's `createRequestHandler` from `@remix-run/server-runtime` |
| 185 | + * which Remix Adapters (https://remix.run/docs/en/v1/api/remix) use underneath. |
| 186 | + */ |
| 187 | +export function instrumentServer(): void { |
| 188 | + const pkg = loadModule<{ createRequestHandler: CreateRequestHandlerFunction }>('@remix-run/server-runtime'); |
| 189 | + |
| 190 | + if (!pkg) { |
| 191 | + __DEBUG_BUILD__ && logger.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.'); |
| 192 | + |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + fill(pkg, 'createRequestHandler', makeWrappedCreateRequestHandler); |
| 197 | +} |
0 commit comments