From 55982dc6522f94736f78158e44037491e0f5afbe Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Fri, 23 Aug 2024 12:52:01 +0200 Subject: [PATCH 1/2] Replace logrus with stdlib slog --- .gitignore | 13 +++++++++ go.mod | 8 ++---- go.sum | 16 ----------- main.go | 84 ++++++++++++++++++++++++++++++------------------------ 4 files changed, 62 insertions(+), 59 deletions(-) diff --git a/.gitignore b/.gitignore index 7136a0e..0385347 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,16 @@ +# Editors +/.idea/ +[._]*.sw[a-p] +*~ +\#* +.\#* + +# Test files +*.crt +*.out +*.profile +coverage.html + /dist/ *.sql* /ido-cleanup diff --git a/go.mod b/go.mod index fba1471..87f81af 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,10 @@ module github.com/NETWAYS/ido-cleanup -go 1.19 +go 1.21 require ( github.com/go-sql-driver/mysql v1.8.1 - github.com/sirupsen/logrus v1.9.3 github.com/spf13/pflag v1.0.5 ) -require ( - filippo.io/edwards25519 v1.1.0 // indirect - golang.org/x/sys v0.22.0 // indirect -) +require filippo.io/edwards25519 v1.1.0 // indirect diff --git a/go.sum b/go.sum index 178c21a..14ab1d3 100644 --- a/go.sum +++ b/go.sum @@ -1,22 +1,6 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -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/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= -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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index ceafdd4..80a0e11 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "fmt" + "log/slog" "os" "os/signal" "strings" @@ -10,14 +11,11 @@ import ( "time" _ "github.com/go-sql-driver/mysql" // do not remove this although it looks weird. It adds the mysql db driver - "github.com/sirupsen/logrus" "github.com/spf13/pflag" ) const readme = ` Icinga IDO Cleanup - -For more details see: https://github.com/NETWAYS/ido-cleanup ` var ( @@ -46,21 +44,32 @@ var defaultAges = map[string]uint{ func main() { handleArguments() + // Default log options + opts := &slog.HandlerOptions{ + Level: slog.LevelInfo, + } + if debug { - logrus.SetLevel(logrus.DebugLevel) + opts.Level = slog.LevelDebug } - logrus.Info("starting ido-cleanup") + handler := slog.NewTextHandler(os.Stdout, opts) + logger := slog.New(handler) + slog.SetDefault(logger) + + logger.Info("starting ido-cleanup") // Setup database connection db, err := sql.Open("mysql", dbDsn) if err != nil { - logrus.Fatal(err) + logger.Error("could not connect to database", "error", err) + os.Exit(1) } err = db.Ping() if err != nil { - logrus.Fatal("could not connect to database: ", err) + logger.Error("could not connect to database", "error", err) + os.Exit(1) } db.SetConnMaxLifetime(time.Minute * 15) @@ -70,7 +79,8 @@ func main() { if err != nil { _ = db.Close() - logrus.Fatal(err) + logger.Error("could not get instance ID", "error", err) + os.Exit(1) } defer db.Close() @@ -85,15 +95,14 @@ func main() { // Start initial cleanup and prepare timer currentInterval := interval - if runCleanup(db, instanceID) { - logrus.WithField("interval", fastInterval).Debug("updating interval") - + if runCleanup(db, instanceID, logger) { + logger.Debug("updating interval", "interval", fastInterval) currentInterval = fastInterval } // Stop here when only once is requested if once { - logrus.Info("stopping after one cleanup") + logger.Info("stopping after one cleanup") return } @@ -102,7 +111,7 @@ func main() { go func() { sig := <-interrupt - logrus.Info("received signal ", sig) + logger.Info("received signal", "signal", sig) timer.Stop() done <- true @@ -115,19 +124,18 @@ func main() { case <-timer.C: nextInterval := interval - if runCleanup(db, instanceID) { + if runCleanup(db, instanceID, logger) { nextInterval = fastInterval } if currentInterval != nextInterval { - logrus.WithField("interval", nextInterval).Debug("updating interval") - + logger.Debug("updating interval", "interval", nextInterval) timer.Reset(nextInterval) } } } - logrus.Info("stopping ido-cleanup") + logger.Info("stopping ido-cleanup") } func handleArguments() { @@ -173,7 +181,7 @@ func handleArguments() { } } -func runCleanup(db *sql.DB, instanceID int) (busy bool) { +func runCleanup(db *sql.DB, instanceID int, logger *slog.Logger) (busy bool) { for _, table := range knownTables { age, set := ages[table.Name] if !set || *age == 0 { @@ -181,14 +189,11 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) { } start := time.Now() - entry := logrus.WithField("table", table.Name) // Look for the time stamp of the oldest entry and log it oldest, err := table.OldestTime(db, instanceID) if err != nil { - entry.Error(err) - } else if !oldest.IsZero() { - entry = entry.WithField("oldest", oldest) + logger.Error("could not get entry", "error", err, "table", table.Name) } // Until when we want to delete @@ -198,15 +203,16 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) { if noop { rows, err := table.Count(db, instanceID, deleteSince) if err != nil { - entry.Error(err) - + logger.Error("could not enumerate rows", "error", err, "table", table.Name, "oldest", oldest) continue } - entry.WithFields(logrus.Fields{ - "rows": rows, - "took": time.Since(start), - }).Info("would delete rows") + logger.Info("would delete rows", + "table", table.Name, + "oldest", oldest, + "rows", rows, + "took", time.Since(start), + ) continue } @@ -214,8 +220,7 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) { // Run the cleanup rows, err := table.Cleanup(db, instanceID, deleteSince, limit) if err != nil { - entry.Error(err) - + logger.Error("could run cleanup", "error", err, "table", table.Name) continue } @@ -224,15 +229,20 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) { busy = true } - entry = entry.WithFields(logrus.Fields{ - "rows": rows, - "took": time.Since(start), - }) - if rows > 0 { - entry.Info("deleted rows") + logger.Info("deleted rows", + "table", table.Name, + "oldest", oldest, + "rows", rows, + "took", time.Since(start), + ) } else { - entry.Debug("deleted rows") + logger.Debug("deleted rows", + "table", table.Name, + "oldest", oldest, + "rows", rows, + "took", time.Since(start), + ) } } From 927252a0f5743f4632ef0956644680dd934bccd7 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Fri, 23 Aug 2024 15:03:46 +0200 Subject: [PATCH 2/2] Bump Go version in CI --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 058dcf2..4b23faa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: 1.19 + go-version: 1.21 - name: Vet run: go vet ./...