Skip to content

Commit 084cbf7

Browse files
authored
Merge pull request #1752 from cloudflare/query
Fix query/cost suggestions
2 parents 8f08caf + 281e23d commit 084cbf7

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [promql/vector_matching](checks/promql/vector_matching.md) check no longer reports problems that are also reported by [promql/impossible](checks/promql/impossible.md).
88
- Fixed problem description in [promql/series](checks/promql/series.md) check for
99
disable comments that have no effect.
10+
- Fixed invalid suggestions from the [query/cost](checks/query/cost.md) check.
1011

1112
## v0.79.0
1213

internal/checks/query_cost.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ func (c CostCheck) isSuggestionFor(src, potential source.Source, join *source.Jo
478478
goto NEXT
479479
}
480480
}
481+
for _, name := range src.TransformedLabels(source.PossibleLabel, source.GuaranteedLabel) {
482+
if !potential.CanHaveLabel(name) {
483+
goto NEXT
484+
}
485+
}
481486
var extra string
482487
if len(lms) > 0 {
483488
var buf strings.Builder

internal/checks/query_cost_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,59 @@ func TestCostCheck(t *testing.T) {
17051705
},
17061706
},
17071707
},
1708+
{
1709+
// Query uses by(instance) aggregation but the recording rule uses without(instance),
1710+
// suggesting this rule would produce different label sets and is not a valid substitution.
1711+
description: "suggest recording rule / by vs without mismatch",
1712+
content: "- alert: foo\n expr: sum by(instance) (rate(foo_total[5m])) > 10\n",
1713+
checker: func(prom *promapi.FailoverGroup) checks.RuleChecker {
1714+
return checks.NewCostCheck(prom, 100, 100, 0, 0, "check comment", checks.Warning)
1715+
},
1716+
prometheus: newSimpleProm,
1717+
entries: mustParseContent(`
1718+
- record: colo:foo
1719+
expr: sum(rate(foo_total[5m])) without(instance)
1720+
`),
1721+
mocks: []*prometheusMock{
1722+
{
1723+
conds: []requestCondition{
1724+
requireQueryPath,
1725+
formCond{key: "query", value: "count(\nsum by(instance) (rate(foo_total[5m])) > 10\n)"},
1726+
},
1727+
resp: vectorResponse{
1728+
samples: []*model.Sample{
1729+
generateSample(map[string]string{}),
1730+
generateSample(map[string]string{}),
1731+
generateSample(map[string]string{}),
1732+
generateSample(map[string]string{}),
1733+
generateSample(map[string]string{}),
1734+
generateSample(map[string]string{}),
1735+
generateSample(map[string]string{}),
1736+
},
1737+
stats: promapi.QueryStats{
1738+
Samples: promapi.QuerySamples{
1739+
TotalQueryableSamples: 99,
1740+
PeakSamples: 19,
1741+
},
1742+
Timings: promapi.QueryTimings{
1743+
EvalTotalTime: 60.3,
1744+
},
1745+
},
1746+
},
1747+
},
1748+
{
1749+
conds: []requestCondition{
1750+
requireQueryPath,
1751+
formCond{key: "query", value: checks.BytesPerSampleQuery},
1752+
},
1753+
resp: vectorResponse{
1754+
samples: []*model.Sample{
1755+
generateSampleWithValue(map[string]string{}, 2048),
1756+
},
1757+
},
1758+
},
1759+
},
1760+
},
17081761
{
17091762
description: "comments at the end",
17101763
content: `

internal/checks/query_cost_test.snap

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

398398
---
399399

400+
[TestCostCheck/suggest_recording_rule_/_by_vs_without_mismatch - 1]
401+
[]
402+
403+
---
404+
400405
[TestCostCheck/suggest_recording_rule_/_complex - 1]
401406
- description: suggest recording rule / complex
402407
content: |

0 commit comments

Comments
 (0)