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

Warnings on import: "SDK is loaded more than once. This is unsupported and might have unexpected behavior" #2919

Closed
SargisPlusPlus opened this issue Aug 2, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@SargisPlusPlus
Copy link

SargisPlusPlus commented Aug 2, 2024

Steps to Reproduce

  1. Have a Chrome extension that injects content scripts into a webpage.
  2. Import DatadogLogs in both the webpage and the Chrome extension's content script.
  3. Observe that the warning appears in the console.

On import, the Datadog SDK calls defineGlobal on the window object and produces the warning: "SDK is loaded more than once. This is unsupported and might have unexpected behavior."

Environment

  • Datadog SDK version: ^5.23.3
  • Node version: v20.8.1
  • Browser: Chrome

Additional Context

This issue occurs because my Chrome extension and the webpage share the same window object, resulting in two Datadog loggers being defined on the same window.

@SargisPlusPlus SargisPlusPlus added the enhancement New feature or request label Aug 2, 2024
@BenoitZugmeyer
Copy link
Member

This is expected, only one instance of the Logs SDK should be loaded at a time. Beside using a global variable (which doesn't cause any issue if you don't use it), the Logs SDK is also ding a few things globally:

  • a session is store in a cookie. If two SDK instances are configured differently (ex: with a different sample rate), it can lead to unexpected behavior

  • global errors and console.logs are collected by the SDK. If two SDK instances are configured to do so, we'd collect data twice.

That being said, this is not the first time we've had that request, and we probably should do something about it. In the meantime, you could ignore the warning.

@ChillyBots
Copy link

ChillyBots commented Aug 24, 2024

@BenoitZugmeyer I thought I'd add more context here.

We load import { datadogRum } from '@datadog/browser-rum'; in 12 util files across our app, however those util files are used everywhere so we can log RUM actions & errors - this means we're essentially importing a datadogRum everywhere.

Aside from manually adding the package to the global scope myself - which seems like 'doubling up' if the package itself adds to the global scope, I'm not sure how it is intended to be used in a JS module-based app without getting these warnings.

I'd like to request you reopen this and bump it's prio, as we're getting hundreds (maybe thousands) of errors in our test runs & build pipelines which are nearly impossible to ignore.

@BenoitZugmeyer
Copy link
Member

I'm not sure how it is intended to be used in a JS module-based app without getting these warnings.

Importing the library multiple times shouldn't be an issue. It should not evaluate the library multiple times, in the same way React isn't re-evaluated every time you `import React from 'react'.

If it is an issue for you, you might have a problem in your bundler setup and somehow the SDK is included multiple times in your JS assets. This is highly ineficient, and I'd suggest adjusting your configuration.

@vytautas-vitkus-tg
Copy link

Seeing the same issue in our tests as well bumping from 5.8.0 to 5.26.0 version. Fixed tests by mocking the lib:

jest.mock('@datadog/browser-rum-slim', () => ({
    init: jest.fn(),
    setUser: jest.fn(),
    clearUser: jest.fn(),
    addAction: jest.fn(),
    addError: jest.fn(),
}));

@ChillyBots
Copy link

@BenoitZugmeyer sorry, I should have clarified, it just meant our test logs went crazy with the same error. There was no functional impact.

@vytautas-vitkus-tg - I could kiss you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants