Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace logrus with stdlib slog #15

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Editors
/.idea/
[._]*.sw[a-p]
*~
\#*
.\#*

# Test files
*.crt
*.out
*.profile
coverage.html

/dist/
*.sql*
/ido-cleanup
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
16 changes: 0 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
84 changes: 47 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package main
import (
"database/sql"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -173,22 +181,19 @@ 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 {
continue
}

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
Expand All @@ -198,24 +203,24 @@ 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
}

// 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
}

Expand All @@ -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),
)
}
}

Expand Down