Database integrations are auto-loaded from @sentry/tracing when testing frontend projects
Is there an existing issue for this?
- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [X] I have reviewed the documentation https://docs.sentry.io/
- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
@sentry/angular
SDK Version
7.2.0
Framework Version
No response
Link to Sentry event
No response
Steps to Reproduce
No exact reproduction yet. This behaviour is noticed by Jest because it detects one of our DB integrations (Mongoose). However, impact is unclear: Does the behaviour break test suite execution or is it just showing a warning/info message?
Expected Result
When a Frontend JS SDK (i.e. @sentry/browser and derivatives) is used in conjunction with @sentry/tracing, _autoloadDatabaseIntegrations should not be called and hence, database integrations should not be set up.
Actual Result
As recently brought to our attention, our Frontend SDKs (for sure the Angular SDK but probably most other frontend JS SDKs, too) are auto-discovering database integrations if @sentry/tracing is used (i.e. added to the project and imported)
when the projects' tests are executed in a node envirnoment (e.g. Jest tests).
In the linked case above, the project lives in a monorepo in which also a DB library we automatically instrument (Mongoose) is listed as a dependency. In this case, the integration would be loaded (because of auto-discovery) and cause a Jest error.
This happens because _autoloadDatabaseIntegrations is called when the tracing package is imported:
https://github.com/getsentry/sentry-javascript/blob/cafd6cb7e529ebecfa5f96d634cffef42ab210ed/packages/tracing/src/hubextensions.ts#L290-L296
_autoloadDatabaseIntegrations is called because isNodeEnv evaluates to true in this case. The function evaluates to true because a) we are in a Node environment and b) the isBrowserBundle check evaluates to false:
https://github.com/getsentry/sentry-javascript/blob/f042723bb442193f03d61d4d316cc2645aad6cea/packages/utils/src/node.ts#L13-L19
https://github.com/getsentry/sentry-javascript/blob/334b09750a4bc7b697c259b08c55e05f5fcbb0d1/packages/utils/src/env.ts#L23-L25
The reason is that we only set the __SENTRY_BROWSER_BUNDLE__ for the NPM bundles that we build but not for our NPM packages.
We have a couple of options how to proceed here:
- Set the
__SENTRY_BROWSER_BUNDLE__magic string also in the NPM package build configs (for Angular, we have to handle it differently because we don't use rollup there) - Find another mechanism disable auto-loading in frontend JS SDKs. E.g. use an allowList of SDKs in which DB integrations can be auto-loaded (we could check the SDK metadata against this list)
- Refrain from addressing this problem for now because we're planning to deprecate and remove auto loading of DB integrations for performance reasons anyway in the near future.
We discussed this at our team meeting and decided to hold off from developing a fix ourselves for a couple of reasons:
- (most importantly) We're going to remove auto discovery of DB integrations in the next major version of our SDKs (v8)
- Given point 1 and the number of preconditions such that the bug described above would occur, we don't have the capacity to work on a fix which would soon get removed anyway.
- Fixing this properly doesn't appear to be trivial because of timing concerns with Sentry.init which might require other packages than sentry-tracing to have side-effects which we do not want.
That being said, if our community wants to see this fixed, please react to the issue with an upvote or a comment. In this case, we'd highly appreciate more information how this bug occurs and how it is breaking you. Furthermore, if you happen to have some time, we're always happy to review and accept PRs. Thanks!
(Backlogging this issue for now and putting it to the 8.0.0 milestone in which this bug will be fixed anyway).
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
What's the timeline for version 8? Will this be solved soonish?
Fixed in the latest release of the SDK: https://github.com/getsentry/sentry-javascript/releases/tag/7.46.0