1. 概述
Sentry's application monitoring platform helps every developer diagnose, fix, and optimize the performance of their code.
Sentry is a service that helps you monitor and fix crashes in realtime. The server is in Python, but it contains a full API for sending events from any language, in any application.
Sentry提供免费和收费的公网服务,也支持内网部署,各有利弊。
- 公网服务,节省很多维护和机器成本,并且保证了服务的可靠性。必要时,可用于备选方案或测试(内网sentry不可用时,可用于排除client问题)
- 内网部署,数据更加安全,对于内网微服务集群是刚需,如果不需要采集公网用户端的crash时
公网服务,可以在Sentry官网注册账号,创建Project后即可使用。
sentry with docker本文重点介绍内网部署流程和测试。
Sentry的内网部署,大致有两种方式:
- 低版本
9.x
部署,功能有限,但是占用资源也少,运行复杂度也低,适合低并发的场景 - 最新版本
21.x
部署,功能较全,占用资源也多,启动近30个container,适合高并发的场景。如果需要更加稳定可靠的服务,需要考虑改造成Sentry Cluster
2. 低版本部署
-
部署很简单,启动5个container即可正常使用。只是sentry版本很低,对client应该也有要求,需要用低版本。因此,不推荐使用此方法。
-
但是,此方法是否支持高版本的sentry服务部署,尚未尝试。
-
部署流程参考dockerhub
https://hub.docker.com/_/sentry
2.1. Start a Redis container
$ docker run -d --name sentry-redis redis
2.2. Start a Postgres container
$ docker run -d --name sentry-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=sentry postgres
2.3. Generate a new secret key to be shared by all sentry
containers. This value will then be used as the SENTRY_SECRET_KEY
environment variable.
$ docker run --rm sentry config generate-secret-key
2.4. If this is a new database, you'll need to run upgrade
$ docker run -it --rm -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-postgres:postgres --link sentry-redis:redis sentry upgrade
Note: the -it
is important as the initial upgrade will prompt to create an initial user and will fail without it
2.5. Now start up Sentry server
$ docker run -d --name my-sentry -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-redis:redis --link sentry-postgres:postgres sentry
2.6. The default config needs a celery beat and celery workers, start as many workers as you need (each with a unique name)
$ docker run -d --name sentry-cron -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-postgres:postgres --link sentry-redis:redis sentry run cron
$ docker run -d --name sentry-worker-1 -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-postgres:postgres --link sentry-redis:redis sentry run worker
3. 最新版本部署
Sentry官方支持企业内网部署:
https://github.com/getsentry/onpremise
3.1. 准备工作
- Docker 19.03.6+
- Compose 1.24.1+
- 4 CPU Cores
- 8 GB RAM
- 20 GB Free Disk Space
3.2. 部署流程
$ git clone https://github.com/getsentry/self-hosted.git
$ cd self-hosted
$ ./install.sh
$ docker-compose up -d
如果需要配置geolocation,可以参考:https://develop.sentry.dev/self-hosted/geolocation/
单独创建用户:docker-compose run --rm web createuser
3.3. 可能遇到的问题
3.3.1. Email配置
修改文件:sentry/config.yml
# mail.backend: 'smtp' # Use dummy if you want to disable email entirely
mail.host: 'smtp.exmail.qq.com'
mail.port: 465
mail.username: 'noreply@aawwee.cn'
mail.password: 'xxxeeesss'
mail.use-tls: false
mail.use-ssl: true
# NOTE: The following 2 configs (mail.from and mail.list-namespace) are set
# through SENTRY_MAIL_HOST in sentry.conf.py so remove those first if
# you want your values in this file to be effective!
# The email address to send on behalf of
mail.from: 'noreply@aawwee.cn'
3.3.2. proxy问题
如果公司是通过http_proxy访问公网,那么,在./install.sh
时,因为要build docker image,所以需要修改~/.docker/config.json
,增加proxies
设置如下
{
"proxies":
{
"default":
{
"httpProxy": "http://127.0.0.1:3001",
"httpsProxy": "http://127.0.0.1:3001",
"noProxy": "*.test.example.com,.example2.com,127.0.0.0/8"
}
}
}
在启动sentry服务时,需要注释掉上述proxies
设置,避免出现运行时域名解析或IP路由异常。
- 参考docker client proxy设置
https://docs.docker.com/network/proxy/#configure-the-docker-client
4. 测试
4.1. Python
- run
$ http_proxy='' python test.py
- code
# py38
import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk import capture_message
sentry_logging = LoggingIntegration(
level=logging.INFO,
event_level=logging.ERROR
)
sentry_sdk.init(
"http://388ceb74a20847028c81dfc616843556@10.211.28.93:9000/1",
traces_sample_rate=1.0,
integrations=[sentry_logging],
debug=True,
)
capture_message('Something went wrong!')
division_by_zero = 1 / 0
4.2. nodejs
- run
$ http_proxy='' node server/test
- code
const Sentry = require("@sentry/node")
Sentry.init({
dsn: "http://388ceb74a20847028c81dfc616843556@10.211.28.93:9000/1",
appName: 'Sentry Test',
tracesSampleRate: 1.0,
debug: true,
});
Sentry.captureMessage('Hello, sentry world!')
网友评论