From 1d1f64b4bc41180842fed4597851753ba0e4a768 Mon Sep 17 00:00:00 2001 From: Chris Marchbanks Date: Tue, 5 Nov 2019 11:22:31 -0700 Subject: [PATCH] Fix Promtool showing false duplicate rule warnings (#6270) Alert rules do not use the Record field, so any alerts with the same labels and different names would be counted as being duplicates. Promtool will now consider either field when finding duplicates. Signed-off-by: Chris Marchbanks --- cmd/promtool/main.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 1a620037c..63eb445de 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -324,19 +324,19 @@ type compareRuleType struct { label map[string]string } -func checkDuplicates(r []rulefmt.RuleGroup) []compareRuleType { +func checkDuplicates(groups []rulefmt.RuleGroup) []compareRuleType { var duplicates []compareRuleType - for rindex := range r { - for index, props := range r[rindex].Rules { + for _, group := range groups { + for index, rule := range group.Rules { inst := compareRuleType{ - metric: props.Record, - label: props.Labels, + metric: ruleMetric(rule), + label: rule.Labels, } for i := 0; i < index; i++ { t := compareRuleType{ - metric: r[rindex].Rules[i].Record, - label: r[rindex].Rules[i].Labels, + metric: ruleMetric(group.Rules[i]), + label: group.Rules[i].Labels, } if reflect.DeepEqual(t, inst) { duplicates = append(duplicates, t) @@ -347,6 +347,13 @@ func checkDuplicates(r []rulefmt.RuleGroup) []compareRuleType { return duplicates } +func ruleMetric(rule rulefmt.Rule) string { + if rule.Alert != "" { + return rule.Alert + } + return rule.Record +} + var checkMetricsUsage = strings.TrimSpace(` Pass Prometheus metrics over stdin to lint them for consistency and correctness.