getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
8.02k stars 1.59k forks source link

Multi sentry instances #14000

Open Amschi opened 1 month ago

Amschi commented 1 month ago

Environment

SaaS (https://sentry.io/)

What are you trying to accomplish?

Hi,

We are experiencing issues to setup sentry correctly when trying to follow this guide. We have a multi module app. A main sentry is instantiated in an angular shell, I'm working with a react sub module and I would like to setup sentry on it.

Angular main app setup

    const client = new BrowserClient({
      dsn: this.config.dsn,
      environment: this.getEnvironment(),
      release: this.environmentDetection.getMoabVersion(),
      beforeSend: (event: ErrorEvent) =>
          errorsFilter(this.errorsToIgnore, event),
      beforeBreadcrumb: (crumb: Breadcrumb) => {
        const shrinkedCrumb = { ...crumb };
        if (crumb.data?.['extra']?.arguments) {
          shrinkedCrumb.data['extra'] = {
            ...crumb.data['extra'],
            arguments: crumb.data['extra'].arguments.map((arg: any) => {
              const shrinkedArg = { ...arg };
              delete shrinkedArg.ngDebugContext;
              delete shrinkedArg.ngErrorLogger;
              return shrinkedArg;
            }),
          };
        }
        return shrinkedCrumb;
      },
      transport: makeFetchTransport,
      stackParser: defaultStackParser,
      integrations: [
        breadcrumbsIntegration(),
        globalHandlersIntegration(),
        linkedErrorsIntegration(),
        dedupeIntegration(),
      ],
    });
    this.scope = new Scope();
    this.scope.setClient(client);
    client.init();
    this.setInitialTags();

React setup

const nonShellIntegrations = getDefaultIntegrations({}).filter((defaultIntegration) => {
  return !['BrowserApiErrors', 'Breadcrumbs', 'GlobalHandlers'].includes(defaultIntegration.name);
});

const reactIntegrations = [
  reactRouterV6BrowserTracingIntegration({
    useEffect,
    useLocation,
    useNavigationType,
    createRoutesFromChildren,
    matchRoutes,
  }),
];

const sentryClient = init({
  dsn,
  ignoreErrors,
  release,
  environment: criteoEnv,
  integrations: [...nonShellIntegrations, ...reactIntegrations],
  tracesSampleRate: 0.0001,
});

const scope = getCurrentScope();

scope.setTag('criteoEnv', criteoEnv);
scope.setTag('relookEnv', relookEnv);
scope.setTag('bootstrapMode', bootstrapMode);
scope.setTag('userId', userId);

return sentryClient;

How are you getting stuck?

With the current setup, parent module are leaking into sub project in sentry, meaning because of navigation we have error in sentry that are not related to our project. According to the documentation, we should instantiate only one sentry client per js app, but If I do

const integrations = getDefaultIntegrations({}).filter(
  (defaultIntegration) => {
    return !["BrowserApiErrors", "Breadcrumbs", "GlobalHandlers"].includes(
      defaultIntegration.name,
    );
  },
);

const client = new BrowserClient({
  dsn: "DSN",
  transport: makeFetchTransport,
  stackParser: defaultStackParser,
  integrations: integrations,
});

const scope = new Scope();
scope.setClient(client);

client.init();

this, i don't receive any event in sentry.

For now, we can do an ugly filtering with onBeforeSend callback, but I would prefer to understand what we are doing wrong here. Thanks alot Support would be appreciated.

Where in the product are you?

Issues

Link

No response

DSN

https://63ada74c29079f1175bafa37bf824f22@o145737.ingest.us.sentry.io/4508099137503232

Version

No response

getsantry[bot] commented 1 month ago

Assigning to @getsentry/support for routing ⏲️

getsantry[bot] commented 1 month ago

Routing to @getsentry/product-owners-issues for triage ⏲️

jangjodi commented 1 month ago

@ReneGreen27 if my understanding is correct, this issue appears to be an SDK issue. Could this be re-assigned?

chargome commented 1 month ago

Hey @Amschi, could you try filtering all the integrations like this, otherwise your filters would probably have no effect:

integrations: (defaultIntegrations) => [
    ...defaultIntegrations.filter(
      (integration) =>
        !["BrowserApiErrors", "Breadcrumbs", "GlobalHandlers"].includes(
          integration.name,
        ),
    ),
    happyIntegration(),
  ],
Amschi commented 1 month ago

Hi @chargome,

With the approach you are showing you confirm I can remove beforeSend callback, and the shell events would not leak in my submodule ?

chargome commented 1 month ago

@Amschi I just wanted to point out that the way you are filtering integrations will not work properly – and that you could try if that fixes your issue.

Amschi commented 1 month ago

@chargome I can change this filtering integrations.

But it's not the main point of the problem.

My issue is that parent module events are leaking in submodules. Do you think by taking your changes it would solve the issue of event leaking to another project ?

chargome commented 1 month ago

@Amschi It might, because these integrations use global state.

Amschi commented 1 month ago

Thanks a lot, I would try asap this changes and come back at you if it changes.

So to test, I took your suggestion + I remove the before send callback, let's hope it works.

Amschi commented 1 month ago

Hey @chargome , thanks a lot it seems to do the trick.

But I have a new problem, I lost the tracings. Do you see any issue with this conf ? I tried to follow this doc, but even when I add browser tracing, I don't have them.

Thanks a lot

  const sentryClient = init({
    dsn,
    ignoreErrors,
    release,
    environment: criteoEnv,
    integrations: (integrations) => [
      ...nonShellIntegrations(integrations),
      ...reactIntegrations,
      thirdPartyErrorFilterIntegrations,
    ],
    tracesSampleRate: 1,
  });
lforst commented 1 month ago

@Amschi could you provide an actual reproduction example we could run? That way we can more effectively debug this.

Amschi commented 1 month ago

Hi @lforst,

I'm afraid it would not be possible. Providing an accurate reproduction is almost impossible, and if I produce a mimic like reproduction we would not be sure of the problem anyway.

Are you not able to pinpoint the issue by looking at the conf ? I understand it might be a hard guess :/

lforst commented 1 month ago

@Amschi unfortunately we cannot make efficient assessments from just looking at parts of your code. If you want us to take a proper look instead of giving superficial hints we need reproduction. There is only so much time we can dedicate to figuring out what is most likely configuration issues.

getsantry[bot] commented 2 days ago

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 remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀