Skip to content

Database integrations are auto-loaded from @sentry/tracing when testing frontend projects  #5284

@Lms24

Description

@Lms24

Is there an existing issue for this?

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:

export function addExtensionMethods(): void {
_addTracingExtensions();
// Detect and automatically load specified integrations.
if (isNodeEnv()) {
_autoloadDatabaseIntegrations();
}

_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:

export function isNodeEnv(): boolean {
// explicitly check for browser bundles as those can be optimized statically
// by terser/rollup.
return (
!isBrowserBundle() &&
Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'
);

export function isBrowserBundle(): boolean {
return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__;
}

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions