Skip to content

Commit be3a661

Browse files
authored
Merge pull request #1703 from cloudflare/ignoring
Check ignoring() when validating joins
2 parents 6137d37 + 90ed082 commit be3a661

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Fixed
1111

1212
- Fixed a crash in [rule/label](checks/rule/label.md) - [#1696](https://github.com/cloudflare/pint/pull/1696).
13+
- Fixed false positive reports from [promql/impossible](checks/promql/impossible.md) check.
1314

1415
## v0.78.0
1516

internal/checks/promql_impossible_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,20 @@ func TestImpossibleCheck(t *testing.T) {
703703
, "workload", "", "owner_name")
704704
, "workload_type", "", "owner_kind")
705705
)
706+
`,
707+
checker: newImpossibleCheck,
708+
prometheus: newSimpleProm,
709+
},
710+
{
711+
description: "sum(...) by(a,b) / ignoring(b) sum(...) by(a)",
712+
content: `
713+
- alert: foo
714+
expr: |
715+
(
716+
sum(rate(panics_total{module_name=~".+"}[5m])) by (colo_name, module_name)
717+
/ ignoring(module_name) group_left
718+
sum(colo:requests:rate5m) by (colo_name)
719+
) > 0.01
706720
`,
707721
checker: newImpossibleCheck,
708722
prometheus: newSimpleProm,

internal/checks/promql_impossible_test.snap

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

32663266
---
32673267

3268+
[TestImpossibleCheck/sum(...)_by(a,b)_/_ignoring(b)_sum(...)_by(a) - 1]
3269+
[]
3270+
3271+
---
3272+
32683273
[TestImpossibleCheck/sum(foo_or_vector(0))_>_0 - 1]
32693274
- description: sum(foo or vector(0)) > 0
32703275
content: |4

internal/parser/source/source.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,9 @@ func canJoin(ls, rs Source, vm *promParser.VectorMatching) (bool, string, posran
14041404
if l.Kind != GuaranteedLabel {
14051405
continue
14061406
}
1407+
if slices.Contains(vm.MatchingLabels, name) {
1408+
continue
1409+
}
14071410
if ls.CanHaveLabel(name) && !rs.CanHaveLabel(name) {
14081411
reason, fragment := rs.LabelExcludeReason(name)
14091412
return false, fmt.Sprintf("The %s hand side will never be matched because it doesn't have the `%s` label while the left hand side will. %s",

internal/parser/source/source_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,14 @@ group by (cluster, namespace, workload, workload_type, pod) (
376376
`foo{job="xxx"} + ignoring(job) group_right(instance) bar{job="zzz"}`,
377377
`foo or ignoring(job) bar`,
378378
`foo or on(job) bar`,
379+
`
380+
(
381+
sum(rate(panics_total{module_name=~".+"}[5m])) by (colo_name, module_name)
382+
/
383+
ignoring(module_name) group_left
384+
sum(colo:requests:rate5m) by (colo_name)
385+
) > 0.01
386+
`,
379387
}
380388

381389
func TestLabelsSource(t *testing.T) {

internal/parser/source/source_test.snap

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11813,3 +11813,98 @@ output:
1181311813
type: selector
1181411814

1181511815
---
11816+
11817+
[TestLabelsSource/184 - 1]
11818+
expr: |4
11819+
11820+
(
11821+
sum(rate(panics_total{module_name=~".+"}[5m])) by (colo_name, module_name)
11822+
/
11823+
ignoring(module_name) group_left
11824+
sum(colo:requests:rate5m) by (colo_name)
11825+
) > 0.01
11826+
output:
11827+
- labels:
11828+
"":
11829+
reason: Query is using aggregation with `by(colo_name, module_name)`, only labels included inside `by(...)` will be present on the results.
11830+
kind: excluded
11831+
fragment:
11832+
start: 52
11833+
end: 55
11834+
__name__:
11835+
reason: Binary operation between two vectors removes metric names.
11836+
kind: excluded
11837+
fragment:
11838+
start: 5
11839+
end: 161
11840+
colo_name:
11841+
reason: Query is using aggregation with `by(colo_name, module_name)`, only labels included inside `by(...)` will be present on the results.
11842+
kind: included
11843+
fragment:
11844+
start: 56
11845+
end: 65
11846+
module_name:
11847+
reason: Query will only return series where these labels are present.
11848+
kind: guaranteed
11849+
fragment:
11850+
start: 9
11851+
end: 50
11852+
returns: vector
11853+
operations:
11854+
- node: '[*parser.VectorSelector] panics_total{module_name=~".+"}'
11855+
op: ""
11856+
- node: '[*parser.Call] rate(panics_total{module_name=~".+"}[5m])'
11857+
op: rate
11858+
- node: '[*parser.AggregateExpr] sum by (colo_name, module_name) (rate(panics_total{module_name=~".+"}[5m]))'
11859+
op: sum
11860+
joins:
11861+
- matchinglabels:
11862+
- module_name
11863+
addedlabels: []
11864+
src:
11865+
labels:
11866+
"":
11867+
reason: Query is using aggregation with `by(colo_name)`, only labels included inside `by(...)` will be present on the results.
11868+
kind: excluded
11869+
fragment:
11870+
start: 147
11871+
end: 150
11872+
__name__:
11873+
reason: Aggregation removes metric name.
11874+
kind: excluded
11875+
fragment:
11876+
start: 121
11877+
end: 161
11878+
colo_name:
11879+
reason: Query is using aggregation with `by(colo_name)`, only labels included inside `by(...)` will be present on the results.
11880+
kind: included
11881+
fragment:
11882+
start: 151
11883+
end: 160
11884+
returns: vector
11885+
operations:
11886+
- node: '[*parser.VectorSelector] colo:requests:rate5m'
11887+
op: ""
11888+
- node: '[*parser.AggregateExpr] sum by (colo_name) (colo:requests:rate5m)'
11889+
op: sum
11890+
usedLabels:
11891+
- colo_name
11892+
position:
11893+
start: 125
11894+
end: 145
11895+
type: aggregation
11896+
fixedLabels: true
11897+
op: 57384
11898+
depth: 0
11899+
ison: false
11900+
usedLabels:
11901+
- colo_name
11902+
- module_name
11903+
position:
11904+
start: 14
11905+
end: 49
11906+
type: aggregation
11907+
fixedLabels: true
11908+
isConditional: true
11909+
11910+
---

0 commit comments

Comments
 (0)