Skip to content

Commit 9d1a899

Browse files
authored
Merge pull request #1474 from cloudflare/query/cost
Don't suggest bogus replacements
2 parents c2ccaa4 + 8a1ea74 commit 9d1a899

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### Fixed
66

77
- Fixed false positive reports from [promql/fragile](checks/promql/fragile.md) check - [#1466](https://github.com/cloudflare/pint/issues/1466).
8+
- Fixed incorrect reports from [query/cost](checks/query/cost.md) when multiple rules with `rate(...)` were present
9+
and each had a different rate time window.
810

911
## v0.74.2
1012

internal/checks/query_cost.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ func (c CostCheck) suggestRecordingRules(
251251
continue
252252
}
253253

254+
// Don't replace the whole rule, that's usually not what we want.
255+
// Useful suggestions should find parts of the query that can be improved.
256+
if op.PositionRange().Start == expr.Query.Expr.PositionRange().Start &&
257+
op.PositionRange().End == expr.Query.Expr.PositionRange().End {
258+
continue
259+
}
260+
254261
sq := c.rewriteRuleFragment(expr.Value.Value, op.PositionRange(), other.Rule.RecordingRule.Record.Value+extra)
255262
var details strings.Builder
256263
qr, afterSeries, err := c.getQueryCost(ctx, sq)
@@ -414,9 +421,10 @@ func (c CostCheck) isSuggestionFor(src, potential utils.Source, join *utils.Join
414421
// * sum -> rate -> selector
415422
// * rate -> selector
416423
// - On same the selector.
417-
// - With the same labels possible. All? Only from joins?
424+
// - With the same labels possible.
418425

419426
// Src must have all operations potential does, so skip checks if potential is shorter.
427+
// Ideally potential is shorter than src because we're looking to speed up part of the query, not replace it.
420428
if len(potential.Operations) > len(src.Operations) {
421429
return nil, "", false, false
422430
}

internal/checks/query_cost_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,56 @@ func TestCostCheck(t *testing.T) {
13251325
},
13261326
},
13271327
},
1328+
{
1329+
description: "suggest recording rule / ignore whole rule",
1330+
content: `- record: sum:foo:rate5m
1331+
expr: sum(rate(foo_total[5m]))
1332+
`,
1333+
checker: func(prom *promapi.FailoverGroup) checks.RuleChecker {
1334+
return checks.NewCostCheck(prom, 100, 100, 0, 0, "check comment", checks.Warning)
1335+
},
1336+
prometheus: newSimpleProm,
1337+
entries: mustParseContent(`
1338+
1339+
- record: sum:foo:rate15m
1340+
expr: sum(rate(foo_total[15m]))
1341+
- record: sum:foo:rate30m
1342+
expr: sum(rate(foo_total[30m]))
1343+
`),
1344+
mocks: []*prometheusMock{
1345+
{
1346+
conds: []requestCondition{
1347+
requireQueryPath,
1348+
formCond{key: "query", value: "count(sum(rate(foo_total[5m])))"},
1349+
},
1350+
resp: vectorResponse{
1351+
samples: []*model.Sample{
1352+
generateSample(map[string]string{}),
1353+
},
1354+
stats: promapi.QueryStats{
1355+
Samples: promapi.QuerySamples{
1356+
TotalQueryableSamples: 50,
1357+
PeakSamples: 50,
1358+
},
1359+
Timings: promapi.QueryTimings{
1360+
EvalTotalTime: 10,
1361+
},
1362+
},
1363+
},
1364+
},
1365+
{
1366+
conds: []requestCondition{
1367+
requireQueryPath,
1368+
formCond{key: "query", value: checks.BytesPerSampleQuery},
1369+
},
1370+
resp: vectorResponse{
1371+
samples: []*model.Sample{
1372+
generateSampleWithValue(map[string]string{}, 2048),
1373+
},
1374+
},
1375+
},
1376+
},
1377+
},
13281378
}
13291379

13301380
runTests(t, testCases)

internal/checks/query_cost_test.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@
457457

458458
---
459459

460+
[TestCostCheck/suggest_recording_rule_/_ignore_whole_rule - 1]
461+
[]
462+
463+
---
464+
460465
[TestCostCheck/suggest_recording_rule_/_irate_vs_rate - 1]
461466
- description: suggest recording rule / irate vs rate
462467
content: |4

0 commit comments

Comments
 (0)