Skip to content

Commit

Permalink
fix: metrics updates
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Dec 6, 2024
1 parent 218ff33 commit 3fc3e6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
31 changes: 19 additions & 12 deletions scrapers/stale.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/flanksource/commons/duration"
v1 "github.com/flanksource/config-db/api/v1"
"github.com/flanksource/config-db/db/models"
"github.com/flanksource/duty/context"
"github.com/google/uuid"
)
Expand Down Expand Up @@ -40,22 +41,28 @@ func DeleteStaleConfigItems(ctx context.Context, staleTimeout string, scraperID
}

deleteQuery := `
UPDATE config_items
SET
deleted_at = NOW(),
delete_reason = ?
WHERE
((NOW() - last_scraped_time) > INTERVAL '1 SECOND' * ?) AND
deleted_at IS NULL AND
scraper_id = ?`

result := ctx.DB().Exec(deleteQuery, v1.DeletedReasonStale, staleDuration.Seconds(), scraperID)
UPDATE config_items
SET
deleted_at = NOW(),
delete_reason = ?
WHERE
((NOW() - last_scraped_time) > INTERVAL '1 SECOND' * ?) AND
deleted_at IS NULL AND
scraper_id = ?
RETURNING type`

var deletedConfigs []models.ConfigItem
result := ctx.DB().Raw(deleteQuery, v1.DeletedReasonStale, staleDuration.Seconds(), scraperID).Scan(&deletedConfigs)
if err := result.Error; err != nil {
return 0, fmt.Errorf("failed to delete stale config items: %w", err)
}

if result.RowsAffected > 0 {
ctx.Logger.V(3).Infof("Deleted %d stale config items", result.RowsAffected)
if len(deletedConfigs) > 0 {
ctx.Logger.V(3).Infof("Deleted %d stale config items", len(deletedConfigs))
}

for _, c := range deletedConfigs {
ctx.Counter("scraper_deleted", "scraper_id", scraperID.String(), "kind", c.Type, "reason", string(v1.DeletedReasonStale)).Add(1)
}

return result.RowsAffected, nil
Expand Down
2 changes: 1 addition & 1 deletion utils/kube/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
v1 "github.com/flanksource/config-db/api/v1"
)

var fetchDelayBuckets = []float64{500, 1_000, 3_000, 5_000, 10_000, 20_000, 30_000, 60_000}
var fetchDelayBuckets = []float64{10, 50, 100, 200, 300, 500, 1_000, 3_000, 5_000, 10_000, 20_000, 30_000, 60_000}

func FetchInvolvedObjects(ctx api.ScrapeContext, iObjs []v1.InvolvedObject) ([]*unstructured.Unstructured, error) {
clientMap := map[schema.GroupVersionKind]dynamic.NamespaceableResourceInterface{}
Expand Down

0 comments on commit 3fc3e6b

Please sign in to comment.