diff --git a/cmd/rp-indexer/main.go b/cmd/rp-indexer/main.go index 33b0589..6470eae 100644 --- a/cmd/rp-indexer/main.go +++ b/cmd/rp-indexer/main.go @@ -2,20 +2,20 @@ package main import ( "database/sql" - + "log" "log/slog" "os" "os/signal" "syscall" "time" - "github.com/evalphobia/logrus_sentry" + "github.com/getsentry/sentry-go" _ "github.com/lib/pq" "github.com/nyaruka/ezconf" indexer "github.com/nyaruka/rp-indexer/v8" "github.com/nyaruka/rp-indexer/v8/indexers" - "github.com/nyaruka/rp-indexer/v8/utils" - "github.com/sirupsen/logrus" + slogmulti "github.com/samber/slog-multi" + slogsentry "github.com/samber/slog-sentry" ) var ( @@ -29,33 +29,41 @@ func main() { loader := ezconf.NewLoader(cfg, "indexer", "Indexes RapidPro contacts to ElasticSearch", []string{"indexer.toml"}) loader.MustLoad() - level, err := logrus.ParseLevel(cfg.LogLevel) + var level slog.Level + err := level.UnmarshalText([]byte(cfg.LogLevel)) if err != nil { - logrus.Fatalf("Invalid log level '%s'", level) + log.Fatalf("invalid log level %s", level) + os.Exit(1) } - logrus.SetLevel(level) - logrus.SetOutput(os.Stdout) - logrus.SetFormatter(&logrus.TextFormatter{}) - logrus.WithField("version", version).WithField("released", date).Info("starting indexer") - - // configure golang std structured logging to route to logrus - slog.SetDefault(slog.New(utils.NewLogrusHandler(logrus.StandardLogger()))) + // configure our logger + logHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level}) + slog.SetDefault(slog.New(logHandler)) logger := slog.With("comp", "main") logger.Info("starting indexer", "version", version, "released", date) // if we have a DSN entry, try to initialize it if cfg.SentryDSN != "" { - hook, err := logrus_sentry.NewSentryHook(cfg.SentryDSN, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}) - hook.Timeout = 0 - hook.StacktraceConfiguration.Enable = true - hook.StacktraceConfiguration.Skip = 4 - hook.StacktraceConfiguration.Context = 5 + err := sentry.Init(sentry.ClientOptions{ + Dsn: cfg.SentryDSN, + EnableTracing: false, + }) if err != nil { - logger.Error("invalid sentry DSN: '%s': %s", cfg.SentryDSN, err) + log.Fatalf("error initiating sentry client, error %s, dsn %s", err, cfg.SentryDSN) + os.Exit(1) } - logrus.StandardLogger().Hooks.Add(hook) + + defer sentry.Flush(2 * time.Second) + + logger = slog.New( + slogmulti.Fanout( + logHandler, + slogsentry.Option{Level: slog.LevelError}.NewSentryHandler(), + ), + ) + logger = logger.With("release", version) + slog.SetDefault(logger) } db, err := sql.Open("postgres", cfg.DB) diff --git a/go.mod b/go.mod index 03ff3ab..ce12e9f 100644 --- a/go.mod +++ b/go.mod @@ -1,24 +1,23 @@ module github.com/nyaruka/rp-indexer/v8 -go 1.21 +go 1.22 require ( - github.com/evalphobia/logrus_sentry v0.8.2 + github.com/getsentry/sentry-go v0.22.0 github.com/lib/pq v1.10.9 github.com/nyaruka/ezconf v0.2.1 github.com/nyaruka/gocommon v1.42.7 github.com/olivere/elastic/v7 v7.0.32 github.com/pkg/errors v0.9.1 - github.com/sirupsen/logrus v1.9.3 + github.com/samber/slog-multi v1.0.2 + github.com/samber/slog-sentry v1.2.2 github.com/stretchr/testify v1.8.4 ) require ( - github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/structs v1.1.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect - github.com/getsentry/raven-go v0.2.0 // indirect github.com/go-chi/chi v4.1.2+incompatible // indirect github.com/josharian/intern v1.0.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect @@ -29,6 +28,7 @@ require ( github.com/nyaruka/null/v2 v2.0.3 // indirect github.com/nyaruka/phonenumbers v1.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/samber/lo v1.38.1 // indirect github.com/shopspring/decimal v1.3.1 // indirect golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect golang.org/x/net v0.19.0 // indirect diff --git a/go.sum b/go.sum index 96d8af4..cf0fe42 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,5 @@ -github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s= -github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ= -github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc= github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= @@ -12,12 +7,14 @@ github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8 github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= -github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs= -github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= +github.com/getsentry/sentry-go v0.22.0 h1:XNX9zKbv7baSEI65l+H1GEJgSeIC1c7EN5kluWaP6dM= +github.com/getsentry/sentry-go v0.22.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +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/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= @@ -44,23 +41,26 @@ github.com/nyaruka/phonenumbers v1.3.0 h1:IFyyJfF2Elg8xGKFghWrRXzb6qAHk+Q3uPqmIg github.com/nyaruka/phonenumbers v1.3.0/go.mod h1:4jyKp/BFUokLbCHyoZag+T3S1KezFVoEKtgnbpzItC4= github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E= github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k= +github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= +github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= +github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/samber/slog-multi v1.0.2 h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ= +github.com/samber/slog-multi v1.0.2/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo= +github.com/samber/slog-sentry v1.2.2 h1:S0glIVITlGCCfSvIOte2Sh63HMHJpYN3hDr+97hILIk= +github.com/samber/slog-sentry v1.2.2/go.mod h1:bHm8jm1dks0p+xc/lH2i4TIFwnPcMTvZeHgCBj5+uhA= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM= golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -69,6 +69,5 @@ google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7 google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/utils/logrus.go b/utils/logrus.go deleted file mode 100644 index 2650bf9..0000000 --- a/utils/logrus.go +++ /dev/null @@ -1,92 +0,0 @@ -// Structured logging handler for logrus so we can rewrite code to use slog package incrementally. Once all logging is -// happening via slog, we just need to hook up Sentry directly to that, and then we can get rid of this file. -package utils - -import ( - "context" - "log/slog" - "slices" - "strings" - - "github.com/sirupsen/logrus" -) - -var levels = map[slog.Level]logrus.Level{ - slog.LevelError: logrus.ErrorLevel, - slog.LevelWarn: logrus.WarnLevel, - slog.LevelInfo: logrus.InfoLevel, - slog.LevelDebug: logrus.DebugLevel, -} - -type LogrusHandler struct { - logger *logrus.Logger - groups []string - attrs []slog.Attr -} - -func NewLogrusHandler(logger *logrus.Logger) *LogrusHandler { - return &LogrusHandler{logger: logger} -} - -func (l *LogrusHandler) clone() *LogrusHandler { - return &LogrusHandler{ - logger: l.logger, - groups: slices.Clip(l.groups), - attrs: slices.Clip(l.attrs), - } -} - -func (l *LogrusHandler) Enabled(ctx context.Context, level slog.Level) bool { - return levels[level] <= l.logger.GetLevel() -} - -func (l *LogrusHandler) Handle(ctx context.Context, r slog.Record) error { - log := logrus.NewEntry(l.logger) - if r.Time.IsZero() { - log = log.WithTime(r.Time) - } - - f := logrus.Fields{} - for _, a := range l.attrs { - if a.Key != "" { - f[a.Key] = a.Value - } - } - log = log.WithFields(f) - - r.Attrs(func(attr slog.Attr) bool { - if attr.Key == "" { - return true - } - log = log.WithField(attr.Key, attr.Value) - return true - }) - log.Logf(levels[r.Level], r.Message) - return nil -} - -func (l *LogrusHandler) groupPrefix() string { - if len(l.groups) > 0 { - return strings.Join(l.groups, ":") + ":" - } - return "" -} - -func (l *LogrusHandler) WithAttrs(attrs []slog.Attr) slog.Handler { - newHandler := l.clone() - for _, a := range attrs { - newHandler.attrs = append(newHandler.attrs, slog.Attr{ - Key: l.groupPrefix() + a.Key, - Value: a.Value, - }) - } - return newHandler -} - -func (l *LogrusHandler) WithGroup(name string) slog.Handler { - newHandler := l.clone() - newHandler.groups = append(newHandler.groups, name) - return newHandler -} - -var _ slog.Handler = &LogrusHandler{}