Add sentry wrapper, improve recover() logic
This commit is contained in:
parent
0707f139c4
commit
aabff3df6e
@ -5,13 +5,13 @@ import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/titpetric/factory"
|
||||
"github.com/titpetric/factory/logger"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -5,11 +5,11 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
sentry "github.com/getsentry/sentry-go"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/internal/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
@ -5,13 +5,13 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
sentry "github.com/getsentry/sentry-go"
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/internal/payload"
|
||||
"github.com/cortezaproject/corteza-server/internal/payload/outgoing"
|
||||
"github.com/cortezaproject/corteza-server/messaging/internal/repository"
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
sentry "github.com/getsentry/sentry-go"
|
||||
"github.com/go-chi/chi"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/internal/repository"
|
||||
"github.com/cortezaproject/corteza-server/messaging/internal/service"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
)
|
||||
|
||||
func middlewareAllowedAccess(next http.Handler) http.Handler {
|
||||
|
||||
@ -5,7 +5,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
sentry "github.com/getsentry/sentry-go"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
@ -17,6 +16,7 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/messaging/internal/service"
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
@ -5,8 +5,6 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/internal/auth"
|
||||
"github.com/cortezaproject/corteza-server/internal/http"
|
||||
"github.com/cortezaproject/corteza-server/internal/mail"
|
||||
@ -22,29 +20,6 @@ func InitGeneralServices(smtpOpt *options.SMTPOpt, jwtOpt *options.JWTOpt, httpC
|
||||
)
|
||||
}
|
||||
|
||||
func InitSentry(sentryOpt *options.SentryOpt) error {
|
||||
if sentryOpt.DSN == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return sentry.Init(sentry.ClientOptions{
|
||||
Dsn: sentryOpt.DSN,
|
||||
Debug: sentryOpt.Debug,
|
||||
AttachStacktrace: sentryOpt.AttachStacktrace,
|
||||
SampleRate: sentryOpt.SampleRate,
|
||||
MaxBreadcrumbs: sentryOpt.MaxBreadcrumbs,
|
||||
IgnoreErrors: nil,
|
||||
BeforeSend: nil,
|
||||
BeforeBreadcrumb: nil,
|
||||
Integrations: nil,
|
||||
Transport: nil,
|
||||
ServerName: sentryOpt.ServerName,
|
||||
Release: sentryOpt.Release,
|
||||
Dist: sentryOpt.Dist,
|
||||
Environment: sentryOpt.Environment,
|
||||
})
|
||||
}
|
||||
|
||||
func HandleError(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
|
||||
@ -3,7 +3,6 @@ package cli
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
@ -13,6 +12,7 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/pkg/api"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -220,11 +220,12 @@ func (c *Config) MakeCLI(ctx context.Context) (cmd *cobra.Command) {
|
||||
Use: c.RootCommandName,
|
||||
TraverseChildren: true,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
if err = InitSentry(c.SentryOpt); err != nil {
|
||||
if err = sentry.Init(c.SentryOpt); err != nil {
|
||||
c.Log.Error("could not initialize Sentry", zap.Error(err))
|
||||
}
|
||||
|
||||
defer sentry.Recover()
|
||||
|
||||
InitGeneralServices(c.SmtpOpt, c.JwtOpt, c.HttpClientOpt)
|
||||
|
||||
err = c.RootCommandDBSetup.Run(ctx, cmd, c)
|
||||
|
||||
46
pkg/sentry/sentry.go
Normal file
46
pkg/sentry/sentry.go
Normal file
@ -0,0 +1,46 @@
|
||||
package sentry
|
||||
|
||||
import (
|
||||
"github.com/getsentry/sentry-go"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
)
|
||||
|
||||
func Init(sentryOpt *options.SentryOpt) error {
|
||||
if sentryOpt.DSN == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return sentry.Init(sentry.ClientOptions{
|
||||
Dsn: sentryOpt.DSN,
|
||||
Debug: sentryOpt.Debug,
|
||||
AttachStacktrace: sentryOpt.AttachStacktrace,
|
||||
SampleRate: sentryOpt.SampleRate,
|
||||
MaxBreadcrumbs: sentryOpt.MaxBreadcrumbs,
|
||||
IgnoreErrors: nil,
|
||||
BeforeSend: nil,
|
||||
BeforeBreadcrumb: nil,
|
||||
Integrations: nil,
|
||||
Transport: nil,
|
||||
ServerName: sentryOpt.ServerName,
|
||||
Release: sentryOpt.Release,
|
||||
Dist: sentryOpt.Dist,
|
||||
Environment: sentryOpt.Environment,
|
||||
})
|
||||
}
|
||||
|
||||
func Recover() {
|
||||
// Check if client is configured
|
||||
if sentry.CurrentHub().Client() == nil {
|
||||
// We do not have Sentry client configured, that means we do not want to
|
||||
// recover from panic here as it will only suppress it
|
||||
logger.Default()
|
||||
return
|
||||
}
|
||||
|
||||
if err := recover(); err != nil {
|
||||
hub := sentry.CurrentHub()
|
||||
hub.Recover(err)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user