forked from cloudflare/pint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve promql/vector_matching check query rewrites
- Loading branch information
Showing
5 changed files
with
171 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package utils | ||
|
||
import ( | ||
promParser "github.com/prometheus/prometheus/promql/parser" | ||
) | ||
|
||
func RemoveConditions(source string) promParser.Node { | ||
node, _ := promParser.ParseExpr(source) | ||
switch n := node.(type) { | ||
case *promParser.AggregateExpr: | ||
n.Expr = RemoveConditions(n.Expr.String()).(promParser.Expr) | ||
return n | ||
case *promParser.BinaryExpr: | ||
lhs := RemoveConditions(n.LHS.String()) | ||
rhs := RemoveConditions(n.RHS.String()) | ||
_, ln := lhs.(*promParser.NumberLiteral) | ||
if v, ok := lhs.(*promParser.VectorSelector); ok && v.Name == "" { | ||
ln = true | ||
} | ||
_, rn := rhs.(*promParser.NumberLiteral) | ||
if v, ok := rhs.(*promParser.VectorSelector); ok && v.Name == "" { | ||
rn = true | ||
} | ||
if ln && rn { | ||
return &promParser.VectorSelector{} | ||
} | ||
if ln { | ||
return rhs | ||
} | ||
if rn { | ||
return lhs | ||
} | ||
n.LHS = lhs.(promParser.Expr) | ||
n.RHS = rhs.(promParser.Expr) | ||
return n | ||
case *promParser.Call: | ||
ret := promParser.Expressions{} | ||
for _, e := range n.Args { | ||
ret = append(ret, RemoveConditions(e.String()).(promParser.Expr)) | ||
} | ||
n.Args = ret | ||
return n | ||
case *promParser.SubqueryExpr: | ||
n.Expr = RemoveConditions(n.Expr.String()).(promParser.Expr) | ||
return n | ||
case *promParser.ParenExpr: | ||
n.Expr = RemoveConditions(n.Expr.String()).(promParser.Expr) | ||
switch n.Expr.(type) { | ||
case *promParser.NumberLiteral, *promParser.StringLiteral, *promParser.VectorSelector, *promParser.MatrixSelector: | ||
return n.Expr | ||
} | ||
return n | ||
case *promParser.UnaryExpr: | ||
n.Expr = RemoveConditions(n.Expr.String()).(promParser.Expr) | ||
return n | ||
case *promParser.StepInvariantExpr: | ||
n.Expr = RemoveConditions(n.Expr.String()).(promParser.Expr) | ||
return n | ||
case *promParser.NumberLiteral, *promParser.StringLiteral, *promParser.VectorSelector, *promParser.MatrixSelector: | ||
return node | ||
default: | ||
return node | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/cloudflare/pint/internal/parser/utils" | ||
) | ||
|
||
func TestRemoveConditions(t *testing.T) { | ||
type testCaseT struct { | ||
input string | ||
output string | ||
} | ||
|
||
testCases := []testCaseT{ | ||
{ | ||
input: "100", | ||
output: "100", | ||
}, | ||
{ | ||
input: "(100)", | ||
output: "100", | ||
}, | ||
{ | ||
input: "100 ^ 2", | ||
output: "", | ||
}, | ||
{ | ||
input: "(1024 ^ 2)", | ||
output: "", | ||
}, | ||
{ | ||
input: "(100*(1024^2))", | ||
output: "", | ||
}, | ||
{ | ||
input: "min_over_time((foo_with_notfound > 0)[30m:1m]) / bar", | ||
output: "min_over_time(foo_with_notfound[30m:1m]) / bar", | ||
}, | ||
{ | ||
input: "min_over_time(rate(http_requests_total[5m])[30m:1m])", | ||
output: "min_over_time(rate(http_requests_total[5m])[30m:1m])", | ||
}, | ||
{ | ||
input: "(memory_bytes / ignoring(job) (memory_limit > 0)) * on(app_name) group_left(a,b,c) app_registry", | ||
output: "(memory_bytes / ignoring(job) memory_limit) * on(app_name) group_left(a, b, c) app_registry", | ||
}, | ||
{ | ||
input: `(quantile_over_time(0.9, (rate(container_cpu_system_seconds_total{app_name="foo"}[5m]) + rate(container_cpu_user_seconds_total{app_name="foo"}[5m]))[5m:]) / on(instance) bar) > 0.65`, | ||
output: `(quantile_over_time(0.9, (rate(container_cpu_system_seconds_total{app_name="foo"}[5m]) + rate(container_cpu_user_seconds_total{app_name="foo"}[5m]))[5m:]) / on(instance) bar)`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.input, func(t *testing.T) { | ||
assert := assert.New(t) | ||
output := utils.RemoveConditions(tc.input) | ||
assert.Equalf(tc.output, output.String(), "input: %q", tc.input) | ||
}) | ||
} | ||
} |