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

Use LabelsSource for alerts/absent #1164

Merged
merged 1 commit into from
Oct 24, 2024
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
12 changes: 10 additions & 2 deletions internal/checks/alerts_absent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
"github.com/cloudflare/pint/internal/parser/utils"
"github.com/cloudflare/pint/internal/promapi"

"github.com/prometheus/common/model"
promParser "github.com/prometheus/prometheus/promql/parser"
)

const (
Expand Down Expand Up @@ -58,7 +58,15 @@ func (c AlertsAbsentCheck) Check(ctx context.Context, _ discovery.Path, rule par
return problems
}

if n, ok := rule.AlertingRule.Expr.Query.Expr.(*promParser.Call); !ok || n.Func.Name != "absent" {
var hasAbsent bool
src := utils.LabelsSource(rule.AlertingRule.Expr.Query)
for _, s := range append(src.Alternatives, src) {
if s.Operation == "absent" {
hasAbsent = true
}
}

if !hasAbsent {
return problems
}

Expand Down
33 changes: 33 additions & 0 deletions internal/checks/alerts_absent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func TestAlertsAbsentCheck(t *testing.T) {
prometheus: newSimpleProm,
problems: noProblems,
},
{
description: "ignores rules with no absent()",
content: "- alert: foo\n expr: count(foo)\n for: 2m\n",
checker: newAlertsAbsentCheck,
prometheus: newSimpleProm,
problems: noProblems,
},
{
description: "ignores rules with invalid duration",
content: "- alert: foo\n expr: absent(foo)\n for: abc\n",
Expand All @@ -47,6 +54,32 @@ func TestAlertsAbsentCheck(t *testing.T) {
},
},
},
{
description: "count() or absent() without for",
content: "- alert: foo\n expr: count(foo) > 5 or absent(foo)\n",
checker: newAlertsAbsentCheck,
prometheus: newSimpleProm,
problems: func(uri string) []checks.Problem {
return []checks.Problem{
{
Lines: parser.LineRange{
First: 2,
Last: 2,
},
Reporter: checks.AlertsAbsentCheckName,
Text: absentForNeeded("prom", uri, "2m"),
Details: checks.AlertsAbsentCheckDetails,
Severity: checks.Warning,
},
}
},
mocks: []*prometheusMock{
{
conds: []requestCondition{requireConfigPath},
resp: configResponse{yaml: "global:\n scrape_interval: 1m\n"},
},
},
},
{
description: "absent() without for",
content: "- alert: foo\n expr: absent(foo)\n",
Expand Down
78 changes: 0 additions & 78 deletions internal/parser/utils/absent.go

This file was deleted.

150 changes: 0 additions & 150 deletions internal/parser/utils/absent_test.go

This file was deleted.