Skip to content
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

ref(remix): Isolate Express instrumentation from server auto-instrumentation. #9966

Merged
merged 1 commit into from
Dec 29, 2023

Conversation

onurtemizkan
Copy link
Collaborator

@onurtemizkan onurtemizkan commented Dec 22, 2023

Potentially fixes: #9963

We have been using a boolean flag isRequestHandlerWrapped in the scope of instrumentServer 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 to loader/action/requestHandlers, to supply information about in which context a wrapped loader / action / requestHandler is used.

We still run instrumentBuild inside Sentry.init for all Express / non-Express applications, but with this context, we can gauge the behaviour of instrumented functions, depending on which instrumentBuild they're invoked from.

So, with this PR we maintain two side-effect free flags:

  • __sentry_express_wrapped__ from the instrumented getLoadContext
  • manuallyInstrumented flag we pass to instrumentBuild function which can be invoked either from Sentry.init or wrapExpressCreateRequestHandler

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 and sentry-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

Copy link
Contributor

github-actions bot commented Dec 22, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped) 75.92 KB (0%)
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 67.29 KB (0%)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 60.91 KB (0%)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.86 KB (0%)
@sentry/browser (incl. Feedback) - Webpack (gzipped) 30.42 KB (0%)
@sentry/browser - Webpack (gzipped) 22.12 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback) - ES6 CDN Bundle (gzipped) 73.34 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 65 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 31.15 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped) 23.15 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 203.87 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 93.76 KB (0%)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 68.69 KB (0%)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 34.11 KB (0%)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 67.7 KB (0%)
@sentry/react - Webpack (gzipped) 22.16 KB (0%)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 84.34 KB (0%)
@sentry/nextjs Client - Webpack (gzipped) 48.98 KB (0%)
@sentry-internal/feedback - Webpack (gzipped) 16.63 KB (0%)

@onurtemizkan onurtemizkan force-pushed the onur/fix-remix-wrapper-flag branch 6 times, most recently from 30c78ea to bb04a33 Compare December 28, 2023 19:49
@onurtemizkan onurtemizkan changed the title fix(remix): Move isRequestHandlerWrapped flag into scope. fix(remix): Isolate Express instrumentation from server auto-instrumentation. Dec 28, 2023
@onurtemizkan onurtemizkan force-pushed the onur/fix-remix-wrapper-flag branch from bb04a33 to e8a1841 Compare December 28, 2023 23:09
@onurtemizkan onurtemizkan changed the title fix(remix): Isolate Express instrumentation from server auto-instrumentation. ref(remix): Isolate Express instrumentation from server auto-instrumentation. Dec 28, 2023
@onurtemizkan onurtemizkan marked this pull request as ready for review December 28, 2023 23:21
Copy link
Member

@lforst lforst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! If, for some reason, this doesn't end up working or isn't robust enough we could investigate marking individual request objects as instrumented.

@lforst lforst merged commit 679e149 into develop Dec 29, 2023
95 checks passed
@lforst lforst deleted the onur/fix-remix-wrapper-flag branch December 29, 2023 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No automatic transactions when instrumenting Custom Remix Server
2 participants