Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive in alerts/template check #685

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fixed a crash in `promql/series` check when Prometheus instance becomes
unavailable - #682.
- Fixed false positive reports in `alerts/template` check - #681.

## v0.44.1

Expand Down
25 changes: 24 additions & 1 deletion internal/checks/alerts_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func TestTemplateCheck(t *testing.T) {
},
{
description: "annotation label missing from metrics (1+)",
content: "- alert: Foo Is Down\n expr: 1 + sum(foo) by(job) + sum(foo) by(notjob)\n annotations:\n summary: '{{ .Labels.job }}'\n",
content: "- alert: Foo Is Down\n expr: 1 + sum(foo) by(notjob)\n annotations:\n summary: '{{ .Labels.job }}'\n",
checker: newTemplateCheck,
prometheus: noProm,
problems: func(uri string) []checks.Problem {
Expand Down Expand Up @@ -989,6 +989,29 @@ func TestTemplateCheck(t *testing.T) {
sum(foo:count) by(job) > 20
labels:
notify: "{{ $labels.notify }}"
`,
checker: newTemplateCheck,
prometheus: noProm,
problems: noProblems,
},
{
description: "abs / scalar",
content: `
- alert: ScyllaNonBalancedcqlTraffic
expr: >
abs(rate(scylla_cql_updates{conditional="no"}[1m]) - scalar(avg(rate(scylla_cql_updates{conditional="no"}[1m]))))
/
scalar(stddev(rate(scylla_cql_updates{conditional="no"}[1m])) + 100) > 2
for: 10s
labels:
advisor: balanced
dashboard: cql
severity: moderate
status: "1"
team: team_devops
annotations:
description: CQL queries are not balanced among shards {{ $labels.instance }} shard {{ $labels.shard }}
summary: CQL queries are not balanced
`,
checker: newTemplateCheck,
prometheus: noProm,
Expand Down
10 changes: 9 additions & 1 deletion internal/parser/utils/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,22 @@ NEXT:
aggs = append(aggs, HasOuterAggregation(child)...)
}
return aggs
case promParser.DIV, promParser.LUNLESS, promParser.LAND:
case promParser.LUNLESS, promParser.LAND:
for _, child := range node.Children {
return HasOuterAggregation(child)
}
case promParser.DIV, promParser.SUB, promParser.ADD:
if _, ok := n.LHS.(*promParser.NumberLiteral); ok {
goto CHILDREN
}
for _, child := range node.Children {
return HasOuterAggregation(child)
}
}
}
}

CHILDREN:
for _, child := range node.Children {
aggs = append(aggs, HasOuterAggregation(child)...)
}
Expand Down
13 changes: 11 additions & 2 deletions internal/parser/utils/aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestHasOuterAggregation(t *testing.T) {
},
{
expr: "sum(foo) + sum(bar)",
output: []string{"sum(foo)", "sum(bar)"},
output: []string{"sum(foo)"},
},
{
expr: "foo / on(bbb) sum(bar)",
Expand All @@ -64,7 +64,7 @@ func TestHasOuterAggregation(t *testing.T) {
},
{
expr: "1 + sum(foo) by(job) + sum(foo) by(notjob)",
output: []string{"sum by (job) (foo)", "sum by (notjob) (foo)"},
output: []string{"sum by (job) (foo)"},
},
{
expr: "sum(foo) by (job) > count(bar)",
Expand Down Expand Up @@ -128,6 +128,15 @@ func TestHasOuterAggregation(t *testing.T) {
expr: "(quantile(0.9, foo))",
output: []string{"quantile(0.9, foo)"},
},
{
expr: `abs(rate(scylla_cql_updates{conditional="no"}[1m]) - scalar(avg(rate(scylla_cql_updates{conditional="no"}[1m]))))`,
},
{
expr: `abs(rate(scylla_cql_updates{conditional="no"}[1m]) + scalar(avg(rate(scylla_cql_updates{conditional="no"}[1m]))))`,
},
{
expr: `abs(rate(scylla_cql_updates{conditional="no"}[1m]) / scalar(avg(rate(scylla_cql_updates{conditional="no"}[1m]))))`,
},
}

for _, tc := range testCases {
Expand Down