From b600868faedbfd70979bd1b3bed7c679fea0fde9 Mon Sep 17 00:00:00 2001 From: James Luck Date: Mon, 3 Mar 2025 14:48:13 +1100 Subject: [PATCH 1/3] Updating to reflect Prometheus v3 improvements, including new functions + new aggregate operators. --- src/PromQL.Parser/Functions.cs | 15 +++++++++++++-- src/PromQL.Parser/Operators.cs | 4 +++- tests/PromQL.Parser.Tests/ParserTests.cs | 21 +++++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/PromQL.Parser/Functions.cs b/src/PromQL.Parser/Functions.cs index de8c0d1..eb10934 100644 --- a/src/PromQL.Parser/Functions.cs +++ b/src/PromQL.Parser/Functions.cs @@ -9,7 +9,7 @@ public static class Functions /// /// /// Primarily taken from https://github.com/prometheus/prometheus/blob/main/web/ui/module/codemirror-promql/src/grammar/promql.grammar#L121-L188. - /// More authoritative source would be https://github.com/prometheus/prometheus/blob/7471208b5c8ff6b65b644adedf7eb964da3d50ae/promql/parser/functions.go. + /// More authoritative source would be https://github.com/prometheus/prometheus/blob/38fd48e6b54b5413b5281efbeeb989a44845be34/promql/parser/functions.go. /// public static ImmutableDictionary Map { get; set; } = new[] { @@ -34,15 +34,23 @@ public static class Functions new Function("days_in_month", ValueType.Vector, varadicModifier: 1 , ValueType.Vector), new Function("day_of_month", ValueType.Vector, varadicModifier: 1, ValueType.Vector), new Function("day_of_week", ValueType.Vector, varadicModifier: 1, ValueType.Vector), + new Function("day_of_year", ValueType.Vector, varadicModifier: 1, ValueType.Vector), new Function("deg", ValueType.Vector, ValueType.Vector), new Function("delta", ValueType.Vector, ValueType.Matrix), new Function("deriv", ValueType.Vector, ValueType.Matrix), new Function("exp", ValueType.Vector, ValueType.Vector), new Function("floor", ValueType.Vector, ValueType.Vector), + new Function("histogram_avg", ValueType.Vector, ValueType.Vector), + new Function("histogram_count", ValueType.Vector, ValueType.Vector), + new Function("histogram_sum", ValueType.Vector, ValueType.Vector), + new Function("histogram_stddev", ValueType.Vector, ValueType.Vector), + new Function("histogram_stdvar", ValueType.Vector, ValueType.Vector), + new Function("histogram_fraction", ValueType.Vector, ValueType.Scalar, ValueType.Scalar, ValueType.Vector), new Function("histogram_quantile", ValueType.Vector, ValueType.Scalar, ValueType.Vector), - new Function("holt_winters", ValueType.Vector, ValueType.Matrix, ValueType.Scalar, ValueType.Scalar), + new Function("double_exponential_smoothing", ValueType.Vector, ValueType.Matrix, ValueType.Scalar, ValueType.Scalar), new Function("hour", ValueType.Vector, varadicModifier: 1, ValueType.Vector), new Function("idelta", ValueType.Vector, ValueType.Matrix), + new Function("info", ValueType.Vector, varadicModifier: 1, ValueType.Vector, ValueType.Vector), new Function("increase", ValueType.Vector, ValueType.Matrix), new Function("irate", ValueType.Vector, ValueType.Matrix), new Function("label_replace", ValueType.Vector, ValueType.Vector, ValueType.String, ValueType.String, ValueType.String, ValueType.String), @@ -51,6 +59,7 @@ public static class Functions new Function("ln", ValueType.Vector, ValueType.Vector), new Function("log_10", ValueType.Vector, ValueType.Vector), new Function("log_2", ValueType.Vector, ValueType.Vector), + new Function("mad_over_time", ValueType.Vector, ValueType.Matrix), new Function("max_over_time", ValueType.Vector, ValueType.Matrix), new Function("min_over_time", ValueType.Vector, ValueType.Matrix), new Function("minute", ValueType.Vector, varadicModifier: 1, ValueType.Vector), @@ -69,6 +78,8 @@ public static class Functions new Function("sinh", ValueType.Vector, ValueType.Vector), new Function("sort", ValueType.Vector, ValueType.Vector), new Function("sort_desc", ValueType.Vector, ValueType.Vector), + new Function("sort_by_label", ValueType.Vector, varadicModifier: 0, ValueType.Vector, ValueType.String), + new Function("sort_by_label_desc", ValueType.Vector, varadicModifier: 0, ValueType.Vector, ValueType.String), new Function("sqrt", ValueType.Vector, ValueType.Vector), new Function("stddev_over_time", ValueType.Vector, ValueType.Matrix), new Function("stdvar_over_time", ValueType.Vector, ValueType.Matrix), diff --git a/src/PromQL.Parser/Operators.cs b/src/PromQL.Parser/Operators.cs index 8e6182d..7f9b546 100644 --- a/src/PromQL.Parser/Operators.cs +++ b/src/PromQL.Parser/Operators.cs @@ -137,7 +137,9 @@ public enum Unary new AggregateOperator("topk", ValueType.Scalar), new AggregateOperator("bottomk", ValueType.Scalar), new AggregateOperator("count_values", ValueType.String), - new AggregateOperator("quantile", ValueType.Scalar) + new AggregateOperator("quantile", ValueType.Scalar), + new AggregateOperator("limitk", ValueType.Scalar), + new AggregateOperator("limit_ratio", ValueType.Scalar) }.ToImmutableDictionary(k => k.Name, v => v, StringComparer.OrdinalIgnoreCase); public static string ToPromQl(this Operators.Binary op) => op switch diff --git a/tests/PromQL.Parser.Tests/ParserTests.cs b/tests/PromQL.Parser.Tests/ParserTests.cs index 59a5afa..831e4e7 100644 --- a/tests/PromQL.Parser.Tests/ParserTests.cs +++ b/tests/PromQL.Parser.Tests/ParserTests.cs @@ -383,11 +383,26 @@ public void FunctionCall_InvalidParameterCountVaradic() [TestCase("hour()")] [TestCase("hour(one)")] [TestCase("hour(one, two)")] - [TestCase("label_join(instant_vector, 'dst_label', 'separator', 'one', 'two')")] - public void FunctionCall_Varadic(string query) + public void FunctionCall_Varadic_One(string query) { Parse(Parser.Expr, query).Should().BeOfType(); } + + [Test] + public void FunctionCall_Varadic_Label_Join() + { + var exp = Parse(Parser.Expr, "label_join(instant_vector, 'dst_label', 'separator', 'one', 'two')").Should().BeOfType().Subject; + exp.Args.Should().HaveCount(5); + } + + [Test] + [TestCase("sort_by_label(my_metric, 'one', 'two', 'three')")] + [TestCase("sort_by_label_desc(my_metric, 'one', 'two', 'three')")] + public void FunctionCall_Varadic_Sort(string query) + { + var exp = Parse(Parser.Expr, query).Should().BeOfType().Subject; + exp.Args.Should().HaveCount(4); + } [Test] public void FunctionCall_OneArg() => Parse(Parser.Expr, "abs (1)") @@ -850,6 +865,8 @@ public void BinaryExpr_BoolNonComparison(string query) [TestCase("stdvar (blah)", "stdvar")] [TestCase("sum (blah)", "sum")] [TestCase("topk (1, blah)", "topk")] + [TestCase("limitk (1, blah)", "limitk")] + [TestCase("limit_ratio (1, blah)", "limit_ratio")] public void AggregateExpr_Operator(string input, string expected) => Parse(Parser.AggregateExpr, input) .Operator.Name.Should().Be(expected); From 9be19312e5943e7bdede781692d7d7ef2b08b8d6 Mon Sep 17 00:00:00 2001 From: James Luck Date: Mon, 3 Mar 2025 14:55:42 +1100 Subject: [PATCH 2/3] Updating to .net6 + 8.0 --- .github/workflows/publish-nuget-packages.yaml | 6 +++--- .github/workflows/run-tests.yaml | 12 ++++++------ src/PromQL.Parser/PromQL.Parser.csproj | 2 +- .../PromQL.Parser.Tests.csproj | Bin 1346 -> 1334 bytes 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/publish-nuget-packages.yaml b/.github/workflows/publish-nuget-packages.yaml index 48e2b30..f3b49bc 100644 --- a/.github/workflows/publish-nuget-packages.yaml +++ b/.github/workflows/publish-nuget-packages.yaml @@ -8,10 +8,10 @@ jobs: publish: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - uses: actions/setup-dotnet@v1 + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 with: - dotnet-version: '6.0.x' + dotnet-version: '8.0.x' - run: | echo "Github ref is ${GITHUB_REF}" arrTag=(${GITHUB_REF//\// }) diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 9b10467..e2185a0 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -9,13 +9,13 @@ jobs: test: runs-on: ubuntu-latest steps: - - name: Setup .NET Core 3.1 - uses: actions/setup-dotnet@v1 - with: - dotnet-version: 3.1.x - name: Setup .NET Core 6.0 - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v4 with: dotnet-version: '6.0.x' - - uses: actions/checkout@v1 + - name: Setup .NET Core 8.0 + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '8.0.x' + - uses: actions/checkout@v4 - run: dotnet test -c "Debug" \ No newline at end of file diff --git a/src/PromQL.Parser/PromQL.Parser.csproj b/src/PromQL.Parser/PromQL.Parser.csproj index c1f7d75..d140b24 100644 --- a/src/PromQL.Parser/PromQL.Parser.csproj +++ b/src/PromQL.Parser/PromQL.Parser.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net6.0 + net6.0;net8.0 10 enable true diff --git a/tests/PromQL.Parser.Tests/PromQL.Parser.Tests.csproj b/tests/PromQL.Parser.Tests/PromQL.Parser.Tests.csproj index 7ade3d5a28db9bbf4107b1b3828d92a5357259b6..0af98218f03fd31e78f16ff8ce079ea4864a7c51 100644 GIT binary patch delta 43 ycmX@awT)}SKYlX?Jq7~?Ylb|ARE81;i^ Date: Mon, 3 Mar 2025 15:00:17 +1100 Subject: [PATCH 3/3] Whoops looks like tests were never running! --- .../PromQL.Parser.Tests.csproj | Bin 1334 -> 1478 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/tests/PromQL.Parser.Tests/PromQL.Parser.Tests.csproj b/tests/PromQL.Parser.Tests/PromQL.Parser.Tests.csproj index 0af98218f03fd31e78f16ff8ce079ea4864a7c51..2c19004462ba2305cc81b96221b3b95d82fc66d9 100644 GIT binary patch delta 70 zcmdnSb&Pw%2PSrN20aEt2II+*Ou>`An5%%8OWc?tgdvron4yHhks*a4k)Z&{N@XaT UY|A9hY6?_u08+jA2y+o50B|-C;{X5v delta 28 kcmX@cy^U+b2PSqi20aEt2E)mWEWwk#n5#C2uoN)@0C{}}!~g&Q