上下文:部门很多项目,基本自测,测试覆盖率很低,即便有测试老师的加入,测试覆盖率也很难100%;用户肯定不会按照我们的预期来操作,但我们又不能让用户给我们试错,毕竟每一个错误/bug对用户,公司都可能是切切实实的伤害;而且我们的项目打印日志太多,基本没啥用,就导致很多异常也不知情,即便用户反馈了错误,需要先复现再捞错误日志,有些错误还很难复现,费时费力;最近部门,在整稳定性,于是便有了这篇文章。
简介:Sentry 是一款基于 Django实现的错误日志收集和聚合并告警的平台,基于Python编码实现,但是其日志监控功能却不局限于python;对很多开源的框架(Django/Flask/Gin等),语言(Python/Go/Java/React等)都可以做到无缝集成,甚至可以用来对iOS, Android 移动客户端以及 Web前端异常进行跟踪。
源码
安装
目前安装支持以下两种方式:
推荐Docker方式,既省事又方便;本文也将从docker方式(onpremise)进行安装讲解,也就是跟着ReadMe来操作。
Requirements
- Docker 17.05.0+
- Compose 1.23.0+
1、安装docker,跟着docker官网来就是
# 安装
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
# 启动
sudo systemctl enable docker
sudo systemctl start docker
2、安装Docker-Compose,也跟着Docker官网来,但官网有点慢,我们调整为下面的:
# 安装方式一
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
卸载就直接删掉这个可执行文件即可,rm -rf /usr/local/bin/docker-compose
# 安装方式二
sudo pip install docker-compose
卸载:sudo pip uninstall docker-compose
3、开始安装sentry
# 安装
$ git clone git@github.com:getsentry/onpremise.git
$ cd onpremise
# 添加钉钉通知等插件
$ cd sentry && vim requirements.example.txt 加入如下内容
sentry-dingchat
django-smtp-ssl~=1.0
redis-py-cluster==1.3.4
# 修改nginx.conf文件的server段配置,类似如下
$ cd ../nginx && vim nginx.conf
server {
listen 80;
server_name sentry.xxxxx.com;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect off;
# keepalive + raven.js is a disaster keepalive_timeout 0;
# use very aggressive timeouts proxy_read_timeout 5s;
proxy_send_timeout 5s;
send_timeout 10s;
resolver_timeout 5s;
client_body_timeout 5s;
client_max_body_size 4096m;
client_header_buffer_size 10m;
large_client_header_buffers 4 10m;
access_log /var/log/nginx/sentry.xxxxx.com.access.log main;
error_log /var/log/nginx/sentry.xxxxx.com.error.log;
location /api/store/ {
proxy_pass http://relay;
}
location ~ ^/api/[1-9]\d*/ {
proxy_pass http://relay;
}
location / {
proxy_pass http://sentry;
}
}
# 执行
$ cd .. && bash install.sh
4、启动sentry
docker-compose up -d
5、Django接入,还是跟着Sentry官网来操作
# 安装依赖
pip install --upgrade 'sentry-sdk==0.14.4'
# 编辑Django项目的settings文件
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
dsn="https://<key>@<organization>.ingest.sentry.io/<project>",
integrations=[DjangoIntegration(), CeleryIntegration(), RedisIntegration()],
# 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
)
# 编辑项目的url文件,添加测试sentry采集异常是否正常的API
from django.urls import path
def trigger_error(request):
division_by_zero = 1 / 0
urlpatterns = [
path('sentry-debug/', trigger_error),
# ...
]
6、预览效果
实践
1、React接入sentry,可参考:https://segmentfault.com/a/1190000021602782?utm_source=tag-newest
2、Vue接入sentry,可参考:https://www.jianshu.com/p/66e00077fac3