Skip to content

Commit

Permalink
fix: better error logging when saving scrape results
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed May 10, 2024
1 parent 38a8ec3 commit cbe459b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult) (*models.ConfigItem
if len(ci.ExternalID) == 0 {
return nil, false, fmt.Errorf("config item %s has no external id", ci)
}

if ci.ID != "" {
if err := uuid.Validate(ci.ID); err == nil {
if existing, err = ctx.TempCache().Get(ci.ID); err != nil {
Expand All @@ -123,7 +124,7 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult) (*models.ConfigItem

if existing == nil || existing.ID == "" {
if err := ctx.DB().Clauses(clause.OnConflict{UpdateAll: true}).Create(ci).Error; err != nil {
return nil, false, err
return nil, false, fmt.Errorf("failed to create config: %w", err)
}

return ci, false, nil
Expand Down Expand Up @@ -272,7 +273,7 @@ func saveChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.Conf

if changeResult.Action == v1.Delete {
if err := deleteChangeHandler(ctx, changeResult); err != nil {
return err
return fmt.Errorf("failed to delete config from change: %w", err)
}
}

Expand All @@ -294,7 +295,7 @@ func saveChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.Conf
change.ConfigID = ci.ID
} else if !change.GetExternalID().IsEmpty() {
if ci, err := ctx.TempCache().FindExternalID(change.GetExternalID()); err != nil {
return err
return fmt.Errorf("failed to get config from change (externalID=%s): %w", change.GetExternalID(), err)
} else if ci != "" {
change.ConfigID = ci
} else if ci == "" {
Expand All @@ -305,11 +306,11 @@ func saveChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.Conf

if changeResult.UpdateExisting {
if err := db.Save(change).Error; err != nil {
return err
return fmt.Errorf("failed to update config change: %w", err)
}
} else {
if err := db.Create(change).Error; err != nil {
return err
return fmt.Errorf("failed to create config change: %w", err)
}
}
}
Expand Down Expand Up @@ -383,20 +384,20 @@ func SaveResults(ctx api.ScrapeContext, results []v1.ScrapeResult) error {
if result.Config != nil {
var updated bool
if ci, updated, err = updateCI(ctx, result); err != nil {
return err
return fmt.Errorf("failed to save config item (%s): %w", result, err)
} else if !updated {
toTouch = append(toTouch, ci.ID)
}
}

if result.AnalysisResult != nil {
if err := upsertAnalysis(ctx, &result); err != nil {
return err
return fmt.Errorf("failed to analysis (%s): %w", result, err)
}
}

if err := saveChanges(ctx, &result, ci); err != nil {
return err
return fmt.Errorf("failed to changes (%s): %w", result, err)
}

relationshipToForm = append(relationshipToForm, result.RelationshipResults...)
Expand Down Expand Up @@ -433,7 +434,7 @@ func SaveResults(ctx api.ScrapeContext, results []v1.ScrapeResult) error {
if err := ctx.DB().Table("config_items").
Where("id in (?)", toTouch[i:end]).
Update("last_scraped_time", gorm.Expr("NOW()")).Error; err != nil {
return err
return fmt.Errorf("error updating last scraped time: %w", err)
}
}
}
Expand Down

0 comments on commit cbe459b

Please sign in to comment.