Browse Source

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
commit
1d1f64b4bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      cmd/promtool/main.go

21
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.

Loading…
Cancel
Save