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(core): Use versioned carrier on global object #12206

Merged
merged 13 commits into from
May 28, 2024

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented May 24, 2024

This PR implements a versioned Sentry carrier as described in #12188. The idea is that SDKs can from now on access their global Sentry instance and thereby no longer overwrite or interfere with potentially other SDKs (e.g. 3rd party libraries, scripts, etc).

Internally, SDKs can access their carrier via the window.__SENTRY__[SDK_VERSION]. Externally (spotlight, loader script) via window.__SENTRY__[window.__SENTRY__.version].

image

As we can see in this example, the main SDK sets its properties on the 8.4.0 key, while another, presumably older SDK sets its hub directly onto the __SENTRY__ object.

There are some caveats to consider with this approach as documented in #12188. For now though, I'd argue that the benefits (i.e. get rid of a lot of errors our users can't do much about) outweigh the potential problems.

closes #12188

also:

fixes #12155
fixes #12054

Once this is merged, we have to make updates to:

  • Spotlight, to access versioned carrier
  • The loader script to access the versioned carrier
    • I temporarily patched the loader script we use in our loader integration tests to highlight the necessary change and to get the tests passing.

Copy link
Contributor

github-actions bot commented May 24, 2024

size-limit report 📦

Path Size
@sentry/browser 21.74 KB (-0.2% 🔽)
@sentry/browser (incl. Tracing) 32.75 KB (-0.14% 🔽)
@sentry/browser (incl. Tracing, Replay) 68.21 KB (-0.11% 🔽)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.63 KB (-0.12% 🔽)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.25 KB (-0.11% 🔽)
@sentry/browser (incl. Tracing, Replay, Feedback) 84.32 KB (-0.08% 🔽)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 85.78 KB (-0.09% 🔽)
@sentry/browser (incl. metrics) 23.12 KB (-0.21% 🔽)
@sentry/browser (incl. Feedback) 37.74 KB (-0.15% 🔽)
@sentry/browser (incl. sendFeedback) 26.31 KB (-0.17% 🔽)
@sentry/browser (incl. FeedbackAsync) 30.74 KB (-0.17% 🔽)
@sentry/react 24.46 KB (-0.17% 🔽)
@sentry/react (incl. Tracing) 35.78 KB (-0.14% 🔽)
@sentry/vue 25.71 KB (-0.17% 🔽)
@sentry/vue (incl. Tracing) 34.58 KB (-0.11% 🔽)
@sentry/svelte 21.87 KB (-0.23% 🔽)
CDN Bundle 22.97 KB (-0.15% 🔽)
CDN Bundle (incl. Tracing) 34.25 KB (-0.11% 🔽)
CDN Bundle (incl. Tracing, Replay) 68.07 KB (-0.04% 🔽)
CDN Bundle (incl. Tracing, Replay, Feedback) 73.08 KB (-0.05% 🔽)
CDN Bundle - uncompressed 67.8 KB (-0.12% 🔽)
CDN Bundle (incl. Tracing) - uncompressed 101.67 KB (-0.08% 🔽)
CDN Bundle (incl. Tracing, Replay) - uncompressed 211.56 KB (-0.04% 🔽)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 223.94 KB (-0.04% 🔽)
@sentry/nextjs (client) 35.12 KB (-0.1% 🔽)
@sentry/sveltekit (client) 33.37 KB (-0.09% 🔽)
@sentry/node 114.66 KB (-0.04% 🔽)
@sentry/aws-serverless 103.37 KB (-0.01% 🔽)

@Lms24 Lms24 force-pushed the lms/ref-versioned-carrier branch from 8a2dce8 to 8a45505 Compare May 24, 2024 10:39
@Lms24
Copy link
Member Author

Lms24 commented May 24, 2024

Hmm seems like TS 3.8 isn't happy with the VersionString type alias being used in a [key: VersionString]: SentryCarrier type 😢
https://github.com/getsentry/sentry-javascript/actions/runs/9222664236/job/25374684416?pr=12206#step:11:109

Problem is, I can't use [key: string]: SentryCarrier either because it conflicts with other properties

Copy link
Member Author

Choose a reason for hiding this comment

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

This is only a temporary modification. Besides formatting the minified code, I added logic to access the versioned carrier in the loader script. We have to make this modification in the actual loader script that we ship to users.

@Lms24
Copy link
Member Author

Lms24 commented May 24, 2024

@timfish @krystofwoldrich do you see potential problems with this in Electron or RN?

@timfish
Copy link
Collaborator

timfish commented May 24, 2024

@timfish @krystofwoldrich do you see potential problems with this in Electron or RN?

No!

@Lms24 Lms24 marked this pull request as ready for review May 28, 2024 09:15
packages/core/src/carrier.ts Outdated Show resolved Hide resolved
* Sentry carrier object. This is because certain scripts (e.g. our loader script) obtain
* the client via `window.__SENTRY__.hub.getClient()`.
* the client via `window.__SENTRY__[version].stack.getClient()`.
Copy link
Member

Choose a reason for hiding this comment

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

Is this actually true going forward? I think in loader for v8 we can do this:

const sentry = window.__SENTRY__[version].stack.getClient()

So we'll have a different code path anyhow...? 🤔 do we need this for spotlight or something else?

Copy link
Member

Choose a reason for hiding this comment

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

ah wait, I see now - this just sets this on the stack 🤔 I guess that's ok for now...

Copy link
Member Author

@Lms24 Lms24 May 28, 2024

Choose a reason for hiding this comment

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

I mean we could also just set a flag, something like window.__SENTRY__[version].sdkInitialized = true instead, right? At least for the loader that's enough I think, since we only check if the client exists or not 🤔

Spotlight currently accesses window.__SENTRY__.acs which I still need to change to window.__SENTRY__[version].acs once this PR lands. So I don't think this is necessary for spotlight at all.

Copy link
Member

Choose a reason for hiding this comment

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

could we just check if window.__SENTRY__[version] exists, actually? This will only be set if the SDK was initialized I guess..? 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess our integration tests should tell us. Let me check :)

packages/core/src/sdk.ts Outdated Show resolved Hide resolved
packages/types/src/misc.ts Outdated Show resolved Hide resolved
@Lms24 Lms24 force-pushed the lms/ref-versioned-carrier branch from 8f8c99d to 9d14585 Compare May 28, 2024 11:16
Co-authored-by: Francesco Novy <francesco.novy@sentry.io>
@krystofwoldrich
Copy link
Member

@timfish @krystofwoldrich do you see potential problems with this in Electron or RN?

Nope, I don't see problems with this in RN.

@Lms24 Lms24 force-pushed the lms/ref-versioned-carrier branch from 055398f to 0c08418 Compare May 28, 2024 13:28
@Lms24 Lms24 force-pushed the lms/ref-versioned-carrier branch from 2cfc81a to 26f7d02 Compare May 28, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants