參考: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
原因如下:
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。
六、相關資料
-
sentry官網 https://sentry.io/
-
Sentry SDK for React Native
https://github.com/getsentry/react-native-sentry -
bugsnag的demo源碼
https://github.com/bugsnag/bugsnag-react-native -
fundebug
https://www.fundebug.com/