ref(remix): Isolate Express instrumentation from server auto-instrumentation. #9966
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.
Potentially fixes: #9963
We have been using a boolean flag
isRequestHandlerWrapped
in the scope ofinstrumentServer
to avoid instrumenting Remix build multiple times when an Express server adapter is manually instrumented too.The reason behind that decision was that we could not be sure that the
build
object is the same, so looking for__sentry_original__
on the wrapped function was not reliable all the time.While we still can't reproduce locally, #9963 showed that this strategy (hack) may not work all the time, so I tried to find an alternative reliable way to resolve this.
This PR introduces instrumentation for
getLoadContext
which is available in all Remix server adapters, and passes data toloader
/action
/requestHandler
s, to supply information about in which context a wrappedloader
/action
/requestHandler
is used.We still run
instrumentBuild
insideSentry.init
for all Express / non-Express applications, but with this context, we can gauge the behaviour of instrumented functions, depending on whichinstrumentBuild
they're invoked from.So, with this PR we maintain two side-effect free flags:
__sentry_express_wrapped__
from the instrumentedgetLoadContext
manuallyInstrumented
flag we pass toinstrumentBuild
function which can be invoked either fromSentry.init
orwrapExpressCreateRequestHandler
Having those two, an instrumented
loader
/action
/requestHandler
will behave accordingly to avoid creating multiple transactions.This approach can also be applied to injecting
baggage
andsentry-trace
as mentioned in #9737 in a new PR, if this gets merged.Additionally, this PR makes a slight change on the ordering of
host
lookup of RequestData, which I came across while testing this.Still not sure if this fixes the exact problem mentioned in #9963, but regardless IMHO it feels like a better resolution for this problem. cc: @souredoutlook