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

Pass the query node directly #1179

Merged
merged 1 commit into from
Oct 29, 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
2 changes: 1 addition & 1 deletion internal/checks/alerts_absent.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c AlertsAbsentCheck) Check(ctx context.Context, _ discovery.Path, rule par
}

var hasAbsent bool
src := utils.LabelsSource(rule.AlertingRule.Expr.Value.Value, rule.AlertingRule.Expr.Query)
src := utils.LabelsSource(rule.AlertingRule.Expr.Value.Value, rule.AlertingRule.Expr.Query.Expr)
for _, s := range append(src.Alternatives, src) {
if s.Operation == "absent" {
hasAbsent = true
Expand Down
2 changes: 1 addition & 1 deletion internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c TemplateCheck) Check(ctx context.Context, _ discovery.Path, rule parser.
return nil
}

src := utils.LabelsSource(rule.AlertingRule.Expr.Value.Value, rule.AlertingRule.Expr.Query)
src := utils.LabelsSource(rule.AlertingRule.Expr.Value.Value, rule.AlertingRule.Expr.Query.Expr)
data := promTemplate.AlertTemplateData(map[string]string{}, map[string]string{}, "", promql.Sample{})

if rule.AlertingRule.Labels != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/checks/promql_fragile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c FragileCheck) Check(_ context.Context, _ discovery.Path, rule parser.Rul
}

if rule.AlertingRule != nil {
for _, problem := range c.checkSampling(expr.Value.Value, expr.Query) {
for _, problem := range c.checkSampling(expr.Value.Value, expr.Query.Expr) {
problems = append(problems, Problem{
Lines: expr.Value.Lines,
Reporter: c.Reporter(),
Expand Down Expand Up @@ -126,7 +126,7 @@ NEXT:
return problems
}

func (c FragileCheck) checkSampling(expr string, node *parser.PromQLNode) (problems []exprProblem) {
func (c FragileCheck) checkSampling(expr string, node promParser.Node) (problems []exprProblem) {
s := utils.LabelsSource(expr, node)
for _, src := range append(s.Alternatives, s) {
if src.Type != utils.AggregateSource {
Expand Down
6 changes: 2 additions & 4 deletions internal/parser/utils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"slices"
"strings"

"github.com/cloudflare/pint/internal/parser"

"github.com/prometheus/prometheus/model/labels"
promParser "github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/promql/parser/posrange"
Expand Down Expand Up @@ -42,8 +40,8 @@ type Source struct {
FixedLabels bool // Labels are fixed and only allowed labels can be present.
}

func LabelsSource(expr string, node *parser.PromQLNode) Source {
return walkNode(expr, node.Expr)
func LabelsSource(expr string, node promParser.Node) Source {
return walkNode(expr, node)
}

func walkNode(expr string, node promParser.Node) (s Source) {
Expand Down
6 changes: 3 additions & 3 deletions internal/parser/utils/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_us
t.Error(err)
t.FailNow()
}
output := utils.LabelsSource(tc.expr, n)
output := utils.LabelsSource(tc.expr, n.Expr)
require.EqualExportedValues(t, tc.output, output)
})
}
Expand Down Expand Up @@ -1655,7 +1655,7 @@ func TestLabelsSourceCallCoverage(t *testing.T) {
t.Error(err)
t.FailNow()
}
output := utils.LabelsSource(b.String(), n)
output := utils.LabelsSource(b.String(), n.Expr)
require.NotNil(t, output.Call, "no call detected in: %q ~> %+v", b.String(), output)
require.Equal(t, name, output.Operation)
require.Equal(t, def.ReturnType, output.Returns, "incorrect return type on Source{}")
Expand All @@ -1671,6 +1671,6 @@ func TestLabelsSourceCallCoverageFail(t *testing.T) {
},
},
}
output := utils.LabelsSource("fake_call()", n)
output := utils.LabelsSource("fake_call()", n.Expr)
require.Nil(t, output.Call, "no call should have been detected in fake function")
}