Sentry-javascript: How to disable Sentry again at runtime?

Created on 25 Apr 2019  ·  14Comments  ·  Source: getsentry/sentry-javascript

Package + Version

  • [x] @sentry/browser
  • [ ] @sentry/node
  • [ ] raven-js
  • [ ] raven-node _(raven for node)_
  • [ ] other:

Description

We allow to enable or disable error reporting in our browser extension (which is needed for GDPR). A browser extensions background script is long-running (doesn't re-run at page reloads), so when error reporting is disabled, we need to disable Sentry after it was already enabled with Sentry.init(). I see there is Sentry.init({ enabled: false }) but it doesn't look legal to call Sentry.init() twice. How can I disable Sentry after it was enabled before, at runtime?

Most helpful comment

As described in the issue description, we need to allow users to opt in and out of error reporting for privacy reasons. Anything that is unclear? Happy to provide more info but not sure what you are looking for

All 14 comments

SentryBrowser.getCurrentHub().getClient().getOptions().enabled

That gets me the current value, but how do I _change_ it?

Sentry.getCurrentHub().getClient().getOptions().enabled = false; 😅

And just to be sure, that will make Sentry stop sending anything to Sentry servers?

Could you consider this a feature request for something more explicit, or at least a request for official documentation?

@felixfbecker Sure, can you just elaborate why you need this?
Is it some kind of "Ask the user for permission to send crash reports?"?

As described in the issue description, we need to allow users to opt in and out of error reporting for privacy reasons. Anything that is unclear? Happy to provide more info but not sure what you are looking for

@felixfbecker Sorry, still not fully awake 😅 should have read the initial issue.
We will add something like Sentry.setEnabled(boolean) it makes perfect sense to have something like this. We also got some reports for other SDKs already.

Just for the record, there are other ways of handling this (e.g. beforeSend) besides what Kamil posted but they are all not really convenient.

Why is this issue closed if this feature request is in the works ?

Any updates on providing the Sentry.setEnabled(boolean) to toggle error reports on/off?

In Raven I think you had shouldSendCallback to toggle reporting

In Raven I think you had shouldSendCallback to toggle reporting

Sentry.init({
  beforeSend(event) {
    if (shouldSendEvent) return event;
    return null;
  }
});

Any updates on providing the Sentry.setEnabled(boolean) to toggle error reports on/off?

What's your use-case specifically? It's already possible to conditionally drop and/or enable/disable sdk.

Thanks, I couldn't find on the docs if actually sending null would prevent the sdk from reporting errors. this solves my use case

@diegolanda https://docs.sentry.io/error-reporting/configuration/?platform=go#before-send but I agree nothing is not specific enough for javascript 😅

Was this page helpful?
0 / 5 - 0 ratings