sentry
sentry是一个跨平台
的异常上报监控系统。sentry主要用于实时监控的应用服务,收集相关应用服务在运行状态时出现的异常或者错误日志信息,并且sentry会通过自身集成的通知渠道将错误信息推送给维护人员。
sentry收集到的异常或者错误日志,会在告警规则的匹配下,第一时间让维护人员接收到服务运行的异常信息,便于快速定位问题以及解决问题。
sentry支持绝大多数的语言以及框架,使用相对便捷。
组件架构
安装
准备sentry的工作目录
mkdir sentry && cd $_
touch sentry.env docker-compose.yml # 分别填充以下内容
DotEnv
sentry.env
SENTRY_SECRET_KEY=0x4r6f^0ak5w_3)w1ybh2qbr4@-0tr33ey)lf*e5_cq55xae&5-)r*=9
DB_PASSWORD="bw1knjWnUA"
DB_USER=sentry
docker-compose
version: '2'
volumes:
pgdb:
services:
redis:
image: redis:alpine
restart: always
#command: /bin/sh -c "redis-server --requirepass $$REDIS_HOST_PASSWORD"
postgres:
image: postgres
environment:
POSTGRES_USER: $$DB_USER
POSTGRES_PASSWORD: $$DB_PASSWORD
POSTGRES_DB: sentry
volumes:
- pgdb:/var/lib/postgresql/data
env_file:
- sentry.env
sentry:
image: sentry
links:
- redis
- postgres
ports:
- 9000:9000
environment:
SENTRY_SECRET_KEY: $$SENTRY_SECRET_KEY
SENTRY_POSTGRES_HOST: postgres
SENTRY_DB_USER: $$DB_USER
SENTRY_DB_PASSWORD: $$DB_PASSWORD
SENTRY_REDIS_HOST: redis
env_file:
- sentry.env
cron:
image: sentry
links:
- redis
- postgres
command: "sentry run cron"
environment:
SENTRY_SECRET_KEY: $$SENTRY_SECRET_KEY
SENTRY_POSTGRES_HOST: postgres
SENTRY_DB_USER: $$DB_USER
SENTRY_DB_PASSWORD: $$DB_PASSWORD
SENTRY_REDIS_HOST: redis
env_file:
- sentry.env
worker:
image: sentry
links:
- redis
- postgres
command: "sentry run worker"
environment:
SENTRY_SECRET_KEY: $$SENTRY_SECRET_KEY
SENTRY_POSTGRES_HOST: postgres
SENTRY_DB_USER: $$DB_USER
SENTRY_DB_PASSWORD: $$DB_PASSWORD
SENTRY_REDIS_HOST: redis
env_file:
- sentry.env
- 生成sentry secret key填充到sentry.env中的
SENTRY_SECRET_KEY
docker run --rm sentry config generate-secret-key
- 初始化数据库以及创建登录用户
docker-compose up -d # 部署sentry以及相关依赖的服务
docker-compose exec sentry sentry upgrade ## create user and password.
sentry dashboard使用
- 使用前面创建的用户以及密码登录
- 创建项目
- 一个配置demo
大概就是这些,具体的各语言的SDK还要看对应的文档以及源码。
go sentry测试
本实例,将通过创建一个go语言编写的demo服务,测试错误上报到sentry的效果
- 创建go项目
mkdir go-sentry-gin
cd $_
- 初始化模块
go mod init go-gin-sentry
go get github.com/getsentry/sentry-go
go get github.com/getsentry/sentry-go/gin
- source code
package main
import (
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "http://c0056580f26c469c8cc31d780d1995a8@localhost:9000/6",
Environment: "dev",
Release: "go-sentry-gin@0.0.1",
Debug: true,
})
if err != nil {
sentry.CaptureMessage("Sentry initialization failed." + err.Error())
}
r := gin.Default()
r.Use(sentrygin.New(sentrygin.Options{}))
r.Use(func(ctx *gin.Context) {
if hub := sentrygin.GetHubFromContext(ctx); hub != nil {
hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
ctx.Next()
}
})
r.GET("/demo", func(c *gin.Context) {
if hub := sentrygin.GetHubFromContext(c); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
})
c.Status(http.StatusOK)
}
panic("error occured")
})
r.Run()
}
运行后,访问服务路由,错误日志信息即上报到sentry上
打开项目详情页,即可看到整个项目的状态信息