如何使用sentry进行异常监控

参考:https://www.phpmianshi.com/?id=99

 

系统架构中应用程序的监控非常重要。比如你是否遇到过这种问题:当用户向你抛出一个bug(或者说异常),而你却找不到异常出现的原因和时机,也很难去重现这种奇葩的事件,此时你有一种众里寻他千百度,那bug却不知在何处的感觉。所以,利用某种工具去实现系统的异常监控,方便我们及时查看结果,并且作出合理的处理,这对于每个架构师来说都是挺重要的。

 

一、概念理解:

1、异常与捕获

  • 异常指的是在程序运行过程中发生的异常事件,通常是由外部问题(如硬件错误、输入错误)所导致的。

  • 异常(Exception)都是运行时的。编译时产生的不是异常,而是错误(Error)。需要注意的是,程序设计导致的错误不属于异常(Exception)。

  • 总结:
    Error:系统内部错误,这类错误由系统进行处理,程序本身无需捕获处理;
    Exception:可以处理的异常

  • 捕获:获取异常
    通过try…catch语句进行捕获异常。
    通过throw抛出异常,throws向上一级调用方法抛出异常。

2、DSN(数据源名称)

  • DSN:即Data Source Name

  • Sentry官网是如此描述DSN的:
    完成Sentry项目的设置后,将给您指定一个值,我们称之为DSN或数据源名称。它看起来像标准的URL,但实际上只是Sentry SDKS所需要的配置的表示。

  • 下面就是一个DSN的例子:

    SENTRY_LARAVEL_DSN=http://[email protected]:9000/2
    
  • 可以在你所在的项目中查看你的DSN,把他集成到你的代码中

     

     

二、为什么要集成异常监控

即使我们对上线的项目做了大量的测试,但有时候还是会有潜在的bug,这种比较顽固的问题只能通过监控机制才能有效的减少其带来的损失,所以异常捕获和上报很重要。

 

三、如何安装

 

参考官网:https://github.com/getsentry/onpremise

 

这里主要介绍邮件配置中遇到的问题,安装后发现无法发送邮件,解决方案如下:

修改config.yml配置文件:

mail.backend: 'smtp'  

mail.host: 'smtp.exmail.qq.com'

#mail.port: 465  这里端口需要改为587

mail.port: 587

mail.username: '[email protected]'

mail.password: 'password'

mail.use-tls: true

mail.from: '[email protected]'


重新启动:

docker-compose down
docker-compose build
docker-compose up -d

 

主要参考了:

https://github.com/getsentry/onpremise/issues/404

https://stackoverflow.com/questions/47328056/smtpserverdisconnected-connection-unexpectedly-closed-timed-out/47328415#47328415

 

原因如下:

It seems that currently django.core.mail.backends.smtp.EmailBackend does not support sending emails over ssl and only TSL.

I changed the port to 587 and emails are going through as expected.

四、如何接入异常捕获

本文主要介绍laravel项目如何接入sentry,sentry主要是捕获到你未catch的程序异常,所以如果你用程序去catch了异常,sentry就没有将这个异常上报到服务器了。这时候你可以代码中自己主动上报。

 

Install the sentry/sentry-laravel package:

$ composer require sentry/sentry-laravel:1.7.1

If you’re on Laravel 5.5 or later the package will be auto-discovered. Otherwise you will need to manually configure it in your config/app.php.

Add Sentry reporting to App/Exceptions/Handler.php.

For Laravel 7.x and later:

public function report(Throwable $exception){
    if (app()->bound('sentry') && $this->shouldReport($exception)) {
        app('sentry')->captureException($exception);
    }

    parent::report($exception);}

For Laravel 5.x and 6.x:

public function report(Exception $exception){
    if (app()->bound('sentry') && $this->shouldReport($exception)) {
        app('sentry')->captureException($exception);
    }

    parent::report($exception);}

Create the Sentry configuration file (config/sentry.php) with this command:

$ php artisan vendor:publish --provider="Sentry\Laravel\ServiceProvider"

Add your DSN to .env:

SENTRY_LARAVEL_DSN=http://[email protected]:9000/2

You can easily verify that Sentry is capturing errors in your Laravel application by creating a debug route that will throw an exception:

Route::get('/debug-sentry', function () {
    throw new Exception('My first Sentry error!');});

Visiting this route will trigger an exception that will be captured by Sentry.

 

接入完毕。

 

五、选择哪个异常监控工具

​1 .腾讯的bugly:比较适合原生APP开发,因为它报告的异常基本都是java端的代码,你很难定位到JavaScript端的代码异常。

2. Bugsnag:聚焦于web端、移动端、服务端程序的错误监控,能自动报告未处理的异常和崩溃,通过崩溃报告日志更好地了解到有关用户的操作情况,也附上了相应的用户信息来确定受崩溃的程度。国内访问bugsnag超级慢且卡,而且收费,排除使用。

 

3.fundebug 目前暂不支持PHP,而且收费,排除使用。对JS的支持能力很强,貌似还有录屏功能,快速复现出错场景。

4. Sentry:Sentry是一个实时事件日志和聚合平台。它的核心是监视错误和提取所有必要的信息,以便进行正确的事后分析。支持多种语言,且有开源免费版本。不仅支持后端,前端JS,貌似现在也支持ios和android。

 

六、相关资料

 


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章