-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(remix): Add Worker Runtime support to Remix SDK.
- Loading branch information
1 parent
3c98e45
commit 9103c6b
Showing
17 changed files
with
421 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { ServerRuntimeClientOptions } from '@sentry/core'; | ||
import { configureScope, getCurrentHub, getIntegrationsToSetup, initAndBind, ServerRuntimeClient } from '@sentry/core'; | ||
import { createStackParser, logger, nodeStackLineParser, stackParserFromStackParserOptions } from '@sentry/utils'; | ||
|
||
import { instrumentServer } from './utils/instrumentServer'; | ||
import { buildMetadata } from './utils/metadata'; | ||
import type { RemixOptions } from './utils/remixOptions'; | ||
import { makeEdgeTransport } from './worker/transport'; | ||
|
||
export { captureRemixServerException } from './utils/instrumentServer'; | ||
export { ErrorBoundary, withErrorBoundary } from '@sentry/react'; | ||
export { remixRouterInstrumentation, withSentry } from './client/performance'; | ||
export { captureRemixErrorBoundaryError } from './client/errors'; | ||
export { wrapExpressCreateRequestHandler } from './utils/serverAdapters/express'; | ||
|
||
const nodeStackParser = createStackParser(nodeStackLineParser()); | ||
|
||
function sdkAlreadyInitialized(): boolean { | ||
const hub = getCurrentHub(); | ||
return !!hub.getClient(); | ||
} | ||
|
||
/** Initializes Sentry Remix SDK on Node. */ | ||
export function init(options: RemixOptions): void { | ||
buildMetadata(options, ['remix', 'node']); | ||
|
||
if (sdkAlreadyInitialized()) { | ||
__DEBUG_BUILD__ && logger.log('SDK already initialized'); | ||
|
||
return; | ||
} | ||
|
||
const clientOptions: ServerRuntimeClientOptions = { | ||
...options, | ||
stackParser: stackParserFromStackParserOptions(options.stackParser || nodeStackParser), | ||
integrations: getIntegrationsToSetup(options), | ||
transport: options.transport || makeEdgeTransport, | ||
}; | ||
|
||
initAndBind(ServerRuntimeClient, clientOptions); | ||
instrumentServer(); | ||
|
||
configureScope(scope => { | ||
scope.setTag('runtime', 'worker'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { getCurrentHub, hasTracingEnabled } from '@sentry/core'; | ||
import { isString, logger } from '@sentry/utils'; | ||
|
||
import { | ||
createRoutes, | ||
getTransactionName, | ||
instrumentBuild, | ||
isRequestHandlerWrapped, | ||
startRequestHandlerTransaction, | ||
} from '../instrumentServer'; | ||
import type { ReactRouterDomPkg, ServerBuild } from '../vendor/types'; | ||
|
||
type WorkerRequestHandler = (request: Request) => Promise<Response>; | ||
export type WorkerCreateRequestHandler = (this: unknown, options: any) => WorkerRequestHandler; | ||
type WorkerRequestHandlerOptions = { | ||
build: ServerBuild; | ||
mode?: string; | ||
poweredByHeader?: boolean; | ||
getLoadContext?: (request: Request) => Promise<unknown> | unknown; | ||
}; | ||
|
||
let pkg: ReactRouterDomPkg; | ||
|
||
function wrapWorkerRequestHandler(origRequestHandler: WorkerRequestHandler, build: ServerBuild): WorkerRequestHandler { | ||
const routes = createRoutes(build.routes); | ||
|
||
// If the core request handler is already wrapped, don't wrap Express handler which uses it. | ||
if (isRequestHandlerWrapped) { | ||
return origRequestHandler; | ||
} | ||
|
||
return async function (this: unknown, request: Request): Promise<Response> { | ||
if (!pkg) { | ||
try { | ||
pkg = await import('react-router-dom'); | ||
} finally { | ||
if (!pkg) { | ||
__DEBUG_BUILD__ && logger.error('Could not find `react-router-dom` package.'); | ||
} | ||
} | ||
} | ||
|
||
const hub = getCurrentHub(); | ||
const options = hub.getClient()?.getOptions(); | ||
const scope = hub.getScope(); | ||
|
||
scope.setSDKProcessingMetadata({ request }); | ||
|
||
if (!options || !hasTracingEnabled(options) || !request.url || !request.method) { | ||
return origRequestHandler.call(this, request); | ||
} | ||
|
||
const url = new URL(request.url); | ||
const [name, source] = getTransactionName(routes, url, pkg); | ||
startRequestHandlerTransaction(hub, name, source, { | ||
headers: { | ||
'sentry-trace': | ||
(request.headers && isString(request.headers.get('sentry-trace')) && request.headers.get('sentry-trace')) || | ||
'', | ||
baggage: (request.headers && isString(request.headers.get('baggage')) && request.headers.get('baggage')) || '', | ||
}, | ||
method: request.method, | ||
}); | ||
|
||
return origRequestHandler.call(this, request); | ||
}; | ||
} | ||
|
||
/** | ||
* Instruments `createRequestHandler` from `@remix-run/express` | ||
*/ | ||
export function wrapWorkerCreateRequestHandler( | ||
origCreateRequestHandler: WorkerCreateRequestHandler, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): (options: any) => WorkerRequestHandler { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return function (this: unknown, options: any): WorkerRequestHandler { | ||
const newBuild = instrumentBuild((options as WorkerRequestHandlerOptions).build); | ||
const requestHandler = origCreateRequestHandler.call(this, { ...options, build: newBuild }); | ||
|
||
return wrapWorkerRequestHandler(requestHandler, newBuild); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.