Skip to content

Commit b600868

Browse files
committed
Updating to reflect Prometheus v3 improvements, including new functions + new aggregate operators.
1 parent 7c2ed31 commit b600868

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/PromQL.Parser/Functions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class Functions
99
/// </summary>
1010
/// <remarks>
1111
/// Primarily taken from https://github.com/prometheus/prometheus/blob/main/web/ui/module/codemirror-promql/src/grammar/promql.grammar#L121-L188.
12-
/// More authoritative source would be https://github.com/prometheus/prometheus/blob/7471208b5c8ff6b65b644adedf7eb964da3d50ae/promql/parser/functions.go.
12+
/// More authoritative source would be https://github.com/prometheus/prometheus/blob/38fd48e6b54b5413b5281efbeeb989a44845be34/promql/parser/functions.go.
1313
/// </remarks>
1414
public static ImmutableDictionary<string, Function> Map { get; set; } = new[]
1515
{
@@ -34,15 +34,23 @@ public static class Functions
3434
new Function("days_in_month", ValueType.Vector, varadicModifier: 1 , ValueType.Vector),
3535
new Function("day_of_month", ValueType.Vector, varadicModifier: 1, ValueType.Vector),
3636
new Function("day_of_week", ValueType.Vector, varadicModifier: 1, ValueType.Vector),
37+
new Function("day_of_year", ValueType.Vector, varadicModifier: 1, ValueType.Vector),
3738
new Function("deg", ValueType.Vector, ValueType.Vector),
3839
new Function("delta", ValueType.Vector, ValueType.Matrix),
3940
new Function("deriv", ValueType.Vector, ValueType.Matrix),
4041
new Function("exp", ValueType.Vector, ValueType.Vector),
4142
new Function("floor", ValueType.Vector, ValueType.Vector),
43+
new Function("histogram_avg", ValueType.Vector, ValueType.Vector),
44+
new Function("histogram_count", ValueType.Vector, ValueType.Vector),
45+
new Function("histogram_sum", ValueType.Vector, ValueType.Vector),
46+
new Function("histogram_stddev", ValueType.Vector, ValueType.Vector),
47+
new Function("histogram_stdvar", ValueType.Vector, ValueType.Vector),
48+
new Function("histogram_fraction", ValueType.Vector, ValueType.Scalar, ValueType.Scalar, ValueType.Vector),
4249
new Function("histogram_quantile", ValueType.Vector, ValueType.Scalar, ValueType.Vector),
43-
new Function("holt_winters", ValueType.Vector, ValueType.Matrix, ValueType.Scalar, ValueType.Scalar),
50+
new Function("double_exponential_smoothing", ValueType.Vector, ValueType.Matrix, ValueType.Scalar, ValueType.Scalar),
4451
new Function("hour", ValueType.Vector, varadicModifier: 1, ValueType.Vector),
4552
new Function("idelta", ValueType.Vector, ValueType.Matrix),
53+
new Function("info", ValueType.Vector, varadicModifier: 1, ValueType.Vector, ValueType.Vector),
4654
new Function("increase", ValueType.Vector, ValueType.Matrix),
4755
new Function("irate", ValueType.Vector, ValueType.Matrix),
4856
new Function("label_replace", ValueType.Vector, ValueType.Vector, ValueType.String, ValueType.String, ValueType.String, ValueType.String),
@@ -51,6 +59,7 @@ public static class Functions
5159
new Function("ln", ValueType.Vector, ValueType.Vector),
5260
new Function("log_10", ValueType.Vector, ValueType.Vector),
5361
new Function("log_2", ValueType.Vector, ValueType.Vector),
62+
new Function("mad_over_time", ValueType.Vector, ValueType.Matrix),
5463
new Function("max_over_time", ValueType.Vector, ValueType.Matrix),
5564
new Function("min_over_time", ValueType.Vector, ValueType.Matrix),
5665
new Function("minute", ValueType.Vector, varadicModifier: 1, ValueType.Vector),
@@ -69,6 +78,8 @@ public static class Functions
6978
new Function("sinh", ValueType.Vector, ValueType.Vector),
7079
new Function("sort", ValueType.Vector, ValueType.Vector),
7180
new Function("sort_desc", ValueType.Vector, ValueType.Vector),
81+
new Function("sort_by_label", ValueType.Vector, varadicModifier: 0, ValueType.Vector, ValueType.String),
82+
new Function("sort_by_label_desc", ValueType.Vector, varadicModifier: 0, ValueType.Vector, ValueType.String),
7283
new Function("sqrt", ValueType.Vector, ValueType.Vector),
7384
new Function("stddev_over_time", ValueType.Vector, ValueType.Matrix),
7485
new Function("stdvar_over_time", ValueType.Vector, ValueType.Matrix),

src/PromQL.Parser/Operators.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public enum Unary
137137
new AggregateOperator("topk", ValueType.Scalar),
138138
new AggregateOperator("bottomk", ValueType.Scalar),
139139
new AggregateOperator("count_values", ValueType.String),
140-
new AggregateOperator("quantile", ValueType.Scalar)
140+
new AggregateOperator("quantile", ValueType.Scalar),
141+
new AggregateOperator("limitk", ValueType.Scalar),
142+
new AggregateOperator("limit_ratio", ValueType.Scalar)
141143
}.ToImmutableDictionary(k => k.Name, v => v, StringComparer.OrdinalIgnoreCase);
142144

143145
public static string ToPromQl(this Operators.Binary op) => op switch

tests/PromQL.Parser.Tests/ParserTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,26 @@ public void FunctionCall_InvalidParameterCountVaradic()
383383
[TestCase("hour()")]
384384
[TestCase("hour(one)")]
385385
[TestCase("hour(one, two)")]
386-
[TestCase("label_join(instant_vector, 'dst_label', 'separator', 'one', 'two')")]
387-
public void FunctionCall_Varadic(string query)
386+
public void FunctionCall_Varadic_One(string query)
388387
{
389388
Parse(Parser.Expr, query).Should().BeOfType<FunctionCall>();
390389
}
390+
391+
[Test]
392+
public void FunctionCall_Varadic_Label_Join()
393+
{
394+
var exp = Parse(Parser.Expr, "label_join(instant_vector, 'dst_label', 'separator', 'one', 'two')").Should().BeOfType<FunctionCall>().Subject;
395+
exp.Args.Should().HaveCount(5);
396+
}
397+
398+
[Test]
399+
[TestCase("sort_by_label(my_metric, 'one', 'two', 'three')")]
400+
[TestCase("sort_by_label_desc(my_metric, 'one', 'two', 'three')")]
401+
public void FunctionCall_Varadic_Sort(string query)
402+
{
403+
var exp = Parse(Parser.Expr, query).Should().BeOfType<FunctionCall>().Subject;
404+
exp.Args.Should().HaveCount(4);
405+
}
391406

392407
[Test]
393408
public void FunctionCall_OneArg() => Parse(Parser.Expr, "abs (1)")
@@ -850,6 +865,8 @@ public void BinaryExpr_BoolNonComparison(string query)
850865
[TestCase("stdvar (blah)", "stdvar")]
851866
[TestCase("sum (blah)", "sum")]
852867
[TestCase("topk (1, blah)", "topk")]
868+
[TestCase("limitk (1, blah)", "limitk")]
869+
[TestCase("limit_ratio (1, blah)", "limit_ratio")]
853870
public void AggregateExpr_Operator(string input, string expected) => Parse(Parser.AggregateExpr, input)
854871
.Operator.Name.Should().Be(expected);
855872

0 commit comments

Comments
 (0)