Skip to content

Commit

Permalink
Use cheaper queries for series check
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Mar 18, 2022
1 parent 5c9ddc0 commit d83d50e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.15.3

### Fixed

- Use more efficient queries for `promql/series` check.
- Fixed YAML parsing panics detected by Go 1.18 fuzzing.

## v0.15.2

### Fixed
Expand Down
8 changes: 5 additions & 3 deletions internal/checks/promql_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ func (c SeriesCheck) Check(ctx context.Context, rule parser.Rule, entries []disc

// 3. If foo is ALWAYS/SOMETIMES there BUT {bar OR baz} is NEVER there -> BUG
for _, name := range labelNames {
log.Debug().Str("check", c.Reporter()).Stringer("selector", &selector).Str("label", name).Msg("Checking if base metric has historical series with required label")
trsLabelCount, err := c.serieTimeRanges(ctx, fmt.Sprintf("count(%s) by (%s)", bareSelector.String(), name), rangeLookback, rangeStep)
l := stripLabels(selector)
l.LabelMatchers = append(l.LabelMatchers, labels.MustNewMatcher(labels.MatchRegexp, name, ".+"))
log.Debug().Str("check", c.Reporter()).Stringer("selector", &l).Str("label", name).Msg("Checking if base metric has historical series with required label")
trsLabelCount, err := c.serieTimeRanges(ctx, fmt.Sprintf("count(%s) by (%s)", l.String(), name), rangeLookback, rangeStep)
if err != nil {
problems = append(problems, c.queryProblem(err, selector.String(), expr))
continue
Expand All @@ -216,7 +218,7 @@ func (c SeriesCheck) Check(ctx context.Context, rule parser.Rule, entries []disc
promText(c.prom.Name(), trsLabelCount.uri), bareSelector.String(), name, trsLabelCount.sinceDesc(trsLabelCount.from)),
Severity: Bug,
})
log.Debug().Str("check", c.Reporter()).Stringer("selector", &selector).Str("label", name).Msg("No historical series with label used for the query")
log.Debug().Str("check", c.Reporter()).Stringer("selector", &l).Str("label", name).Msg("No historical series with label used for the query")
}

if len(trsLabelCount.labelValues(name)) == len(trsLabelCount.ranges) && trsLabelCount.avgLife() < (trsLabelCount.duration()/2) {
Expand Down
28 changes: 14 additions & 14 deletions internal/checks/promql_series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(found) by (job)`},
formCond{key: "query", value: `count(found{job=~".+"}) by (job)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand All @@ -565,7 +565,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(found) by (notfound)`},
formCond{key: "query", value: `count(found{notfound=~".+"}) by (notfound)`},
},
resp: respondWithSingleRangeVector1W(),
},
Expand Down Expand Up @@ -604,7 +604,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(found) by (notfound)`},
formCond{key: "query", value: `count(found{notfound=~".+"}) by (notfound)`},
},
resp: respondWithInternalError(),
},
Expand Down Expand Up @@ -652,7 +652,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(found) by (job)`},
formCond{key: "query", value: `count(found{job=~".+"}) by (job)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand All @@ -668,7 +668,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(found) by (instance)`},
formCond{key: "query", value: `count(found{instance=~".+"}) by (instance)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -716,7 +716,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: "count(found) by (instance)"},
formCond{key: "query", value: `count(found{instance=~".+"}) by (instance)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand All @@ -732,7 +732,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: "count(found) by (not)"},
formCond{key: "query", value: `count(found{not=~".+"}) by (not)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand All @@ -748,7 +748,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: "count(found) by (notfound)"},
formCond{key: "query", value: `count(found{notfound=~".+"}) by (notfound)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -819,7 +819,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: "count(found) by (error)"},
formCond{key: "query", value: `count(found{error=~".+"}) by (error)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -895,7 +895,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(sometimes) by (churn)`},
formCond{key: "query", value: `count(sometimes{churn=~".+"}) by (churn)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -962,7 +962,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count({__name__="found"}) by (removed)`},
formCond{key: "query", value: `count({__name__="found",removed=~".+"}) by (removed)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -1026,7 +1026,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(found) by (sometimes)`},
formCond{key: "query", value: `count(found{sometimes=~".+"}) by (sometimes)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -1147,7 +1147,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: `count(sometimes) by (foo)`},
formCond{key: "query", value: `count(sometimes{foo=~".+"}) by (foo)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down Expand Up @@ -1207,7 +1207,7 @@ func TestSeriesCheck(t *testing.T) {
{
conds: []requestCondition{
requireRangeQueryPath,
formCond{key: "query", value: "count(found) by (job)"},
formCond{key: "query", value: `count(found{job=~".+"}) by (job)`},
},
resp: matrixResponse{
samples: []*model.SampleStream{
Expand Down

0 comments on commit d83d50e

Please sign in to comment.