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 <csmarchbanks@gmail.com>
pull/6277/head
Chris Marchbanks 5 years ago committed by GitHub
parent 3836cc1997
commit 1d1f64b4bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -324,19 +324,19 @@ type compareRuleType struct {
label map[string]string label map[string]string
} }
func checkDuplicates(r []rulefmt.RuleGroup) []compareRuleType { func checkDuplicates(groups []rulefmt.RuleGroup) []compareRuleType {
var duplicates []compareRuleType var duplicates []compareRuleType
for rindex := range r { for _, group := range groups {
for index, props := range r[rindex].Rules { for index, rule := range group.Rules {
inst := compareRuleType{ inst := compareRuleType{
metric: props.Record, metric: ruleMetric(rule),
label: props.Labels, label: rule.Labels,
} }
for i := 0; i < index; i++ { for i := 0; i < index; i++ {
t := compareRuleType{ t := compareRuleType{
metric: r[rindex].Rules[i].Record, metric: ruleMetric(group.Rules[i]),
label: r[rindex].Rules[i].Labels, label: group.Rules[i].Labels,
} }
if reflect.DeepEqual(t, inst) { if reflect.DeepEqual(t, inst) {
duplicates = append(duplicates, t) duplicates = append(duplicates, t)
@ -347,6 +347,13 @@ func checkDuplicates(r []rulefmt.RuleGroup) []compareRuleType {
return duplicates return duplicates
} }
func ruleMetric(rule rulefmt.Rule) string {
if rule.Alert != "" {
return rule.Alert
}
return rule.Record
}
var checkMetricsUsage = strings.TrimSpace(` var checkMetricsUsage = strings.TrimSpace(`
Pass Prometheus metrics over stdin to lint them for consistency and correctness. Pass Prometheus metrics over stdin to lint them for consistency and correctness.

Loading…
Cancel
Save