Sentry-javascript: 使用 angular 插件时在开发环境中禁用哨兵

创建于 2015-12-17  ·  15评论  ·  资料来源: getsentry/sentry-javascript

目前无法禁用哨兵插件,因为 ngRaven 模块无法加载。

在开发环境上我使用以下不提供DSN
Raven.config().install()

注册 raven provider 的回调没有被调用

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

因为安装函数没有调用安装模块的插件
isSetup() 失败,因为 globalServer 为空

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

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

        isRavenInstalled = true;
    }

    return Raven;
},

最有用的评论

我从@benvinegar那里收到了类似的请求,他指出您可以使用:

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

这将阻止 Raven 报告。 将return状态切换为 TRUE,它将开始报告。 至少这是我被告知的。 我们计划使用它加上一个部署sed命令在部署时翻转布尔值。

所有15条评论

与#414 和#413 相关

@Sija @odedfos – 你能评论 #414 吗? 比如,那个公关会解决这个问题吗?

@benvinegar对我来说似乎是合法的,尽管我可以想象您希望Raven提供程序可用的情况,即使没有调用.config() - 例如设置Raven.debug标志

我从@benvinegar那里收到了类似的请求,他指出您可以使用:

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

这将阻止 Raven 报告。 将return状态切换为 TRUE,它将开始报告。 至少这是我被告知的。 我们计划使用它加上一个部署sed命令在部署时翻转布尔值。

我做了与上面类似的事情来允许 install() 成功,并避免发出任何 http 请求来实际发送错误( window.SERVER_FLAGS是我将我的 sentryURL 从后端传递到前端的方式)。

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();

这似乎运行得相当好,并且仍然让 Raven 运行,但它根本不报告。 我真的不喜欢必须破解isSetup但这是我能找到的唯一方法,它是让ngRaven加载的微创方法。

对于偶然发现此问题的其他人

如果您使用 webpack 构建您的应用程序,您可以使用环境变量通过环境插件来配置 raven

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

例如

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 现在是只读的,因此上述方法不再有效。

我找到了以下解决方法,它似乎有效,但我不知道为什么。

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)
  }
}

这仍然是一个糟糕的解决方案,因为它劫持了我所有的错误,所以我不知道它们是从哪一行抛出的。 至少我想这不像根本没有看到它们那么可怕。

我用过这种方式。 似乎对我有用

`如果(环境。生产){
Raven.config('https://@sentry.io/')
。安装();
}

并在提供者中
提供者:[环境.生产? { 提供: ErrorHandler, useClass: RavenErrorHandler } : [], ...`

如果我在这里做错了什么,请告诉我。

对我来说看起来不错。 显然,原来的问题已经在这里得到了回答,所以关闭这个。 如果它仍然以任何方式相关,请随时重新打开。

'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
  ],

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

这样,您的控制台中就有 localdev 消息(否则 Sentry 会抓取一些),而在生产中您会得到想要的消息

至少在 Chrome 中(不确定其他浏览器),另一种防止 Raven“劫持”开发控制台的方法是黑箱raven.js

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

2018-05-11_12-45-12

那么哨兵呢? 我不使用 Raven,我有 Sentry,我想在我的本地主机上的开发环境中禁用它。

https://dev.to/angular/tracking-errors-in-angular-with-sentry-4oo0 — 在这里

@artuska您可以提供空的 DSN 或使用beforeSend来停止传输。

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

或者

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

我不认为这实际上会禁用 Sentry。 它只是停止如果发送
数据。 面包屑仍然包含了大多数糟糕的问题

2019 年 12 月 9 日星期一上午 10:15 Kamil Ogórek [email protected]
写道:

@artuska https://github.com/artuska您可以提供空的 DSN 或
使用 beforeSend 停止传输。

哨兵.init({
dsn: process.env.development ? '' : '你真正的 dsn'
})

或者

哨兵.init({
dsn: '你的真实 dsn',
之前发送(事件){
if (process.env.development) 返回 null;
返回事件;
}
})


您收到此消息是因为您发表了评论。
直接回复本邮件,在GitHub上查看
https://github.com/getsentry/sentry-javascript/issues/436?email_source=notifications&email_token=AAJVX45ZEIJWZSXZSBQ5ZQLQXYECDA5CNFSM4BW42VRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LODNXPY43VMZGOZG0DNXPY43VMZGOZZSXZSBQ5ZQXYECDA5CNFSM4BW42VRKYY3
或取消订阅
https://github.com/notifications/unsubscribe-auth/AAJVX4YBWA3R6SM63NV5JPDQXYECDANCNFSM4BW42VRA
.

@jimmykane正确,如果你想禁用 Sentry,只需有条件地调用init

此页面是否有帮助?
0 / 5 - 0 等级