Skip to content

Commit

Permalink
Fix absent() checks
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jan 5, 2022
1 parent e156686 commit bedea1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.6.1

### Fixed

- Labels returned by `absent()` are only from equal match types (`absent(foo="bar")`,
not `absent(foo=~"bar.+")` but `alerts/template` didn't test for match type when
checking for labels sourced from `absent()` queries.

## v0.6.0

### Changed
Expand Down
5 changes: 4 additions & 1 deletion internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cloudflare/pint/internal/parser"
"github.com/cloudflare/pint/internal/parser/utils"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
promTemplate "github.com/prometheus/prometheus/template"
)
Expand Down Expand Up @@ -345,7 +346,9 @@ func absentLabels(node *parser.PromQLNode) []string {
for _, child := range node.Children {
for _, v := range utils.HasVectorSelector(child) {
for _, lm := range v.LabelMatchers {
labelMap[lm.Name] = struct{}{}
if lm.Type == labels.MatchEqual {
labelMap[lm.Name] = struct{}{}
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions internal/checks/alerts_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,25 @@ func TestTemplateCheck(t *testing.T) {
},
},
},
{
description: "annotation label missing from metrics (absent({job=~}))",
content: `
- alert: Foo Is Missing
expr: absent({job=~".+"})
annotations:
summary: '{{ .Labels.job }} is missing'
`,
checker: checks.NewTemplateCheck(),
problems: []checks.Problem{
{
Fragment: `summary: {{ .Labels.job }} is missing`,
Lines: []int{5},
Reporter: "alerts/template",
Text: `template is using "job" label but absent() is not passing it`,
Severity: checks.Bug,
},
},
},
}
runTests(t, testCases)
}

0 comments on commit bedea1e

Please sign in to comment.