業務監控工具 Sentry 的搭建與使用
官方網址
Sentry 簡介
Sentry 是一個開源的實時錯誤報告工具,支持 web 前后端、移動應用以及游戲,支持 Python、OC、Java、Go、Node、Django、RoR 等主流編程語言和框架 ,還提供了 GitHub、Slack、Trello 等常見開發工具的集成。
Sentry 服務支持多用戶、多團隊、多應用管理,每個應用都對應一個 PROJECT_ID,以及用於身份認證的 PUBLIC_KEY 和 SECRET_KEY。由此組成一個這樣的 DSN:
{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}
PROTOCOL 通常會是 http 或者 https,HOST 為 Sentry 服務的主機名和端口,PATH 通常為空。
環境依賴
- Redis 搭建 / RabbitMQ 的搭建
- MySQL / PostgreSQL
- Python 虛擬環境
安裝教程
-
Redis 的安裝
參考文檔:
https://linux.cn/article-6719-1.html
http://www.jianshu.com/p/aec247ffbe51 - MySQL 的安裝
- 略
-
Python 虛擬環境的安裝
因為 Sentry 依賴的 Python 庫比較多,為了避免對系統環境的污染,與現有的Python有沖突,建議還是將 Sentry 安裝在虛擬環境中。
A. Python 庫文件: python-setuptools, python-dev, build-essential, python-pip
B. 安裝虛擬環境: pip install virtualenv
安裝完成后,可以直接 virtualenv xxx 即可在當前目錄下生成一個虛擬環境xxx目錄,進入到目錄中,source bin/activate 即可激活當前虛擬環境。
C. 選擇安裝 virtualenvwrapper: pip install virtualenvwrapper
安裝完成后,建立個虛擬環境安裝存儲的目錄,建議是 $HOME/.virtualenv 目錄,配置下 .bashrc 文件,文件末尾添加:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
source .bashrc后,運行 mkvirtualenv xxx 即可建立虛擬環境。退出運行 deactivate。這樣,就不需要再進入到虛擬環境目錄運行 source xxx/activate,直接在終端輸入 workon xxx 即可。
- Sentry
在虛擬環境下,直接運行pip install sentry
即可。
這樣,安裝基本上就結束了。接下來需要配置下 sentry。
配置 Sentry
運行 sentry init
, 會在 $HOME 下生成 .sentry
目錄。進入 .sentry 后,需要修改數據庫配置(當然,你也可以不改,直接使用 PostgreSQL):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 這里換成了 MySQL,默認是 pq
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': 'xxx',
'PORT': 'xxx',
}
}
端口和隊列等可以自行指定。這里,我指定的是15000。下面是一個配置參考:
# This file is just Python, with a touch of Django which means
# you can inherit and tweak settings to your hearts content.
from sentry.conf.server import *
import os.path
CONF_ROOT = os.path.dirname(__file__)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_sentry',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
'AUTOCOMMIT': True,
'ATOMIC_REQUESTS': False,
}
}
# You should not change this setting after your database has been created
# unless you have altered all schemas first
SENTRY_USE_BIG_INTS = True
# If you're expecting any kind of real traffic on Sentry, we highly recommend
# configuring the CACHES and Redis settings
########### # General # ###########
# Instruct Sentry that this install intends to be run by a single organization
# and thus various UI optimizations should be enabled.
SENTRY_SINGLE_ORGANIZATION = True
DEBUG = False
######### # Cache # #########
# Sentry currently utilizes two separate mechanisms. While CACHES is not a
# requirement, it will optimize several high throughput patterns.
# If you wish to use memcached, install the dependencies and adjust the config
# as shown:
#
# pip install python-memcached
#
# CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
# 'LOCATION': ['127.0.0.1:11211'],
# }
# }
# A primary cache is required for things such as processing events
SENTRY_CACHE = 'sentry.cache.redis.RedisCache'
######### # Queue # #########
# See https://docs.sentry.io/on-premise/server/queue/ for more
# information on configuring your queue broker and workers. Sentry relies
# on a Python framework called Celery to manage queues.
CELERY_ALWAYS_EAGER = False
BROKER_URL = 'redis://127.0.0.1:6379'
############### # Rate Limits # ###############
# Rate limits apply to notification handlers and are enforced per-project
# automatically.
SENTRY_RATELIMITER = 'sentry.ratelimits.redis.RedisRateLimiter'
##################
# Update Buffers #
##################
# Buffers (combined with queueing) act as an intermediate layer between the
# database and the storage API. They will greatly improve efficiency on large
# numbers of the same events being sent to the API in a short amount of time.
# (read: if you send any kind of real data to Sentry, you should enable buffers)
SENTRY_BUFFER = 'sentry.buffer.redis.RedisBuffer'
########## # Quotas # ##########
# Quotas allow you to rate limit individual projects or the Sentry install as
# a whole.
SENTRY_QUOTAS = 'sentry.quotas.redis.RedisQuota'
########
# TSDB #
########
# The TSDB is used for building charts as well as making things like per-rate
# alerts possible.
SENTRY_TSDB = 'sentry.tsdb.redis.RedisTSDB'
########### # Digests # ###########
# The digest backend powers notification summaries.
SENTRY_DIGESTS = 'sentry.digests.backends.redis.RedisBackend'
################ # File storage # ################
# Any Django storage backend is compatible with Sentry. For more solutions see
# the django-storages package: https://django-storages.readthedocs.org/en/latest/
SENTRY_FILESTORE = 'django.core.files.storage.FileSystemStorage'
SENTRY_FILESTORE_OPTIONS = {
'location': '/tmp/sentry-files',
}
##############
# Web Server #
##############
# If you're using a reverse SSL proxy, you should enable the X-Forwarded-Proto
# header and uncomment the following settings
# SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# SESSION_COOKIE_SECURE = True
# CSRF_COOKIE_SECURE = True
# If you're not hosting at the root of your web server,
# you need to uncomment and set it to the path where Sentry is hosted.
# FORCE_SCRIPT_NAME = '/sentry'
SENTRY_WEB_HOST = '0.0.0.0'
SENTRY_WEB_PORT = 5000
SENTRY_WEB_OPTIONS = {
# 'workers': 3, # the number of web workers
# 'protocol': 'uwsgi', # Enable uwsgi protocol instead of http
}
LANGUAGES = (
('en', gettext_noop('English')),
('zh-cn', gettext_noop('Simplified Chinese')),
# ('zh-cn', gettext_noop('Traditional Chinese')),
)
運行 Sentry
- 初始化:
sentry upgrade
注意,這里可能會出現錯誤,可以參考下面遇到的坑。初始化的時候,需要設置一個 superuser 角色,直接按提示操作即可。
- 啟動 web 進程:
sentry run web
- 啟動 worker 進程:
sentry run worker
- 這時候,通過 IP:PORT 的形式訪問下,填寫剛才填寫的用戶名和密碼即可登錄。登錄后,我們創建一個 project。我這里設置的是 Odeon_Dev,接下來選擇項目,我選擇的是 Django。這個時候,會彈出一個在項目中配置的教程。我們按照提示操作即可。
測試環境的地址:
http://localhost:5000/sentry/odeon_dev/
項目中配置 Sentry
按照上面的操作,Sentry 服務就可以 run 起來了。接下來需要在 Odeon 的項目中配置下 Sentry 環境即可。這里,我們需要引入一個新包: raven。我安裝的 是 raven 6.1.0
安裝:
A. 可以直接下載 raven 包,將其導入到環境中;
B. 直接指令安裝: build/env/bin/pip install raven==6.1.0
項目配置:
直接將 sentry 創建 project 時返回的信息放入 settings 文件中即可
import os
import raven
RAVEN_CONFIG = {
'dsn': 'http://fxxx:xxx@localhost:xxx/2',
'release': raven.fetch_git_sha(os.path.dirname(os.pardir)),
}
至此,整個 Sentry 的搭建和項目中需要的配置就完全 OK 了。
當然,也可以更完善一下,比如:
- 利用 Nginx 反向代理使用域名訪問服務;
- 利用 supervisor 來起 Sentry 服務等。
接下來,就是按需使用了。
遇到的坑
-
sentry默認使用 PostgreSQL。我用的是 mysql。運行 sentry upgrade 的時候,發現運行到 db migration 的時候拋了異常,查閱發現是 db engine 使用的是MyISAM,不支持 transaction 導致的。這里需要注意下,我將 engine 指定為 InnoDB后,執行 migration 的時候錯誤消失。
-
頁面打開后,提示 worker 沒有正常運行。發現沒有啟動 worker。我們手動啟動下 worker,啟動時,需要在系統中將 C_FORCE_ROOT 設置為 true。詳細點擊: 參考鏈接
參考鏈接:
- https://yunsonbai.top/2016/05/30/django-sentry/
- https://tech.liuchao.me/2015/06/monitor-service-error-logs-by-using-sentry/