Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

Fix/delete old snapshot (#1621)

Nityananda Gohainfe0a4ab0cb4c
diff --git a/ee/query-service/usage/manager.go b/ee/query-service/usage/manager.go
index 59f16b1a3d..067b65e0b4 100644
--- a/ee/query-service/usage/manager.go
+++ b/ee/query-service/usage/manager.go
@@ -139,6 +139,8 @@ func (lm *Manager) UsageExporter(ctx context.Context) {
lm.CollectAndStoreUsage(ctx)
case <-uploadTicker.C:
lm.UploadUsage(ctx)
+ // remove the old snapshots
+ lm.repository.DropOldSnapshots(ctx)
}
}
}
diff --git a/ee/query-service/usage/repository/repository.go b/ee/query-service/usage/repository/repository.go
index 99bd4c5796..57bf5388b6 100644
--- a/ee/query-service/usage/repository/repository.go
+++ b/ee/query-service/usage/repository/repository.go
@@ -17,6 +17,7 @@ import (
const (
MaxFailedSyncCount = 9 // a snapshot will be ignored if the max failed count is greater than or equal to 9
+ SnapShotLife = 3 * 24 * time.Hour
)
// Repository is usage Repository which stores usage snapshot in a secured DB
@@ -111,6 +112,18 @@ func (r *Repository) GetSnapshotsNotSynced(ctx context.Context) ([]*model.Usage,
return snapshots, nil
}
+func (r *Repository) DropOldSnapshots(ctx context.Context) error {
+ query := `delete from usage where created_at <= $1`
+
+ _, err := r.db.ExecContext(ctx, query, time.Now().Add(-(SnapShotLife)))
+ if err != nil {
+ zap.S().Errorf("failed to remove old snapshots from db: %v", zap.Error(err))
+ return err
+ }
+
+ return nil
+}
+
// CheckSnapshotGtCreatedAt checks if there is any snapshot greater than the provided timestamp
func (r *Repository) CheckSnapshotGtCreatedAt(ctx context.Context, ts time.Time) (bool, error) {