Skip to content

Commit

Permalink
Allow disabling individual query/series checks
Browse files Browse the repository at this point in the history
Fixes #112
  • Loading branch information
prymitive committed Jan 12, 2022
1 parent aa5076d commit 080b29f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog

## v0.6.5

### Added

- Allow disabling `query/series` check for individual series using
`# pint disable query/series(my_metric_name)` comments.

## v0.6.4

### Fixed
Expand Down
21 changes: 21 additions & 0 deletions docs/CONFIGURATION.md
Expand Up @@ -725,3 +725,24 @@ groups:
- record: instance:http_requests_total:avg_over_time:1w
expr: avg_over_time(http_requests_total[1w]) by (instance) # pint disable query/cost
```

Some checks allow to specify extra parameters.

### query/series

You can disable `query/series` for specific metric using `# pint disable query/series(selector)`
comment.
Just like with PromQL if a selector doesn't have any labels then it will match all instances,
if you pass any labels it will only pass time series with those labels.

Disable warnings about missing `my_metric_name`:

```YAML
# pint disable query/series(my_metric_name)
```

Disable it only for `my_metric_name{cluster="dev"}` but still warn about `my_metric_name{cluster="prod"}`:

```YAML
# pint disable query/series(my_metric_name{cluster="dev"})
```
30 changes: 21 additions & 9 deletions internal/checks/query_series.go
Expand Up @@ -39,6 +39,13 @@ func (c SeriesCheck) Check(rule parser.Rule) (problems []Problem) {
done := map[string]bool{}

for _, selector := range getSelectors(expr.Query) {
bareSelector := stripLabels(selector)
c1 := fmt.Sprintf("disable %s(%s)", SeriesCheckName, selector.String())
c2 := fmt.Sprintf("disable %s(%s)", SeriesCheckName, bareSelector.String())
if rule.HasComment(c1) || rule.HasComment(c2) {
done[selector.String()] = true
continue
}
if _, ok := done[selector.String()]; ok {
continue
}
Expand Down Expand Up @@ -71,15 +78,7 @@ func (c SeriesCheck) countSeries(expr parser.PromQLExpr, selector promParser.Vec
if series == 0 {
if len(selector.LabelMatchers) > 1 {
// retry selector with only __name__ label
s := promParser.VectorSelector{
Name: selector.Name,
LabelMatchers: []*labels.Matcher{},
}
for _, lm := range selector.LabelMatchers {
if lm.Name == labels.MetricName {
s.LabelMatchers = append(s.LabelMatchers, lm)
}
}
s := stripLabels(selector)
p := c.countSeries(expr, s)
// if we have zero series without any label selector then the whole
// series is missing, but if we have some then report missing series
Expand Down Expand Up @@ -118,3 +117,16 @@ func getSelectors(n *parser.PromQLNode) (selectors []promParser.VectorSelector)

return
}

func stripLabels(selector promParser.VectorSelector) promParser.VectorSelector {
s := promParser.VectorSelector{
Name: selector.Name,
LabelMatchers: []*labels.Matcher{},
}
for _, lm := range selector.LabelMatchers {
if lm.Name == labels.MetricName {
s.LabelMatchers = append(s.LabelMatchers, lm)
}
}
return s
}
47 changes: 46 additions & 1 deletion internal/checks/query_series_test.go
Expand Up @@ -22,7 +22,7 @@ func TestSeriesCheck(t *testing.T) {
query := r.Form.Get("query")

switch query {
case "count(notfound)", `count({__name__="notfound",job="bar"})`:
case "count(notfound)", `count(notfound{job="foo"})`, `count(notfound{job!="foo"})`, `count({__name__="notfound",job="bar"})`:
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
Expand Down Expand Up @@ -241,6 +241,51 @@ func TestSeriesCheck(t *testing.T) {
},
},
},
{
description: "series missing but check disabled",
content: `
# pint disable query/series(notfound)
- record: foo
expr: count(notfound) == 0
`,
checker: checks.NewSeriesCheck("prom", srv.URL, time.Second*5),
},
{
description: "series missing but check disabled, labels",
content: `
# pint disable query/series(notfound)
- record: foo
expr: count(notfound{job="foo"}) == 0
`,
checker: checks.NewSeriesCheck("prom", srv.URL, time.Second*5),
},
{
description: "series missing but check disabled, negative labels",
content: `
# pint disable query/series(notfound)
- record: foo
expr: count(notfound{job!="foo"}) == 0
`,
checker: checks.NewSeriesCheck("prom", srv.URL, time.Second*5),
},
{
description: "series missing, disabled comment for labels",
content: `
# pint disable query/series(notfound{job="foo"})
- record: foo
expr: count(notfound) == 0
`,
checker: checks.NewSeriesCheck("prom", srv.URL, time.Second*5),
problems: []checks.Problem{
{
Fragment: `notfound`,
Lines: []int{4},
Reporter: "query/series",
Text: `query using prom completed without any results for notfound`,
Severity: checks.Warning,
},
},
},
}
runTests(t, testCases)
}

0 comments on commit 080b29f

Please sign in to comment.