-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(remix): Provide sentry-trace
and baggage
via root loader.
#5509
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5d2c95
fix(remix): Provide `sentry-trace` and `baggage` via root loader.
onurtemizkan 019d791
Use root wrapper after `loader` wrapper to prevent multiple `loader` …
onurtemizkan f62e2c3
Update integration tests.
onurtemizkan be37bff
Update packages/remix/src/utils/instrumentServer.ts
onurtemizkan 0290aaa
Address review comments.
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* eslint-disable max-lines */ | ||
import { captureException, getCurrentHub } from '@sentry/node'; | ||
import { getActiveTransaction } from '@sentry/tracing'; | ||
import { addExceptionMechanism, fill, loadModule, logger, serializeBaggage } from '@sentry/utils'; | ||
import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing'; | ||
import { addExceptionMechanism, fill, isNodeEnv, loadModule, logger, serializeBaggage } from '@sentry/utils'; | ||
|
||
// Types vendored from @remix-run/[email protected]: | ||
// https://github.com/remix-run/remix/blob/f3691d51027b93caa3fd2cdfe146d7b62a6eb8f2/packages/remix-server-runtime/server.ts | ||
|
@@ -72,6 +72,8 @@ interface HandleDataRequestFunction { | |
|
||
interface ServerEntryModule { | ||
default: HandleDocumentRequestFunction; | ||
meta: MetaFunction; | ||
loader: DataFunction; | ||
handleDataRequest?: HandleDataRequestFunction; | ||
} | ||
|
||
|
@@ -237,33 +239,31 @@ function makeWrappedLoader(origAction: DataFunction): DataFunction { | |
return makeWrappedDataFunction(origAction, 'loader'); | ||
} | ||
|
||
function makeWrappedMeta(origMeta: MetaFunction | HtmlMetaDescriptor = {}): MetaFunction { | ||
return function ( | ||
this: unknown, | ||
args: { data: AppData; parentsData: RouteData; params: Params; location: Location }, | ||
): HtmlMetaDescriptor { | ||
let origMetaResult; | ||
if (origMeta instanceof Function) { | ||
origMetaResult = origMeta.call(this, args); | ||
} else { | ||
origMetaResult = origMeta; | ||
} | ||
function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string } { | ||
const transaction = getActiveTransaction(); | ||
const currentScope = getCurrentHub().getScope(); | ||
|
||
const scope = getCurrentHub().getScope(); | ||
if (scope) { | ||
const span = scope.getSpan(); | ||
const transaction = getActiveTransaction(); | ||
if (isNodeEnv() && hasTracingEnabled()) { | ||
if (currentScope) { | ||
const span = currentScope.getSpan(); | ||
|
||
if (span && transaction) { | ||
return { | ||
...origMetaResult, | ||
'sentry-trace': span.toTraceparent(), | ||
baggage: serializeBaggage(transaction.getBaggage()), | ||
sentryTrace: span.toTraceparent(), | ||
sentryBaggage: serializeBaggage(transaction.getBaggage()), | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return origMetaResult; | ||
return {}; | ||
} | ||
|
||
function makeWrappedRootLoader(origLoader: DataFunction): DataFunction { | ||
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> { | ||
const res = await origLoader.call(this, args); | ||
|
||
return { ...res, ...getTraceAndBaggage() }; | ||
}; | ||
} | ||
|
||
|
@@ -378,8 +378,6 @@ function makeWrappedCreateRequestHandler( | |
for (const [id, route] of Object.entries(build.routes)) { | ||
const wrappedRoute = { ...route, module: { ...route.module } }; | ||
|
||
fill(wrappedRoute.module, 'meta', makeWrappedMeta); | ||
|
||
if (wrappedRoute.module.action) { | ||
fill(wrappedRoute.module, 'action', makeWrappedAction); | ||
} | ||
|
@@ -388,6 +386,16 @@ function makeWrappedCreateRequestHandler( | |
fill(wrappedRoute.module, 'loader', makeWrappedLoader); | ||
} | ||
|
||
// 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 = () => ({}); | ||
} | ||
|
||
fill(wrappedRoute.module, 'loader', makeWrappedRootLoader); | ||
} | ||
|
||
routes[id] = wrappedRoute; | ||
} | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do we do here for TypeScript users? Won't they get type errors if they attempt to access
data.sentryTrace
? Should we also export a type they can use?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked, Remix declares the
data
property asany
. So it's not currently breaking TS.