Skip to content

Commit

Permalink
Merge pull request #100 from Icinga/cleanup-metrics
Browse files Browse the repository at this point in the history
Cleanup metrics after one month
  • Loading branch information
lippserd authored Sep 13, 2024
2 parents 101a01e + c407220 commit 4e1841a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
61 changes: 39 additions & 22 deletions cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/icinga/icinga-go-library/config"
igldatabase "github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/logging"
"github.com/icinga/icinga-go-library/periodic"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/internal"
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/metrics"
"github.com/icinga/icinga-kubernetes/pkg/periodic"
schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1"
"github.com/icinga/icinga-kubernetes/pkg/sync"
syncv1 "github.com/icinga/icinga-kubernetes/pkg/sync/v1"
Expand Down Expand Up @@ -269,28 +269,45 @@ func main() {
return s.Run(ctx)
})

errs := make(chan error, 1)
defer close(errs)
defer periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, 0, -1)

_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "event",
PK: "uuid",
Column: "created",
}, 5000, olderThan,
)
if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "event",
PK: "uuid",
Column: "created",
})
})

return
}
}, periodic.Immediate()).Stop()
com.ErrgroupReceive(ctx, g, errs)
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_cluster_metric",
PK: "(cluster_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_node_metric",
PK: "(node_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_pod_metric",
PK: "(pod_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_container_metric",
PK: "(container_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

if err := g.Wait(); err != nil {
klog.Fatal(err)
Expand Down
30 changes: 30 additions & 0 deletions pkg/database/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/icinga/icinga-go-library/retry"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/periodic"
"golang.org/x/sync/errgroup"
"time"
)

Expand Down Expand Up @@ -103,3 +105,31 @@ func (db *Database) CleanupOlderThan(
type cleanupWhere struct {
Time types.UnixMilli
}

func (db *Database) PeriodicCleanup(ctx context.Context, stmt CleanupStmt) error {
g, ctxCleanup := errgroup.WithContext(ctx)

errs := make(chan error, 1)
defer close(errs)

periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, 0, -1)

_, err := db.CleanupOlderThan(
ctx, stmt, 5000, olderThan,
)

if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}

return
}
}, periodic.Immediate()).Stop()

com.ErrgroupReceive(ctxCleanup, g, errs)

return g.Wait()
}

0 comments on commit 4e1841a

Please sign in to comment.