Sentry是一家开源公司,提供了一个应用程序监视平台,可以帮助您实时识别问题。 在这里,我们涵盖了有关产品,平台集成和自托管Sentry的所有内容。【通俗讲的讲】我们可以使用Sentry平台实时地监控我们的应用或服务、并且可以收集相关运行时错误或异常日志信息,在第一时间将错误信息推送至我们的后台或邮件组等。这样不仅能主动帮我们第一时间发现线上问题,而且很好的保留了异常发生时的“现场”,更有助于我们快速定位问题根源,提高解决问题的效率,逐步提高产品的稳定性和用户体验。
https://sentry.io/welcome/ https://docs.sentry.io https://docs.sentry.io/guides/ https://docs.sentry.io/platforms/go/
简单到仅用一个init方法就可以搞定。
https://github.com/getsentry/docker-sentry
https://github.com/getsentry/onpremise
https://github.com/docker-library/docs/tree/master/sentry
1.docker run -d --name sentry-redis redis
2.docker run -d --name sentry-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=sentry postgres
3.docker run --rm sentry config generate-secret-key
这一步会生成一个密钥key,比如 *l%)ti9=v#!pt__!#hpz3g33tq3hy2afpv%sda6^^ghr24)k4q ,先记下来,后面步骤中多个容器会共享该key
4.docker run -it --rm -e SENTRY_SECRET_KEY='*l%)ti9=v#!pt__!#hpz3g33tq3hy2afpv%sda6^^ghr24)k4q' --link sentry-postgres:postgres --link sentry-redis:redis sentry upgrade
5.docker run -d -p 9090:9000 --name my-sentry -e SENTRY_SECRET_KEY='*l%)ti9=v#!pt__!#hpz3g33tq3hy2afpv%sda6^^ghr24)k4q' --link sentry-redis:redis --link sentry-postgres:postgres sentry
6.docker run -d --name sentry-cron -e SENTRY_SECRET_KEY='*l%)ti9=v#!pt__!#hpz3g33tq3hy2afpv%sda6^^ghr24)k4q' --link sentry-postgres:postgres --link sentry-redis:redis sentry run cron
7.docker run -d --name sentry-worker-1 -e SENTRY_SECRET_KEY='*l%)ti9=v#!pt__!#hpz3g33tq3hy2afpv%sda6^^ghr24)k4q' --link sentry-postgres:postgres --link sentry-redis:redis sentry run worker
上述几步完成后,查看下刚才创建的容器以及运行状态是否完好:
➜ /tmp docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ed2439c527c sentry "/entrypoint.sh run …" 3 hours ago Up 3 hours 9000/tcp sentry-worker-1
f3779877d102 sentry "/entrypoint.sh run …" 3 hours ago Up 3 hours 9000/tcp sentry-cron
9c190bd613a6 sentry "/entrypoint.sh run …" 3 hours ago Up 3 hours 0.0.0.0:9090->9000/tcp my-sentry
188c744c2f21 postgres "docker-entrypoint.s…" 4 hours ago Up 4 hours 5432/tcp sentry-postgres
abad0564ca2b redis "docker-entrypoint.s…" 4 hours ago Up 4 hours 6379/tcp sentry-redis
然后检查下宿主机防火墙或安全组策略,放行上述映射的9090端口;
http://localhost:9090/auth/login/sentry/
如果成功,您会看到如下页面
然后用上述第4步中填入第账户登陆并配置,成功后进入首页
打开左上角第个人中心,然后点击User settings->language-> 选择简体中文即可.
import (
"errors"
"time"
"github.com/getsentry/sentry-go"
)
func main() {
sentry.Init(sentry.ClientOptions{
Dsn: "http://60e443996c464def804082d3c7e04de3@localhost:9090/2",
})
sentry.CaptureException(errors.New("my error"))
// Since sentry emits events in the background we need to make sure
// they are sent before we shut down
sentry.Flush(time.Second * 5)
}
本地运行并模拟错误将日志上报
go run go-sentry-test.go
import (
"fmt"
"net/http"
"github.com/getsentry/sentry-go"
sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
)
// To initialize Sentry's handler, you need to initialize Sentry itself beforehand
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://079843729765443f982ef8f3aea18766@o396662.ingest.sentry.io/5250281",
}); err != nil {
fmt.Printf("Sentry initialization failed: %v\n", err)
}
// Create an instance of sentryfasthttp
sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{})
// After creating the instance, you can attach the handler as one of your middleware
fastHTTPHandler := sentryHandler.Handle(func(ctx *fasthttp.RequestCtx) {
panic("y tho")
})
fmt.Println("Listening and serving HTTP on :3000")
// And run it
if err := fasthttp.ListenAndServe(":3000", fastHTTPHandler); err != nil {
panic(err)
}
上述是自己完整到搭建一下sentry,如果仅是为了学习或测试一下,并想节省时间,则可以直接在官方的后台https://sentry.io/signup/
注册一个账号,然后将sdk潜入到自己的测试应用中看效果,如
package main
import (
"log"
"time"
"github.com/getsentry/sentry-go"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
// Either set your DSN here or set the SENTRY_DSN environment variable.
Dsn: "https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@xxxxxxx.ingest.sentry.io/xxxxxxx",//这里需要替换为您自己的Dsn
// Enable printing of SDK debug messages.
// Useful when getting started or trying to figure something out.
Debug: true,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
// Set the timeout to the maximum duration the program can afford to wait.
defer sentry.Flush(2 * time.Second)
//sentry.CaptureException("自定义运行时错误1")
sentry.CaptureMessage("自定义error")
}
效果如下