Skip to content
Snippets Groups Projects
Commit 46674693735b authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

Add sentry support

parent 847537d7ade0
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,6 @@
import (
"bytes"
"io"
"time"
"github.com/getsentry/sentry-go"
jsoniter "github.com/json-iterator/go"
......@@ -7,9 +6,8 @@
"github.com/getsentry/sentry-go"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
)
// SentryOptions ...
type SentryOptions struct {
SentryDSN func(string) `long:"dsn" env:"DSN" ini-name:"dsn" description:"Sentry DSN"`
......@@ -11,10 +9,7 @@
)
// SentryOptions ...
type SentryOptions struct {
SentryDSN func(string) `long:"dsn" env:"DSN" ini-name:"dsn" description:"Sentry DSN"`
client *sentry.Client
hub *sentry.Hub
}
......@@ -19,7 +14,16 @@
}
// GetClient returns the sentry client
func (o SentryOptions) GetClient() *sentry.Client {
return o.client
}
// NewSentryOptions ...
func NewSentryOptions(loggingOptions *LoggingOptions) *SentryOptions {
sentryOptions := SentryOptions{}
sentryOptions.SentryDSN = func(dsn string) {
log := loggingOptions.Logger()
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: dsn,
})
if err != nil {
log.Err(err).Msg("Could not initialize sentry")
return
}
hub := sentry.NewHub(client, sentry.NewScope())
......@@ -25,7 +29,13 @@
// GetHub returns the sentry hub
func (o SentryOptions) GetHub() *sentry.Hub {
return o.hub
loggingOptions.AddLogWrapper(
func(next io.Writer) io.Writer {
return SentryLogger{hub, next}
})
//OnShutdown(func() {
//hub.Flush(time.Second)
//})
}
return &sentryOptions
}
// SentryLogger ...
......@@ -117,31 +127,3 @@
}
return l.next.Write(p)
}
// Setup ...
func (o *SentryOptions) Setup(loggingOptions *LoggingOptions, environment *string) {
o.SentryDSN = func(dsn string) {
client, err := sentry.NewClient(sentry.ClientOptions{
Environment: *environment,
Dsn: dsn,
})
if err != nil {
log.Err(err).Msg("Could not initialize sentry")
return
}
o.client = client
o.hub = sentry.NewHub(client, sentry.NewScope())
loggingOptions.AddLogWrapper(func(next io.Writer) io.Writer {
return SentryLogger{o.hub, next}
})
}
}
// Shutdown ...
func (o SentryOptions) Shutdown() {
if o.hub != nil {
o.hub.Flush(time.Second)
o.hub = nil
o.client = nil
}
}
......@@ -17,6 +17,7 @@
LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout))
DatabaseOptions = &database.Options{}
ConfigFileOption = &ConfigFile{}
SentryOptions = orusapi.NewSentryOptions(LoggingOptions)
parser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.HelpFlag|flags.PassDoubleDash)
bootstrapParser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.IgnoreUnknown)
......@@ -67,6 +68,7 @@
}
g.Namespace="log"
g.EnvNamespace="LOG"
g, err = parser.AddGroup("Database", "Database options", DatabaseOptions)
if err != nil {
panic(err)
......@@ -74,6 +76,13 @@
g.Namespace="db"
g.EnvNamespace="DB"
g, err = parser.AddGroup("Sentry", "Sentry options", SentryOptions)
if err != nil {
panic(err)
}
g.Namespace="sentry"
g.EnvNamespace="SENTRY"
parser.AddCommand("generate-config", "Generate a configuration file", "", orusapi.NewGenerateConfigCmd(parser))
bootstrapParser.NamespaceDelimiter = "-"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment