Skip to content

Commit

Permalink
chore: handle extract attrs error
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Nov 4, 2024
1 parent 5ecc958 commit c9569bc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/v1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ var (
Delete ChangeAction = "delete"
Ignore ChangeAction = "ignore"
)

const ChangeTypeDiff = "diff"
6 changes: 3 additions & 3 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func extractChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.C
}
}

if change.Severity == "" && change.ChangeType != "diff" {
if change.Severity == "" && change.ChangeType != v1.ChangeTypeDiff {
change.Severity = events.GetSeverity(change.ChangeType)
}

Expand Down Expand Up @@ -739,7 +739,7 @@ func updateLastScrapedTime(ctx api.ScrapeContext, ids []string) error {
// given 2 config items and returns a ConfigChange object if there are any changes.
func generateConfigChange(ctx api.ScrapeContext, newConf, prev models.ConfigItem) (*v1.ChangeResult, error) {
if changeTypExclusion, ok := lo.FromPtr(newConf.Labels)[v1.AnnotationIgnoreChangeByType]; ok {
if collections.MatchItems("diff", strings.Split(changeTypExclusion, ",")...) {
if collections.MatchItems(v1.ChangeTypeDiff, strings.Split(changeTypExclusion, ",")...) {
if ctx.PropertyOn(false, "log.exclusions") {
ctx.Logger.V(4).Infof("excluding diff change for config(%s) with annotation (%s)", newConf, changeTypExclusion)
}
Expand Down Expand Up @@ -801,7 +801,7 @@ func generateConfigChange(ctx api.ScrapeContext, newConf, prev models.ConfigItem

return &v1.ChangeResult{
ConfigType: newConf.Type,
ChangeType: "diff",
ChangeType: v1.ChangeTypeDiff,
ExternalID: newConf.ExternalID[0],
Diff: &diff,
Patches: string(patch),
Expand Down
7 changes: 7 additions & 0 deletions scrapers/processors/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ func (e Extract) Extract(ctx api.ScrapeContext, inputs ...v1.ScrapeResult) ([]v1
}

extracted, err := e.extractAttributes(result)
if extracted.ID == "" {
continue
}
if err != nil {
return results, fmt.Errorf("failed to extract attributes: %v", err)
}
Expand Down Expand Up @@ -517,6 +520,10 @@ func (e Extract) extractAttributes(input v1.ScrapeResult) (v1.ScrapeResult, erro
}

if input.ID == "" {
// Ignore kubernetes diffs
if len(input.Changes) == 1 && (input.Changes[0].ChangeType == v1.ChangeTypeDiff) {
return input, nil
}
return input, fmt.Errorf("no id defined for: %s: %v", input, e.Config)
}

Expand Down

0 comments on commit c9569bc

Please sign in to comment.