Skip to content

Commit

Permalink
Merge pull request #1183 from cloudflare/vector
Browse files Browse the repository at this point in the history
Fix a crash in vector() parsing
  • Loading branch information
prymitive authored Oct 31, 2024
2 parents e9cce03 + f481b96 commit dd9cea9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.67.3

### Fixed

- Fixed a crash when parsing `vector()` calls with non-number arguments.

## v0.67.2

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion internal/parser/utils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,9 @@ If you're hoping to get instance specific labels this way and alert when some ta
s.GuaranteedLabels = nil
s.FixedLabels = true
s.AlwaysReturns = true
s.ReturnedNumbers = append(s.ReturnedNumbers, n.Args[0].(*promParser.NumberLiteral).Val)
if v, ok := n.Args[0].(*promParser.NumberLiteral); ok {
s.ReturnedNumbers = append(s.ReturnedNumbers, v.Val)
}
s.ExcludeReason = setInMap(
s.ExcludeReason,
"",
Expand Down
103 changes: 103 additions & 0 deletions internal/parser/utils/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,109 @@ sum(foo:count) by(job) > 20`,
},
},
},
{
expr: "vector(scalar(foo))",
output: []utils.Source{
{
Type: utils.FuncSource,
Returns: promParser.ValueTypeVector,
Operation: "vector",
FixedLabels: true,
AlwaysReturns: true,
ExcludeReason: map[string]utils.ExcludedLabel{
"": {
Reason: "Calling `vector()` will return a vector value with no labels.",
Fragment: `vector(scalar(foo))`,
},
},
Call: &promParser.Call{
Func: &promParser.Function{
Name: "vector",
ArgTypes: []promParser.ValueType{
promParser.ValueTypeScalar,
},
Variadic: 0,
ReturnType: promParser.ValueTypeVector,
},
Args: promParser.Expressions{
&promParser.Call{
Func: &promParser.Function{
Name: "scalar",
ArgTypes: []promParser.ValueType{
promParser.ValueTypeVector,
},
Variadic: 0,
ReturnType: promParser.ValueTypeScalar,
},
Args: promParser.Expressions{
mustParseVector("foo", 14),
},
PosRange: posrange.PositionRange{
Start: 7,
End: 18,
},
},
},
PosRange: posrange.PositionRange{
Start: 0,
End: 19,
},
},
},
},
},
{
expr: "vector(0.0 >= bool 0.5) == 1",
output: []utils.Source{
{
Type: utils.FuncSource,
Returns: promParser.ValueTypeVector,
Operation: "vector",
FixedLabels: true,
AlwaysReturns: true,
ExcludeReason: map[string]utils.ExcludedLabel{
"": {
Reason: "Calling `vector()` will return a vector value with no labels.",
Fragment: `vector(0.0 >= bool 0.5)`,
},
},
Call: &promParser.Call{
Func: &promParser.Function{
Name: "vector",
ArgTypes: []promParser.ValueType{
promParser.ValueTypeScalar,
},
Variadic: 0,
ReturnType: promParser.ValueTypeVector,
},
Args: promParser.Expressions{
&promParser.BinaryExpr{
Op: promParser.GTE,
LHS: &promParser.NumberLiteral{
Val: 0,
PosRange: posrange.PositionRange{
Start: 7,
End: 10,
},
},
RHS: &promParser.NumberLiteral{
Val: 0.5,
PosRange: posrange.PositionRange{
Start: 20,
End: 23,
},
},
ReturnBool: true,
},
},
PosRange: posrange.PositionRange{
Start: 0,
End: 24,
},
},
},
},
},
{
expr: `sum_over_time(foo{job="myjob"}[5m])`,
output: []utils.Source{
Expand Down

0 comments on commit dd9cea9

Please sign in to comment.