Skip to content

Commit

Permalink
chore: add lag in summary
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Nov 27, 2024
1 parent 8cd4d30 commit 2c700aa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ func (summary ScrapeSummary) HasUpdates() bool {

}

func (summary *ScrapeSummary) UpdateLag(m map[string]float64) {
for k, v := range m {
summary.AddLag(k, v)
}
}

func (summary ConfigTypeScrapeSummary) String() string {
s := []string{}

Expand Down Expand Up @@ -249,6 +255,12 @@ func (t *ScrapeSummary) AddWarning(configType, warning string) {
(*t)[configType] = v
}

func (t *ScrapeSummary) AddLag(key string, lag float64) {
v := (*t)[key]
v.Lag = lag
(*t)[key] = v
}

type ChangeSummary struct {
Orphaned map[string]int `json:"orphaned,omitempty"`
Ignored map[string]int `json:"ignored,omitempty"`
Expand Down Expand Up @@ -307,6 +319,7 @@ type ConfigTypeScrapeSummary struct {
Unchanged int `json:"unchanged,omitempty"`
Change *ChangeSummary `json:"change,omitempty"`
Warnings []string `json:"warnings,omitempty"`
Lag float64 `json:"lag,omitempty"`
}

// +kubebuilder:object:generate=false
Expand Down
43 changes: 43 additions & 0 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,52 @@ func saveResults(ctx api.ScrapeContext, results []v1.ScrapeResult) (v1.ScrapeSum
} else {
ctx.Logger.V(4).Infof("No Update: %s", summary)
}

// Add lag time in summary
if lagMap, err := calculateScrapeLag(ctx); err != nil {
ctx.Errorf("error calculating lag: %v", err)
} else {
summary.UpdateLag(lagMap)
}
return summary, nil
}

func calculateScrapeLag(ctx api.ScrapeContext) (map[string]float64, error) {
if ctx.ScraperID() == "" {
return nil, nil
}

q := `
SELECT COALESCE(ROUND(EXTRACT(EPOCH FROM (MAX(inserted_at - created_at))), 2), 0) AS lag
FROM config_items
WHERE
scraper_id = ? AND
inserted_at >= last_scraped_time
`
var configItemLag float64
if err := ctx.DB().Raw(q, ctx.ScraperID()).Scan(&configItemLag).Error; err != nil {
return nil, fmt.Errorf("error querying config_item scrape lag: %w", dutydb.ErrorDetails(err))
}

q = `
SELECT COALESCE(ROUND(EXTRACT(EPOCH FROM (MAX(cc.inserted_at - cc.created_at))), 2), 0) AS lag
FROM config_changes cc
INNER JOIN config_items ci ON cc.config_id = ci.id
WHERE
ci.scraper_id = ? AND
cc.inserted_at >= ci.last_scraped_time
`
var configChangeLag float64
if err := ctx.DB().Raw(q, ctx.ScraperID()).Scan(&configChangeLag).Error; err != nil {
return nil, fmt.Errorf("error querying config_change scrape lag: %w", dutydb.ErrorDetails(err))
}

return map[string]float64{
"config_item_lag_seconds": configItemLag,
"config_change_lag_seconds": configChangeLag,
}, nil
}

var lastScrapedTimeMutex sync.Map

func updateLastScrapedTime(ctx api.ScrapeContext, ids []string) error {
Expand Down

0 comments on commit 2c700aa

Please sign in to comment.