Skip to content

Commit

Permalink
Update sentry package and fix catch panic (#1001)
Browse files Browse the repository at this point in the history
* Update sentry package and fix catch panic

* Fix: sentry for indexer
  • Loading branch information
aopoltorzhicky committed Nov 7, 2023
1 parent 3fabd33 commit f14d399
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
19 changes: 14 additions & 5 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/baking-bad/bcdhub/internal/periodic"
"github.com/baking-bad/bcdhub/internal/profiler"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -45,9 +46,14 @@ func newApp() *app {
logger.New(cfg.LogLevel)

if cfg.API.SentryEnabled {
helpers.InitSentry(cfg.Sentry.Debug, cfg.Sentry.Environment, cfg.Sentry.URI)
helpers.SetTagSentry("project", cfg.API.ProjectName)
defer helpers.CatchPanicSentry()
helpers.InitSentry(helpers.SentryConfig{
DSN: cfg.Sentry.URI,
Debug: cfg.Sentry.Debug,
Env: cfg.Sentry.Environment,
Tags: map[string]string{
"project": cfg.API.ProjectName,
},
})
}

app := new(app)
Expand Down Expand Up @@ -114,11 +120,14 @@ func (api *app) makeRouter() {
r.Use(corsSettings())
}

r.Use(gin.Recovery())

if api.Config.API.SentryEnabled {
r.Use(helpers.SentryMiddleware())
r.Use(sentrygin.New(sentrygin.Options{
Repanic: true,
}))
}

r.Use(gin.Recovery())
r.Use(ginLogger.SetLogger())
r.Use(timeout.New(
timeout.WithTimeout(30*time.Second),
Expand Down
10 changes: 8 additions & 2 deletions cmd/indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ func main() {
logger.New(cfg.LogLevel)

if cfg.Indexer.SentryEnabled {
helpers.InitSentry(cfg.Sentry.Debug, cfg.Sentry.Environment, cfg.Sentry.URI)
helpers.SetTagSentry("project", cfg.Indexer.ProjectName)
helpers.InitSentry(helpers.SentryConfig{
DSN: cfg.Sentry.URI,
Debug: cfg.Sentry.Debug,
Env: cfg.Sentry.Environment,
Tags: map[string]string{
"project": cfg.Indexer.ProjectName,
},
})
defer helpers.CatchPanicSentry()
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/dipdup-net/go-lib v0.3.5
github.com/ebellocchia/go-base58 v0.1.0
github.com/fatih/color v1.13.0
github.com/getsentry/sentry-go v0.13.0
github.com/getsentry/sentry-go v0.25.0
github.com/gin-contrib/cache v1.2.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-contrib/logger v0.2.6
Expand All @@ -25,7 +25,7 @@ require (
github.com/karlseguin/ccache v2.0.3+incompatible
github.com/lib/pq v1.10.9
github.com/machinebox/graphql v0.2.2
github.com/microcosm-cc/bluemonday v1.0.20
github.com/microcosm-cc/bluemonday v1.0.23
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.30.0
Expand All @@ -39,7 +39,7 @@ require (
github.com/yhirose/go-peg v0.0.0-20210804202551-de25d6753cf1
go.uber.org/mock v0.2.0
golang.org/x/crypto v0.14.0
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
golang.org/x/time v0.3.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/getsentry/sentry-go v0.13.0 h1:20dgTiUSfxRB/EhMPtxcL9ZEbM1ZdR+W/7f7NWD+xWo=
github.com/getsentry/sentry-go v0.13.0/go.mod h1:EOsfu5ZdvKPfeHYV6pTVQnsjfp30+XA7//UooKNumH0=
github.com/getsentry/sentry-go v0.25.0 h1:q6Eo+hS+yoJlTO3uu/azhQadsD8V+jQn2D8VvX1eOyI=
github.com/getsentry/sentry-go v0.25.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
github.com/gin-contrib/cache v1.2.0 h1:WA+AJR4kmHDTaLLShCHo/IeWVmmGRZ3Lsr3JQ46tFlE=
github.com/gin-contrib/cache v1.2.0/go.mod h1:2KkFL8PSnPF3Tt5E2Jpc3HWuBAUKqGZnClCFMm0tXQI=
github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g=
Expand All @@ -108,8 +108,8 @@ github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjX
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
github.com/go-faster/errors v0.6.1 h1:nNIPOBkprlKzkThvS/0YaX8Zs9KewLCOSFQS5BU06FI=
Expand Down Expand Up @@ -274,8 +274,8 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/memcachier/mc/v3 v3.0.3 h1:qii+lDiPKi36O4Xg+HVKwHu6Oq+Gt17b+uEiA0Drwv4=
github.com/memcachier/mc/v3 v3.0.3/go.mod h1:GzjocBahcXPxt2cmqzknrgqCOmMxiSzhVKPOe90Tpug=
github.com/microcosm-cc/bluemonday v1.0.20 h1:flpzsq4KU3QIYAYGV/szUat7H+GPOXR0B2JU5A1Wp8Y=
github.com/microcosm-cc/bluemonday v1.0.20/go.mod h1:yfBmMi8mxvaZut3Yytv+jTXRY8mxyjJ0/kQBTElld50=
github.com/microcosm-cc/bluemonday v1.0.23 h1:SMZe2IGa0NuHvnVNAZ+6B38gsTbi5e4sViiWJyDDqFY=
github.com/microcosm-cc/bluemonday v1.0.23/go.mod h1:mN70sk7UkkF8TUr2IGBpNN0jAgStuPzlK76QuruE/z4=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/moby/patternmatcher v0.5.0 h1:YCZgJOeULcxLw1Q+sVR636pmS7sPEn1Qo2iAN6M7DBo=
Expand Down Expand Up @@ -533,8 +533,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 h1:ftMN5LMiBFjbzleLqtoBZk7KdJwhuybIU+FckUHgoyQ=
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
46 changes: 21 additions & 25 deletions internal/helpers/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ import (
"time"

"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

type SentryConfig struct {
DSN string
Debug bool
Env string
Tags map[string]string
}

// InitSentry -
func InitSentry(debug bool, environment, dsn string) {
func InitSentry(cfg SentryConfig) {
if err := sentry.Init(sentry.ClientOptions{
Dsn: dsn,
Environment: environment,
Debug: debug,
Dsn: cfg.DSN,
Environment: cfg.Env,
Debug: cfg.Debug,
AttachStacktrace: true,
BeforeSend: beforeSend,
Tags: cfg.Tags,
}); err != nil {
log.Err(err).Msg("Sentry initialization failed")
}
}

func beforeSend(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
log.Info().Msgf("[Sentry message] %s", event.Message)
log.Debug().Msgf("[Sentry message] %s", event.Message)
return event
}

// SentryMiddleware -
func SentryMiddleware() gin.HandlerFunc {
return sentrygin.New(sentrygin.Options{})
}

// SetTagSentry -
func SetTagSentry(key, value string) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
Expand All @@ -52,11 +53,14 @@ func SetUserIDSentry(id string) {
// CatchPanicSentry -
func CatchPanicSentry() {
if err := recover(); err != nil {
sentry.CurrentHub().WithScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelError)
})
sentry.CurrentHub().Recover(err)
sentry.Flush(time.Second * 5)
if hub := sentry.CurrentHub(); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelError)
})
if eventId := hub.Recover(err); eventId != nil {
sentry.Flush(time.Second * 5)
}
}
}
}

Expand Down Expand Up @@ -92,11 +96,3 @@ func LocalCatchErrorSentry(hub *sentry.Hub, err error) {
hub.CaptureException(err)
hub.Flush(time.Second * 5)
}

// LocalCatchPanicSentry -
func LocalCatchPanicSentry(hub *sentry.Hub) {
if err := recover(); err != nil {
hub.CaptureMessage(err.(string))
hub.Flush(time.Second * 5)
}
}

0 comments on commit f14d399

Please sign in to comment.