Skip to content

Commit

Permalink
Correctly handle regex names in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jun 16, 2022
1 parent 2b55b5f commit 21b4bd0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

- Strict parsing mode didn't fully validate rule group files, this is now fixed
and pint runs the same set of checks as Prometheus.
- Fixed `promql/series` handling of rules with `{__name__=~"foo|bar"}` queries.

## v0.21.1

Expand Down
2 changes: 1 addition & 1 deletion internal/checks/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func runTests(t *testing.T, testCases []checkTest) {
return
}
}
t.Errorf("no matching response for %s request", r.URL.Path)
t.Errorf("no matching response for %s request with payload: %s", r.URL.Path, r.Form.Encode())
t.FailNow()
}))
defer srv.Close()
Expand Down
4 changes: 3 additions & 1 deletion internal/checks/promql_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,9 @@ func stripLabels(selector promParser.VectorSelector) promParser.VectorSelector {
for _, lm := range selector.LabelMatchers {
if lm.Name == labels.MetricName {
s.LabelMatchers = append(s.LabelMatchers, lm)
s.Name = lm.Value
if lm.Type == labels.MatchEqual {
s.Name = lm.Value
}
}
}
return s
Expand Down
33 changes: 33 additions & 0 deletions internal/checks/promql_series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,39 @@ func TestSeriesCheck(t *testing.T) {
},
},
},
{
description: "__name__=~foo|bar",
content: "- alert: NameRegex\n expr: rate({__name__=~\"(foo|bar)_panics_total\", job=\"myjob\"}[2m]) > 0",
checker: newSeriesCheck,
prometheus: newSimpleProm,
problems: func(uri string) []checks.Problem {
return []checks.Problem{
{
Fragment: `{__name__=~"(foo|bar)_panics_total"}`,
Lines: []int{2},
Reporter: checks.SeriesCheckName,
Text: noSeriesText("prom", uri, `{__name__=~"(foo|bar)_panics_total"}`, "1w"),
Severity: checks.Bug,
},
}
},
mocks: []*prometheusMock{
{
conds: []requestCondition{
requireQueryPath,
formCond{key: "query", value: `count({__name__=~"(foo|bar)_panics_total",job="myjob"})`},
},
resp: respondWithEmptyVector(),
},
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count({__name__=~"(foo|bar)_panics_total"})`},
},
resp: respondWithEmptyMatrix(),
},
},
},
}
runTests(t, testCases)
}

0 comments on commit 21b4bd0

Please sign in to comment.