安装、迁移基于 Sentry 9 的错误跟踪系统

我给公司搭建错误跟踪系统,方便排查代码问题,技术选型为 Sentry 9,因为其开源、轻量、能满足业务需求,相比最新版 Sentry,其搭建、维护也更容易,最新版 Sentry 在我们业务中有点杀鸡用牛刀了

安装 Sentry

使用 Docker Compose 安装 Sentry 9

docker-compose.yml

version: '2'

volumes:
pgdb:

services:
redis:
image: redis
container_name: sentry-redis

postgres:
image: postgres
container_name: sentry-postgres
ports:
- 5432:5432
environment:
POSTGRES_USER: sentry
POSTGRES_PASSWORD: sentry
POSTGRES_DB: sentry
TZ: Asia/Shanghai
volumes:
- pgdb:/var/lib/postgresql/data

sentry:
image: sentry
container_name: sentry
links:
- redis
- postgres
ports:
- 9090:9000
environment:
SENTRY_SECRET_KEY: '5qv7@d1-dx6t!^1r3%5fx7wdncq4a3)44c@v7(dt%7eqiu8!87'
SENTRY_POSTGRES_HOST: postgres
SENTRY_DB_USER: sentry
SENTRY_DB_PASSWORD: sentry
SENTRY_REDIS_HOST: redis
TZ: Asia/Shanghai
# 邮件配置
SENTRY_EMAIL_HOST: 'smtp.163.com'
SENTRY_EMAIL_USER: '[email protected]'
SENTRY_SERVER_EMAIL: '[email protected]'
SENTRY_EMAIL_PASSWORD: '授权码'
SENTRY_EMAIL_USE_TLS: false
SENTRY_EMAIL_PORT: 25

cron:
image: sentry
container_name: sentry-cron
links:
- redis
- postgres
command: "sentry run cron"
environment:
SENTRY_SECRET_KEY: '5qv7@d1-dx6t!^1r3%5fx7wdncq4a3)44c@v7(dt%7eqiu8!87'
SENTRY_POSTGRES_HOST: postgres
SENTRY_DB_USER: sentry
SENTRY_DB_PASSWORD: sentry
SENTRY_REDIS_HOST: redis
TZ: Asia/Shanghai

worker:
image: sentry
container_name: sentry-work-1
links:
- redis
- postgres
command: "sentry run worker"
environment:
SENTRY_SECRET_KEY: '5qv7@d1-dx6t!^1r3%5fx7wdncq4a3)44c@v7(dt%7eqiu8!87'
SENTRY_POSTGRES_HOST: postgres
SENTRY_DB_USER: sentry
SENTRY_DB_PASSWORD: sentry
SENTRY_REDIS_HOST: redis
TZ: Asia/Shanghai
# 邮件配置
SENTRY_EMAIL_HOST: 'smtp.163.com'
SENTRY_EMAIL_USER: '[email protected]'
SENTRY_SERVER_EMAIL: '[email protected]'
SENTRY_EMAIL_PASSWORD: '授权码'
SENTRY_EMAIL_USE_TLS: false
SENTRY_EMAIL_PORT: 25
  • sentry: sentry 应用
  • sentry-work-1: sentry 使用 celery,sentry-work-1 是消费者
  • sentry-cron: 执行定时任务
  • sentry-postgres: 数据存储

启动:

docker comopse up -d

Nginx 配置 Sentry 域名

server {
server_name [your_domain];
listen 80;
location / {
proxy_pass [your_host_ip]:9090;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}

初始化数据库数据

使用 Django 迁移初始化数据库数据

docker exec -it sentry sentry upgrade

创建超级用户

docker-compose exec sentry sentry createuser --email [email protected] --password your_password --superuser

参考链接

配置 Sentry

域名配置

https://your-sentry-domain/manage/settings/ 中修改

邮件配置

可直接配置环境变量实现,也可以修改 sentry 和 sentry-work-1 容器中 /etc/sentry/config.yml 中邮件的配置实现

# mail.backend: 'smtp'  # Use dummy if you want to disable email entirely
# mail.host: 'localhost'
# mail.port: 25
# mail.username: ''
# mail.password: ''
# mail.use-tls: false
# The email address to send on behalf of
# mail.from: 'root@localhost'

sentry 容器配置生效示例:

遇到一个坑:测试邮件可以发送,但个人邮件不能发送

  • 原因:测试邮件在 sentry 容器中发送,但个人邮件在 sentry-work-1 容器中发送。只配置了 sentry 容器,没有在 sentry-work-1 容器中配置邮件信息,所以个人邮件不能发送
  • 解决方式:在 sentry-work-1 中也配置邮件信息
  • 相关链接:http://witmax.cn/docker-sentry.html

使用 webhook 将警报接入飞书

在飞书群中接入机器人,再在「飞书捷径」中配置,项目配置飞书捷径链接后,当出现指定错误时,在群里告警

用户密码修改

可在后台管理界面修改用户密码

几个问题

1、可能的坑:日志报错 relation “sentry_projectkey” does not exist

ProgrammingError('relation "sentry_projectkey" does not exist\nLINE 1: ...te_limit_window", "sentry_projectkey"."data" FROM "sentry_pr...\n                                                             ^\n',)
SQL: SELECT "sentry_projectkey"."id", "sentry_projectkey"."project_id", "sentry_project

原因:没有初始化数据库数据

解决方式:初始化数据库数据

docker exec -it sentry sentry upgrade

2、页面报错

NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

#### Uhhhh

Something went horribly wrong rendering this page. We use a decent error reporting service so this will probably be fixed soon. Unless our error reporting service is also broken. That would be awkward. Anyway, we apologize for the inconvenience.

原因:浏览器前端静态资源缓存的问题

解决方式:清浏览器缓存

3、通知发送问题

希望配置每个 Alert 都能通过告警机器人中发送到飞书群中,但出现有的可以发送,有的不可以发送,还没有找到原因

迁移 Sentry

为提供稳定性和访问速度,我将 Sentry 从物理机到腾讯云上

迁移用户、项目数据命令:

# 导出 Sentry 现有数据到 backup.json 中
docker exec -it sentry sentry export backup.json

# 将 backup.json 中数据导入到新 Sentry 中
docker exec -it sentry sentry import backup.json
  • 可使用 Another Redis Desktop Manager APP 连接 Redis

可能的坑

迁移后出现新用户注册报错的情况

原因:用户主键重复

解决方式:使用 pgAdmin4 工具连接 PostgreSQL 数据库,再执行以下操作

SELECT * FROM auth_user;

// 查看当前主键
SELECT last_value FROM "auth_user_id_seq";

// 修改
SELECT setval('auth_user_id_seq', 22, false);

相关链接