Skip to content

Commit

Permalink
Merge pull request #206 from grafana/vzhuravlev/job-multiple
Browse files Browse the repository at this point in the history
Keep searching for job/instance if multiple job/instance matchers present
  • Loading branch information
rgeyer authored Nov 6, 2024
2 parents 6ba6fe6 + 77e7142 commit 1e79993
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lint/rule_target_job_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ func testTargetRequiredMatcherRule(t *testing.T, matcher string) {
Expr: fmt.Sprintf(`sum(rate(foo{%s=~"$%s"}[5m]))`, matcher, matcher),
},
},
// Happy path (multiple matchers where at least one matches)
{
result: ResultSuccess,
target: Target{
Expr: fmt.Sprintf(`sum(rate(foo{%s="integrations/bar", %s=~"$%s"}[5m]))`, matcher, matcher, matcher),
},
},
{
result: ResultSuccess,
target: Target{
Expr: fmt.Sprintf(`sum(rate(foo{%s=~"$%s", %s="integrations/bar"}[5m]))`, matcher, matcher, matcher),
},
},
// Also happy when the promql is invalid
{
result: ResultSuccess,
Expand Down
15 changes: 10 additions & 5 deletions lint/target_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import (
)

func checkForMatcher(selector []*labels.Matcher, name string, ty labels.MatchType, value string) error {
var result error
result = fmt.Errorf("%s selector not found", name)

for _, matcher := range selector {
if matcher.Name != name {
continue
}
if matcher.Type == ty && matcher.Value == value {
result = nil
break
}

if matcher.Type != ty {
return fmt.Errorf("%s selector is %s, not %s", name, matcher.Type, ty)
result = fmt.Errorf("%s selector is %s, not %s", name, matcher.Type, ty)
}

if matcher.Value != value {
return fmt.Errorf("%s selector is %s, not %s", name, matcher.Value, value)
result = fmt.Errorf("%s selector is %s, not %s", name, matcher.Value, value)
}

return nil
}

return fmt.Errorf("%s selector not found", name)
return result
}

0 comments on commit 1e79993

Please sign in to comment.