当前位置:   article > 正文

Sentry搭建+钉钉告警+Django接入_sentry 集成告警规则

sentry 集成告警规则

上下文:部门很多项目,基本自测,测试覆盖率很低,即便有测试老师的加入,测试覆盖率也很难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官网来就是

  1. # 安装
  2. sudo yum remove docker \
  3. docker-client \
  4. docker-client-latest \
  5. docker-common \
  6. docker-latest \
  7. docker-latest-logrotate \
  8. docker-logrotate \
  9. docker-engine
  10. sudo yum install -y yum-utils
  11. sudo yum-config-manager \
  12. --add-repo \
  13. https://download.docker.com/linux/centos/docker-ce.repo
  14. sudo yum install docker-ce docker-ce-cli containerd.io
  15. # 启动
  16. sudo systemctl enable docker
  17. sudo systemctl start docker

2、安装Docker-Compose,也跟着Docker官网来,但官网有点慢,我们调整为下面的:

  1. # 安装方式一
  2. 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
  3. sudo chmod +x /usr/local/bin/docker-compose
  4. 卸载就直接删掉这个可执行文件即可,rm -rf /usr/local/bin/docker-compose
  5. # 安装方式二
  6. sudo pip install docker-compose
  7. 卸载:sudo pip uninstall docker-compose

3、开始安装sentry

  1. # 安装
  2. $ git clone git@github.com:getsentry/onpremise.git
  3. $ cd onpremise
  4. # 添加钉钉通知等插件
  5. $ cd sentry && vim requirements.example.txt 加入如下内容
  6. sentry-dingchat
  7. django-smtp-ssl~=1.0
  8. redis-py-cluster==1.3.4
  9. # 修改nginx.conf文件的server段配置,类似如下
  10. $ cd ../nginx && vim nginx.conf
  11. server {
  12. listen 80;
  13. server_name sentry.xxxxx.com;
  14. proxy_set_header Host $http_host;
  15. proxy_set_header X-Forwarded-Proto $scheme;
  16. proxy_set_header X-Forwarded-For $remote_addr;
  17. proxy_redirect off;
  18. # keepalive + raven.js is a disaster keepalive_timeout 0;
  19. # use very aggressive timeouts proxy_read_timeout 5s;
  20. proxy_send_timeout 5s;
  21. send_timeout 10s;
  22. resolver_timeout 5s;
  23. client_body_timeout 5s;
  24. client_max_body_size 4096m;
  25. client_header_buffer_size 10m;
  26. large_client_header_buffers 4 10m;
  27. access_log /var/log/nginx/sentry.xxxxx.com.access.log main;
  28. error_log /var/log/nginx/sentry.xxxxx.com.error.log;
  29. location /api/store/ {
  30. proxy_pass http://relay;
  31. }
  32. location ~ ^/api/[1-9]\d*/ {
  33. proxy_pass http://relay;
  34. }
  35. location / {
  36. proxy_pass http://sentry;
  37. }
  38. }
  39. # 执行
  40. $ cd .. && bash install.sh

4、启动sentry

docker-compose up -d

5、Django接入,还是跟着Sentry官网来操作

  1. # 安装依赖
  2. pip install --upgrade 'sentry-sdk==0.14.4'
  3. # 编辑Django项目的settings文件
  4. import sentry_sdk
  5. from sentry_sdk.integrations.django import DjangoIntegration
  6. from sentry_sdk.integrations.celery import CeleryIntegration
  7. from sentry_sdk.integrations.redis import RedisIntegration
  8. sentry_sdk.init(
  9. dsn="https://<key>@<organization>.ingest.sentry.io/<project>",
  10. integrations=[DjangoIntegration(), CeleryIntegration(), RedisIntegration()],
  11. # If you wish to associate users to errors (assuming you are using
  12. # django.contrib.auth) you may enable sending PII data.
  13. send_default_pii=True
  14. )
  15. # 编辑项目的url文件,添加测试sentry采集异常是否正常的API
  16. from django.urls import path
  17. def trigger_error(request):
  18. division_by_zero = 1 / 0
  19. urlpatterns = [
  20. path('sentry-debug/', trigger_error),
  21. # ...
  22. ]

6、预览效果

实践 

1、React接入sentry,可参考:https://segmentfault.com/a/1190000021602782?utm_source=tag-newest

2、Vue接入sentry,可参考:https://www.jianshu.com/p/66e00077fac3

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/725797
推荐阅读
相关标签
  

闽ICP备14008679号