项目监控之sentry
github: https://github.com/getsentry/sentry
1.什么是sentry?
当我们完成一个业务系统的上线时,总是要观察线上的运行情况,对于每一个项目,我们都没办法保证代码零BUG、零报错,即便是经历过测试,因为测试永远无法做到100%覆盖,用户也不会总是按照我们所预期的进行操作,在上线后也会出现一些你预料不到的问题,而这种情况下,广大的用户其实才是最好的测试者。当生产环境中产生了一个 bug 时,如何做到迅速报警,找到问题原因,修复后又如何在线上验证?
此时我们需要一个高效的错误监控系统。sentry扮演着一个错误收集的角色,将你的项目和sentry结合起来,无论谁在项目使用中报错,sentry都会第一次时间通知开发者,我们需要在系统异常时主动对其进行收集上报,出现了什么错误,错误出现在哪,帮你记录错误,以制定解决方案并进行优化迭代。
sentry是一个基于Django构建的现代化的实时事件日志监控、记录和聚合平台,主要用于如何快速的发现故障。支持几乎所有主流开发语言和平台,并提供了现代化UI,它专门用于监视错误和提取执行适当的事后操作所需的所有信息,而无需使用标准用户反馈循环的任何麻烦。官方提供了多个语言的SDK.让开发者第一时间获悉错误信息,并方便的整合进自己和团队的工作流中.官方提供saas版本免费版支持每天5000个event.
sentry支持自动收集和手动收集两种错误收集方法.我们能成功监控到vue中的错误、异常,但是还不能捕捉到异步操作、接口请求中的错误,比如接口返回404、500等信息,此时我们可以通过Sentry.caputureException()进行主动上报。使用sentry需要结合两个部分,客户端与sentry服务端;客户端就像你需要去监听的对象,比如公司的前端项目,而服务端就是给你展示已搜集的错误信息,项目管理,组员等功能的一个服务平台。
这个平台可以自己搭建,也可以直接使用sentry提供的平台(注册可用),当然如果是公司项目,当然推荐自己搭建.
[安装]
安装较为较为简单,略过
[使用]
PHP
Yii2:
安装: composer require notamedia/yii2-sentry 配置: return [ 'components' => [ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'notamedia\sentry\SentryTarget', 'dsn' => 'http://2682ybvhbs347:235vvgy465346@sentry.io/1', 'levels' => ['error', 'warning'], // Write the context information (the default is true): 'context' => true, // Additional options for `Sentry\init`: 'clientOptions' => ['release' => 'my-project-name@2.3.12'] ], ], ], ], ]; 使用: \Yii::error('message', 'category');
更多使用:https://github.com/notamedia/yii2-sentry
laravel:
1. # 安装依赖包, 注意框架版本会有些许不同 composer require sentry/sentry-laravel 2. # 如果是5.5或更高版本则会自动配置, 否则需要手动配置config/app.php 'providers' => array( // ... Sentry\Laravel\ServiceProvider::class, ), 'aliases' => array( // ... 'Sentry' => Sentry\Laravel\Facade::class, ), 3.新增异常捕获方法 App/Exceptions/Handler.php public function report(Exception $exception) { if ($this->shouldReport($exception) && app()->bound('sentry')) { app('sentry')->captureException($exception); } parent::report($exception); } 4. #添加配置, 他会自从创建config/sentry.php文件 php artisan sentry:publish --dsn=https://f4342a949b1a5f2b3140c4eb@o514873.ingest.sentry.io/561879011 # 添加配置项到.env SENTRY_LARAVEL_DSN=https://f4a4333949bf2df140c4eb@o873.ingest.sentry.io/5618 5. # 服务验证 php artisan sentry:test 6. # 添加测试路由 Route::get('/debug-sentry', function () { throw new Exception('My first Sentry error!'); });
更多查看: https://docs.sentry.io/platforms/php/guides/laravel/other-versions/laravel6-7/
Python
Django:
[安装] pip install --upgrade sentry-sdk [配置] 配置文件(settings.py)中添加: import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration sentry_sdk.init( dsn="http://0576e29050c84754a9b8ed43de558741@47.106.98.206:9000/5", integrations=[DjangoIntegration()], # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, # If you wish to associate users to errors (assuming you are using # django.contrib.auth) you may enable sending PII data. send_default_pii=True
environment="dev" # 添加环境变量 )
urls.py中
from django.urls import path
def trigger_error(request):
division_by_zero = 1 / 0
urlpatterns = [
path('sentry-debug/', trigger_error),
# ...
]
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2019-08-15 详解EveryThing