Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rule/duplicate #1094

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.66.0

### Fixed

- Improved accuracy of the [rule/duplicate](checks/rule/duplicate.md) check.

## v0.65.1

### Fixed
Expand Down
31 changes: 18 additions & 13 deletions internal/checks/rule_duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func (c RuleDuplicateCheck) Reporter() string {
}

func (c RuleDuplicateCheck) Check(ctx context.Context, path discovery.Path, rule parser.Rule, entries []discovery.Entry) (problems []Problem) {
if rule.RecordingRule == nil || rule.RecordingRule.Expr.SyntaxError != nil {
return nil
expr := rule.Expr()
if expr.SyntaxError != nil {
return problems
}

for _, entry := range entries {
Expand All @@ -55,6 +56,9 @@ func (c RuleDuplicateCheck) Check(ctx context.Context, path discovery.Path, rule
if entry.Rule.Error.Err != nil {
continue
}
if entry.Rule.Expr().SyntaxError != nil {
continue
}
if entry.Rule.RecordingRule == nil {
continue
}
Expand All @@ -67,23 +71,24 @@ func (c RuleDuplicateCheck) Check(ctx context.Context, path discovery.Path, rule
if !c.prom.IsEnabledForPath(entry.Path.Name) {
continue
}
if entry.Rule.RecordingRule.Record.Value != rule.RecordingRule.Record.Value {
continue

// Look for identical recording rules.
if entry.Rule.RecordingRule != nil && rule.RecordingRule != nil && entry.Rule.RecordingRule.Record.Value == rule.RecordingRule.Record.Value {
problems = append(problems, c.compareRules(ctx, rule.RecordingRule, entry, rule.Lines)...)
}
problems = append(problems, c.compareRules(ctx, rule.RecordingRule, entry, rule.Lines)...)
}
return problems
}

func (c RuleDuplicateCheck) compareRules(_ context.Context, rule *parser.RecordingRule, entry discovery.Entry, lines parser.LineRange) (problems []Problem) {
ruleALabels := buildRuleLabels(rule)
ruleBLabels := buildRuleLabels(entry.Rule.RecordingRule)
ruleALabels := buildRuleLabels(rule.Labels)
ruleBLabels := buildRuleLabels(entry.Rule.RecordingRule.Labels)

if ruleALabels.Hash() != ruleBLabels.Hash() {
return nil
}

if rule.Expr.Value.Value == entry.Rule.RecordingRule.Expr.Value.Value {
if rule.Expr.Query.Expr.String() == entry.Rule.RecordingRule.Expr.Query.Expr.String() {
problems = append(problems, Problem{
Lines: lines,
Reporter: c.Reporter(),
Expand All @@ -95,14 +100,14 @@ func (c RuleDuplicateCheck) compareRules(_ context.Context, rule *parser.Recordi
return problems
}

func buildRuleLabels(rule *parser.RecordingRule) labels.Labels {
if rule.Labels == nil || len(rule.Labels.Items) == 0 {
func buildRuleLabels(l *parser.YamlMap) labels.Labels {
if l == nil || len(l.Items) == 0 {
return labels.EmptyLabels()
}

pairs := make([]string, 0, len(rule.Labels.Items))
for _, l := range rule.Labels.Items {
pairs = append(pairs, l.Key.Value, l.Value.Value)
pairs := make([]string, 0, len(l.Items))
for _, label := range l.Items {
pairs = append(pairs, label.Key.Value, label.Value.Value)
}
return labels.FromStrings(pairs...)
}
29 changes: 29 additions & 0 deletions internal/checks/rule_duplicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ func TestRuleDuplicateCheck(t *testing.T) {
problems: noProblems,
entries: xxxEntries,
},
{
description: "same expr but formatted differently",
content: "- record: job:up:sum\n expr: sum(up) by(job)\n",
checker: func(prom *promapi.FailoverGroup) checks.RuleChecker {
return checks.NewRuleDuplicateCheck(prom)
},
prometheus: newSimpleProm,
problems: func(_ string) []checks.Problem {
return []checks.Problem{
{
Lines: parser.LineRange{
First: 1,
Last: 2,
},
Reporter: checks.RuleDuplicateCheckName,
Text: textDuplicateRule("fake.yml", 6),
Severity: checks.Bug,
},
}
},
entries: mustParseContent(`
- record: job:up:sum
expr: sum by(job) (up)
labels:
cluster: prod
- record: job:up:sum
expr: sum by(job) (up)
`),
},
}

runTests(t, testCases)
Expand Down
Loading