Sentry-javascript: Disable sentry on dev environment when using angular plugin

Created on 17 Dec 2015  ·  15Comments  ·  Source: getsentry/sentry-javascript

At the moment it is not possible to disable the sentry plugin because the ngRaven module fails to load.

On the development environment I use the following without providing the DSN
Raven.config().install()

The callback that registers the raven provider is not called

Raven.addPlugin(function () {
    angular.module('ngRaven', [])
        .provider('Raven',  RavenProvider)
        .config(['$provide', ExceptionHandlerProvider]);
});

Because the install function does not call the plugin that installs the module
isSetup() failed because the globalServer is null

install: function() {
    if (isSetup() && !isRavenInstalled) {
        TraceKit.report.subscribe(handleStackInfo);

        // Install all of the plugins
        each(plugins, function(_, plugin) {
            plugin();
        });

        isRavenInstalled = true;
    }

    return Raven;
},

Most helpful comment

I had a similar request from @benvinegar and he pointed out that you can use:

Raven.config('your dsn', {
  shouldSendCallback: function () {
    return false;
  }
}).install();

And that will stop Raven from reporting. Switch the return status to TRUE and it will start reporting. At least this is what I've been told. We plan to use this plus a deployment sed command to flip the boolean at deployment time.

All 15 comments

Related to #414 and #413

@Sija @odedfos – can you comment on #414? Like, will that PR address this issue?

@benvinegar seems legit for me, although I could imagine case where you'd want Raven provider to be available, even without calling .config() — like setting Raven.debug flag for instance

I had a similar request from @benvinegar and he pointed out that you can use:

Raven.config('your dsn', {
  shouldSendCallback: function () {
    return false;
  }
}).install();

And that will stop Raven from reporting. Switch the return status to TRUE and it will start reporting. At least this is what I've been told. We plan to use this plus a deployment sed command to flip the boolean at deployment time.

I did something similar to the above to allow install() to suceed, and to avoid making any http requests to actually send the errors (window.SERVER_FLAGS is how I pass my sentryURL from my backend to the frontend).

if (!window.SERVER_FLAGS.sentryURL) {
  // Never bother sending data to sentry
  Raven.setShouldSendCallback(function() { return false });
  // Allow Raven.install() to succeed
  Raven.isSetup = function() {
    return true;
  }
}
Raven
.config(window.SERVER_FLAGS.sentryURL, {
  release: window.SERVER_FLAGS.version,
  debug: true,
})
.addPlugin(Raven.Plugins.Angular)
.install();

This seems to be working fairly well, and still lets Raven run, but it simply doesn't report. I really dislike having to hack isSetup but its the only way I could find that was minimally invasive to get ngRaven loaded.

For others who stumble upon this

If you are using webpack to build your application, you use environment variables to configure raven by using the environment plugin

https://webpack.js.org/plugins/environment-plugin/

e.g.

if (process.env.NODE_ENV === 'dev') {
  Raven.setShouldSendCallback(() => { return false; });
  Raven.isSetup = () => { return true; };
}

Raven
  .config(process.env.RAVEN_DSN, {
    debug: process.env.RAVEN_DEBUG,
  })
  .addPlugin(require('raven-js/plugins/angular'), angular)
  .install();

isSetup is now read-only, so the above method no longer works.

I found a following workaround, which seems to work, although I have no idea why.

let isProduction = process.env.ENV === 'build'; // variable provided by webpack

Raven
.config('https://<key>@sentry.io/<project>', {
  shouldSendCallback: function () {
    return isProduction;
  }
})
.install();

if (!isProduction) {
  Raven.uninstall(); // this is necessary! for some reason
}

export class RavenErrorHandler implements ErrorHandler {
  handleError(err: any): void {
    console.error(err); // this still fires after uninstalling!!! it's because it's already listed as Angular provider 
    Raven.captureException(err)
  }
}

It's still an awful solution because it hijacks all my errors so I don't know from which line they were thrown. At least I guess it's not as terrible as not seeing them at all.

I have used this way. It seems working for me

`if (environment.production) {
Raven.config('https://@sentry.io/')
.install();
}

and in providers
providers: [environment.production ? { provide: ErrorHandler, useClass: RavenErrorHandler } : [], ...`

Please let me know if I am doing anything wrong here.

Looks fine to me. Apparently, original issue has been answered here already, so closing this one. Feel free to reopen if it's still relevant in any way.

More easy at 'app.module.ts'

import {environment} from '../environments/environment';
...
    providers: [
    LocalStorageService,
    EventLocalStorageService,
    EventService,
    ActionButtonService,
    WeatherUndergroundWeatherService,
    GeoLocationInfoService,
    AppEventColorService,
    // {provide: ErrorHandler, useClass: RavenErrorHandler}
    {provide: ErrorHandler, useClass: environment.production ? RavenErrorHandler : ErrorHandler} // See here
  ],

and

  .config('key', {
    shouldSendCallback: function () {
      return environment.production;
    }
  })
  .install();

This way you have the localdev messages in your console (else Sentry grabs some) and at production you get the ones you want to have

At least in Chrome (not sure about other browsers), another way to prevent Raven from "hijacking" the dev console is to blackbox raven.js:

https://gist.github.com/paulirish/c307a5a585ddbcc17242
https://developer.chrome.com/devtools/docs/blackboxing

2018-05-11_12-45-12

And what about Sentry? I don't use Raven, i have Sentry and i want to disable it on development environment on my localhost.

https://dev.to/angular/tracking-errors-in-angular-with-sentry-4oo0 — here it is

@artuska you can either provide empty DSN or use beforeSend to halt the transport.

Sentry.init({
  dsn: process.env.development ? '' : 'your-real-dsn'
})

or

Sentry.init({
  dsn: 'your-real-dsn',
  beforeSend(event) {
    if (process.env.development) return null;
    return event;
  }
})

I dont think that actually disables Sentry. It just stops if from sending
data. Still the breadcrumbs wraps most issues which sucks

On Mon, Dec 9, 2019 at 10:15 AM Kamil Ogórek notifications@github.com
wrote:

@artuska https://github.com/artuska you can either provide empty DSN or
use beforeSend to halt the transport.

Sentry.init({
dsn: process.env.development ? '' : 'your-real-dsn'
})

or

Sentry.init({
dsn: 'your-real-dsn',
beforeSend(event) {
if (process.env.development) return null;
return event;
}
})


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/getsentry/sentry-javascript/issues/436?email_source=notifications&email_token=AAJVX45ZEIJWZSXZSBQ5ZQLQXYECDA5CNFSM4BW42VRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGIMY2Y#issuecomment-563137643,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJVX4YBWA3R6SM63NV5JPDQXYECDANCNFSM4BW42VRA
.

@jimmykane correct, if you want to disable Sentry, just call init conditionally

Was this page helpful?
0 / 5 - 0 ratings