getsentry / sentry-javascript

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

In shared environment, additional scope (tag/context) not set #13849

Closed vernak2539 closed 1 month ago

vernak2539 commented 1 month ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

8.32.0

Framework Version

No response

Link to Sentry event

https://vernacchia.sentry.io/issues/5939643523/?environment=production&project=4504945222483968&query=release:0.10.1

Reproduction Example/SDK Setup

// initialisation: https://github.com/vernak2539/chrome-extension-google-doc-default-zoom/blob/main/src/utils/sentry/base.ts
// usage: https://github.com/vernak2539/chrome-extension-google-doc-default-zoom/blob/main/src/contents/index.ts
const sentryClient = new BrowserClient({
  dsn: process.env.PLASMO_PUBLIC_SENTRY_DSN,
  tracesSampleRate: 1.0,
  environment: process.env.NODE_ENV || "development",
  release: packageJson.version,
    stackParser: defaultStackParser,
    integrations: getDefaultIntegrations({}).filter((defaultIntegration) => {
      return !["BrowserApiErrors", "Breadcrumbs", "GlobalHandlers"].includes(
        defaultIntegration.name
      );
    }),
    transport: makeFetchTransport
  });

  const sentryScope = new Scope();
  sentryScope.setTags({
    source,
    locale: chrome.i18n.getUILanguage(),
    extension: packageJson.name
  });
  sentryScope.setClient(sentryClient);

  sentryClient.init(); // initializing has to be done after setting the client on the scope

  sentryScope.setContext('extra', { extra: 'info' })

try {
  main()
} catch(err) {
  sentryClient.captureException(err);
}

Steps to Reproduce

  1. Setup client based on https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/
  2. Set extra context/tags on scope
  3. Use client in try/catch

Expected Result

I would expect the tags and context set on the sentry scope to be recorded in sentry on issues.

Actual Result

The tags and context are not set on the sentry issue when an error does occur (the error is captured)

lforst commented 1 month ago

Hi, you should be calling scope.captureException() like explained here: https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/, instead of calling client.captureException().

vernak2539 commented 1 month ago

Hi, you should be calling scope.captureException() like explained here: https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/, instead of calling client.captureException().

Ohhh my, how did I miss that šŸ¤¦ . Thanks! I'll give that a try and close for now.