feat: better integrates with sentry

This commit is contained in:
Paul Pan 2023-07-20 11:39:14 +08:00
parent 7660069c34
commit 6617b6d68e
5 changed files with 39 additions and 2 deletions

View File

@ -84,3 +84,16 @@ func cleanupSentry(*cli.Context) error {
}
return nil
}
func PanicWrapper(f func(*cli.Context) error) func(*cli.Context) error {
return func(c *cli.Context) error {
defer func() {
if r := recover(); r != nil {
sentry.CaptureException(r.(error))
sentry.Flush(time.Second * 2)
log.Printf("Panic Captured: %v", r)
}
}()
return f(c)
}
}

View File

@ -32,13 +32,13 @@ func main() {
Name: "web",
Aliases: []string{"w"},
Usage: "start web api server",
Action: runServer,
Action: cmd.PanicWrapper(runServer),
},
{
Name: "runner",
Aliases: []string{"r"},
Usage: "start runner",
Action: runRunner,
Action: cmd.PanicWrapper(runRunner),
},
}

1
go.mod
View File

@ -29,6 +29,7 @@ require (
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/TheZeroSlave/zapsentry v1.17.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.9.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect

2
go.sum
View File

@ -4,6 +4,8 @@ github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6Xge
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/TheZeroSlave/zapsentry v1.17.0 h1:RIQCG89U7vWWZVmmCxeUz/g32WEcAYXUrXHoMlbSDmc=
github.com/TheZeroSlave/zapsentry v1.17.0/go.mod h1:D1YMfSuu6xnkhwFXxrronesmsiyDhIqo+86I3Ok+r64=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

View File

@ -3,6 +3,8 @@ package log
import (
"git.0x7f.app/WOJ/woj-server/internal/misc/config"
"git.0x7f.app/WOJ/woj-server/pkg/utils"
"github.com/TheZeroSlave/zapsentry"
"github.com/getsentry/sentry-go"
"github.com/samber/do"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@ -46,9 +48,28 @@ func NewService(i *do.Injector) (Service, error) {
return nil, err
}
srv.logger = attachSentry(srv.logger)
return srv, nil
}
func attachSentry(log *zap.Logger) *zap.Logger {
cfg := zapsentry.Configuration{
Level: zapcore.ErrorLevel,
EnableBreadcrumbs: true,
BreadcrumbLevel: zapcore.InfoLevel,
}
core, err := zapsentry.NewCore(cfg, zapsentry.NewSentryClientFromClient(sentry.CurrentHub().Client()))
if err != nil {
log.Warn("failed to init zap", zap.Error(err))
return log
}
log = zapsentry.AttachCoreToLogger(core, log)
return log.With(zapsentry.NewScope())
}
type service struct {
confService config.Service
logger *zap.Logger