Sentry是一个开源错误跟踪工具,可帮助您实时监控和修复bug。支持 Python、OC、Java、Go、Node、Django、RoR 等主流编程语言和框架,还提供了 GitHub、Slack、Trello 等常见开发工具的集成。
Sentry以组织的形式对用户进行管理,为每个用户建立一个独立的账户,每个用户可以加入多个不同的组织(类似于SSO)。Sentry官方对于用户根据权限的不同划分为以下5种角色:
不同用户的权限对应如下表格:
行动 | Billing | Member | Admin | Manager | Owner |
---|---|---|---|---|---|
可以查看/编辑结算信息和订阅详细信息 | X | X | |||
可以查看和处理问题,例如分配/解析/等。 | X | X | X | X | |
可以加入和离开团队。 | X | X | X | X | |
可以更改项目设置 | X | X | X | ||
可以添加/删除项目 | X | X | X | ||
可以编辑全局集成 | X | X | |||
可以添加/删除/更改成员 | X | X | |||
可以添加/删除团队 | X | X | |||
可以添加存储库 | X | X | |||
可以更改组织设置 | X | X | |||
可以删除组织 | X |
sentry官方提供了两种方式来安装sentry:docker、python。在此使用传统的python环境进行安装。
python-setuptools
、python-devel
、 libxslt1-devel
、gcc
、 libffi-devel
、 libjpeg-devel
、 libxml2-devel
、 libxslt-devel
、 libyaml-devel
、 libpq-devel
(yum安装)环境 | 版本 |
---|---|
系统 | centos 7.4 |
python | 2.7.5 |
pip | 18.1 |
部署位置 | 192.168.228.129 |
新版本sentry不再支持mysql, 官方给的解释:
"Due to numerous issues over the years and recent discoveries that nearly all schema migration was broken in MySQL (due to some behavior in our migration tool), we've made the decision to no longer support MySQL. It is possible to bring the schema up to date on a MySQL machine, but Sentry's automated migrations will likely not work and require DBA assistance. Postgres is now the only supported production database."
Note: postgresql的配置文件在 /var/lib/pgsql/10/data/
。
/var/lib/pgsql/10/data/pg_hba.conf 82行
,允许本地连接:
host all all 127.0.0.1/32 trust
/var/lib/pgsql/10/data/postgresql.conf
,更改监听ip:
listen_addresses = '127.0.0.1'
# 安装基础包
$ yum install -y python-setuptools python-devel libxslt1-devel gcc libffi-devel libjpeg-devel libxml2-devel libxslt-devel libyaml-devel libpq-devel
# 安装virtualenv
$ pip install -U virtualenv
$ su - chunyu
# 创建虚拟环境部署位置:/home/chunyu
$ virtualenv /home/chunyu/workspace/ENV_sentry/
# 进入虚拟环境
$ source /home/chunyu/workspace/ENV_sentry/bin/activate
$ (sentry) [root@host2 sentry]# ## (sentry)标记表示进入了虚拟环境
在虚拟环境执行:
$ pip install -U sentry
该过程安装依赖比较多,遇到问题如下:
# 执行sentry命令,输出结果如下:
$ sentry
Usage: sentry [OPTIONS] COMMAND [ARGS]...
Sentry is cross-platform crash reporting built with love.
The configuration file is looked up in the `~/.sentry` config directory but this can be
overridden with the `SENTRY_CONF` environment variable or be explicitly provided through the
`--config` parameter.
... ...
$ sentry init /home/chunyu/workspace/sentry
# 从8.0版本后初始化会生成两个文件,如下,config.yml会在后续的版本强制取代sentry.conf.py
$ ls /home/chunyu/workspace/sentry/
config.yml sentry.conf.py
# 为sentry指定配置文件路径
$ vim /home/chunyu/workspace/ENV_sentry/lib/python2.7/site-packages/sentry/runner/settings.py
DEFAULT_SETTINGS_CONF = '/home/chunyu/workspace/sentry/config.yml'
DEFAULT_SETTINGS_OVERRIDE = '/home/chunyu/workspace/sentry/sentry.conf.py'
# ~/.sentry/sentry.conf.py
DATABASES = {
'default': {
'ENGINE': 'sentry.db.postgres',
'NAME': 'sentry',
'USER': 'sentry',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
$ yum install -y redis
$ vim /etc/redis.conf
bind 127.0.0.1
port 6379
requirepass 123456
$ systemctl start redis
$ source /home/chunyu/workspace/ENV_sentry/bin/activite
# 创建sentry数据库
$ createdb -E utf-8 sentry
# 初始化数据结构
$ sentry upgrade
# 创建数据库管理员用户
$ sentry createuser
chunyu
# 测试:登录数据库
$ psql -Uchunyu -h127.0.0.1 -p5432 -dsentry -W
用户 chunyu 的口令:123
psql (10.6)
输入 "help" 来获取帮助信息.
sentry=#
(登录成功)
server {
listen 80;
server_name sentry.chunyu.club;
location / {
proxy_pass http://192.168.228.129:9000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
参考:https://docs.sentry.io/server/nginx/
sentry运行三个进程,sentry-web、sentry-worker、sentry-cron,将其加入supervisord更便于管理:
supervisord配置文件:
[unix_http_server]
file=/home/chunyu/workspace/sentry/supervisor.sock
[supervisord]
logfile=/home/chunyu/workspace/sentry/logs/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/home/chunyu/workspace/sentry/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
user=chunyu
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///home/chunyu/workspace/sentry/supervisor.sock ; use a unix:// URL for a unix socket
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/chunyu/workspace/sentry/logs/sentry.slog
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=3
[program:sentry-web]
directory=/home/chunyu/workspace/sentry/
environment=SENTRY_CONF="/etc/sentry"
command=/home/chunyu/workspace/ENV_sentry/bin/sentry run web
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/chunyu/workspace/sentry/logs/sentry-web.slog
[program:sentry-worker]
directory=/home/chunyu/workspace/sentry/
environment=SENTRY_CONF="/etc/sentry"
command=/home/chunyu/workspace/ENV_sentry/bin/sentry run worker
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/chunyu/workspace/sentry/logs/sentry-worker.slog
[program:sentry-cron]
directory=/home/chunyu/workspace/sentry/
environment=SENTRY_CONF="/etc/sentry"
command=/home/chunyu/workspace/ENV_sentry/bin/sentry run cron
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/chunyu/workspace/sentry/logs/sentry-cron.slog
定期清理历史数据,释放存储资源。
# 每天凌晨3点,删除1周前的sentry
0 3 * * * /home/chunyu/workspace/ENV_sentry/bin/sentry cleanup --days=7
参考:https://docs.sentry.io/server/cli/cleanup/
ldap集成依赖于第三方提供的插件,具体配置方法参考链接:https://github.com/Banno/getsentry-ldap-auth
插件管理:https://docs.sentry.io/server/plugins/
注意: 所有的插件都是以最新版本的sentry进行的测试,不保证对旧版本的sentry完全兼容,部署插件的时候注意调研测试。
公司项目很多,并不是每个开发人员都负责所有的项目,默认情况下是接收所有项目的通知,如果想取消某项目的通知,操作如下:
监控脚本 下载
# sentry机器
* * * * * root /usr/bin/python /root/scripts/sentry_monitor.py
因为sentry使用nginx做的代理,所有请求都会通过nginx转发,所以可以通过nginx日志来对sentry数量进行统计,每个项目对应的API接口是唯一的,所以通过API接口进行过滤,将数据上报到Open-falcon,配置告警策略。接口对应如下:
项目名称 | API接口号 |
---|---|
medweb | 3 |
op | 24 |
事件采集脚本如下:
#!/bin/bash
#sentry数量统计
apis="3 24"
for api in $apis;do
ts=`date +%s`;
t=`LC_ALL=en_US.UTF-8 date '+%d/%b/%Y:%H:%M' -d "-1 minute"` # 每分钟采集一次日志
if [ $api -eq 3 ];then
project=medweb_online
elif [ $api -eq 24 ];then
project=op
fi
valus=`tail -1000000 /usr/local/nginx/logs/access.log |egrep 'sentry.chunyu.me' |egrep $t |egrep 'POST'|egrep "/api/$api/" |wc -l`
curl -X POST -d "[{\"metric\": \"sentry\", \"endpoint\": \"sentry\", \"timestamp\": $ts,\"step\": 60,\"value\": $valus,\"type\":\"sentry\",\"counterType\": \"GAUGE\",\"tags\": \"project=$project\"}]" http://127.0.0.1:1988/v1/push
(adsbygoogle = window.adsbygoogle || []).push({});