2026-01-05 07:46:21 -05:00
|
|
|
// Copyright The Prometheus Authors
|
2015-06-12 03:42:36 -04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2020-02-03 11:23:44 -05:00
|
|
|
package parser
|
2015-06-12 03:42:36 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2019-10-09 20:06:53 -04:00
|
|
|
|
2020-10-29 05:43:23 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-22 04:19:38 -04:00
|
|
|
|
2021-11-08 09:23:17 -05:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2015-06-12 03:42:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExprString(t *testing.T) {
|
2025-06-25 11:39:27 -04:00
|
|
|
ExperimentalDurationExpr = true
|
2026-01-15 05:18:48 -05:00
|
|
|
EnableBinopFillModifiers = true
|
2025-06-25 11:39:27 -04:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
ExperimentalDurationExpr = false
|
2026-01-15 05:18:48 -05:00
|
|
|
EnableBinopFillModifiers = false
|
2025-06-25 11:39:27 -04:00
|
|
|
})
|
2015-06-12 03:42:36 -04:00
|
|
|
// A list of valid expressions that are expected to be
|
|
|
|
|
// returned as out when calling String(). If out is empty the output
|
|
|
|
|
// is expected to equal the input.
|
|
|
|
|
inputs := []struct {
|
|
|
|
|
in, out string
|
|
|
|
|
}{
|
2016-10-19 13:38:26 -04:00
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `sum by() (task:errors:rate10s{job="s"})`,
|
2016-10-19 13:38:26 -04:00
|
|
|
out: `sum(task:errors:rate10s{job="s"})`,
|
|
|
|
|
},
|
2015-06-12 03:42:36 -04:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `sum by(code) (task:errors:rate10s{job="s"})`,
|
|
|
|
|
out: `sum by (code) (task:errors:rate10s{job="s"})`,
|
2015-06-12 03:42:36 -04:00
|
|
|
},
|
2018-01-21 16:22:55 -05:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `sum without() (task:errors:rate10s{job="s"})`,
|
|
|
|
|
out: `sum without () (task:errors:rate10s{job="s"})`,
|
2018-01-21 16:22:55 -05:00
|
|
|
},
|
2016-02-07 13:03:16 -05:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `sum without(instance) (task:errors:rate10s{job="s"})`,
|
|
|
|
|
out: `sum without (instance) (task:errors:rate10s{job="s"})`,
|
2016-02-07 13:03:16 -05:00
|
|
|
},
|
2024-08-01 10:07:08 -04:00
|
|
|
{
|
|
|
|
|
in: `sum by("foo.bar") (task:errors:rate10s{job="s"})`,
|
|
|
|
|
out: `sum by ("foo.bar") (task:errors:rate10s{job="s"})`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `sum without("foo.bar") (task:errors:rate10s{job="s"})`,
|
|
|
|
|
out: `sum without ("foo.bar") (task:errors:rate10s{job="s"})`,
|
|
|
|
|
},
|
2016-07-04 08:10:42 -04:00
|
|
|
{
|
|
|
|
|
in: `topk(5, task:errors:rate10s{job="s"})`,
|
|
|
|
|
},
|
2016-07-05 12:12:19 -04:00
|
|
|
{
|
|
|
|
|
in: `count_values("value", task:errors:rate10s{job="s"})`,
|
|
|
|
|
},
|
2016-10-19 13:38:26 -04:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `a - on() c`,
|
|
|
|
|
out: `a - on () c`,
|
2016-10-19 13:38:26 -04:00
|
|
|
},
|
2016-04-21 06:45:06 -04:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `a - on(b) c`,
|
|
|
|
|
out: `a - on (b) c`,
|
2016-04-21 06:45:06 -04:00
|
|
|
},
|
2016-05-28 13:45:35 -04:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `a - on(b) group_left(x) c`,
|
|
|
|
|
out: `a - on (b) group_left (x) c`,
|
2016-05-28 13:45:35 -04:00
|
|
|
},
|
|
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `a - on(b) group_left(x, y) c`,
|
|
|
|
|
out: `a - on (b) group_left (x, y) c`,
|
2016-05-28 13:45:35 -04:00
|
|
|
},
|
|
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `a - on(b) group_left c`,
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
out: `a - on (b) group_left () c`,
|
2016-11-04 07:06:07 -04:00
|
|
|
},
|
|
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `a - on(b) group_left() (c)`,
|
|
|
|
|
out: `a - on (b) group_left () (c)`,
|
2016-05-28 13:45:35 -04:00
|
|
|
},
|
2016-04-21 06:45:06 -04:00
|
|
|
{
|
Prettifier: Add spaces with non-callable keywords (#11005)
* Prettifier: Add spaces with non-callable keywords
I prefer to have a difference between, on one side: functions calls, end(), start(), and on the other side with, without, ignoring, by and group_rrigt, group_left.
The reasoning is that the former ones are not calls, while other are
functions. Additionally, it matches the examples in our documentation.
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
* Fix tests
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2022-07-14 18:09:56 -04:00
|
|
|
in: `a - ignoring(b) c`,
|
|
|
|
|
out: `a - ignoring (b) c`,
|
2016-04-21 06:45:06 -04:00
|
|
|
},
|
2016-10-19 13:38:26 -04:00
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `a - ignoring() c`,
|
2016-10-19 13:38:26 -04:00
|
|
|
out: `a - c`,
|
|
|
|
|
},
|
2025-12-03 08:14:35 -05:00
|
|
|
{
|
|
|
|
|
// This is a bit of an odd case, but valid. If the user specifies ignoring() with
|
|
|
|
|
// no labels, it means that both label sets have to be exactly the same on both
|
|
|
|
|
// sides (except for the metric name). This is the same behavior as specifying
|
|
|
|
|
// no matching modifier at all, but if the user wants to include the metric name
|
|
|
|
|
// from either side in the output via group_x(__name__), they have to specify
|
|
|
|
|
// ignoring() explicitly to be able to do so, since the grammar does not allow
|
|
|
|
|
// grouping modifiers without either ignoring(...) or on(...). So we need to
|
|
|
|
|
// preserve the empty ignoring() clause in this case.
|
|
|
|
|
//
|
|
|
|
|
// a - group_left(__name__) c <--- Parse error
|
|
|
|
|
// a - ignoring() group_left(__name__) c <--- Valid
|
|
|
|
|
in: `a - ignoring() group_left(__metric__) c`,
|
|
|
|
|
out: `a - ignoring () group_left (__metric__) c`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a - ignoring() group_left c`,
|
|
|
|
|
out: `a - ignoring () group_left () c`,
|
|
|
|
|
},
|
2025-12-03 12:46:35 -05:00
|
|
|
{
|
|
|
|
|
in: `a + fill(-23) b`,
|
|
|
|
|
out: `a + fill (-23) b`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a + fill_left(-23) b`,
|
|
|
|
|
out: `a + fill_left (-23) b`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a + fill_right(42) b`,
|
|
|
|
|
out: `a + fill_right (42) b`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a + fill_left(-23) fill_right(42) b`,
|
|
|
|
|
out: `a + fill_left (-23) fill_right (42) b`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a + on(b) group_left fill(-23) c`,
|
|
|
|
|
out: `a + on (b) group_left () fill (-23) c`,
|
|
|
|
|
},
|
2015-09-09 19:37:05 -04:00
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `up > bool 0`,
|
2015-09-09 19:37:05 -04:00
|
|
|
},
|
2015-11-15 04:26:38 -05:00
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `a offset 1m`,
|
2015-11-15 04:26:38 -05:00
|
|
|
},
|
2021-02-23 21:16:28 -05:00
|
|
|
{
|
|
|
|
|
in: `a offset -7m`,
|
|
|
|
|
},
|
2015-11-15 04:26:38 -05:00
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `a{c="d"}[5m] offset 1m`,
|
2015-11-15 04:26:38 -05:00
|
|
|
},
|
|
|
|
|
{
|
2018-01-22 05:14:59 -05:00
|
|
|
in: `a[5m] offset 1m`,
|
2015-11-15 04:26:38 -05:00
|
|
|
},
|
2021-02-23 21:16:28 -05:00
|
|
|
{
|
|
|
|
|
in: `a[12m] offset -3m`,
|
|
|
|
|
},
|
2019-11-26 01:45:51 -05:00
|
|
|
{
|
|
|
|
|
in: `a[1h:5m] offset 1m`,
|
|
|
|
|
},
|
2025-10-17 06:17:32 -04:00
|
|
|
{
|
|
|
|
|
in: `a anchored`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a[5m] anchored`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b="c"}[5m] anchored`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b="c"}[5m] anchored offset 1m`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b="c"}[5m] anchored @ start() offset 1m`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a smoothed`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a[5m] smoothed`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b="c"}[5m] smoothed`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b="c"}[5m] smoothed offset 1m`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b="c"}[5m] smoothed @ start() offset 1m`,
|
|
|
|
|
},
|
2019-05-10 19:46:15 -04:00
|
|
|
{
|
|
|
|
|
in: `{__name__="a"}`,
|
|
|
|
|
},
|
2020-07-24 06:21:42 -04:00
|
|
|
{
|
|
|
|
|
in: `a{b!="c"}[1m]`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b=~"c"}[1m]`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a{b!~"c"}[1m]`,
|
|
|
|
|
},
|
2021-02-09 11:03:16 -05:00
|
|
|
{
|
|
|
|
|
in: `a @ 10`,
|
|
|
|
|
out: `a @ 10.000`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a[1m] @ 10`,
|
|
|
|
|
out: `a[1m] @ 10.000`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a @ start()`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a @ end()`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a[1m] @ start()`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `a[1m] @ end()`,
|
|
|
|
|
},
|
2024-05-06 05:51:08 -04:00
|
|
|
{
|
|
|
|
|
in: `{__name__="",a="x"}`,
|
|
|
|
|
},
|
2024-05-08 10:24:58 -04:00
|
|
|
{
|
|
|
|
|
in: `{"a.b"="c"}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `{"0"="1"}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `{"_0"="1"}`,
|
|
|
|
|
out: `{_0="1"}`,
|
|
|
|
|
},
|
2024-06-13 12:46:35 -04:00
|
|
|
{
|
|
|
|
|
in: `{""="0"}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "{``=\"0\"}",
|
|
|
|
|
out: `{""="0"}`,
|
|
|
|
|
},
|
2025-03-03 04:03:45 -05:00
|
|
|
{
|
|
|
|
|
in: "1048576",
|
|
|
|
|
out: "1048576",
|
|
|
|
|
},
|
2025-06-25 11:39:27 -04:00
|
|
|
{
|
|
|
|
|
in: "foo[step()]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[-step()]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[(step())]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[-(step())]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset step()",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset -step()",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset (step())",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset -(step())",
|
|
|
|
|
},
|
2025-07-01 05:46:03 -04:00
|
|
|
{
|
|
|
|
|
in: "foo offset +(5*2)",
|
|
|
|
|
out: "foo offset (5 * 2)",
|
|
|
|
|
},
|
2025-07-01 05:57:31 -04:00
|
|
|
{
|
|
|
|
|
in: "foo offset +min(10s, 20s)",
|
|
|
|
|
out: "foo offset min(10s, 20s)",
|
|
|
|
|
},
|
2025-07-01 05:46:03 -04:00
|
|
|
{
|
|
|
|
|
in: "foo offset -min(10s, 20s)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset -min(10s, +max(step() ^ 2, 2))",
|
|
|
|
|
out: "foo offset -min(10s, max(step() ^ 2, 2))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[200-min(-step()^+step(),1)]",
|
|
|
|
|
out: "foo[200 - min(-step() ^ step(), 1)]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[200 - min(step() + 10s, -max(step() ^ 2, 3))]",
|
|
|
|
|
},
|
2025-12-10 10:16:08 -05:00
|
|
|
{
|
|
|
|
|
in: "foo[range()]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[-range()]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset range()",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo offset -range()",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: "foo[max(range(), 5s)]",
|
|
|
|
|
},
|
2025-05-14 13:32:53 -04:00
|
|
|
{
|
|
|
|
|
in: `predict_linear(foo[1h], 3000)`,
|
|
|
|
|
},
|
2025-12-14 15:18:06 -05:00
|
|
|
{
|
|
|
|
|
in: `sum by("üüü") (foo)`,
|
|
|
|
|
out: `sum by ("üüü") (foo)`,
|
|
|
|
|
},
|
2025-12-15 07:38:12 -05:00
|
|
|
{
|
|
|
|
|
in: `sum without("äää") (foo)`,
|
|
|
|
|
out: `sum without ("äää") (foo)`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
in: `count by("ööö", job) (foo)`,
|
|
|
|
|
out: `count by ("ööö", job) (foo)`,
|
|
|
|
|
},
|
2015-06-12 03:42:36 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-17 06:17:32 -04:00
|
|
|
EnableExtendedRangeSelectors = true
|
2025-10-17 06:30:57 -04:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
EnableExtendedRangeSelectors = false
|
|
|
|
|
})
|
2025-10-17 06:17:32 -04:00
|
|
|
|
2015-06-12 03:42:36 -04:00
|
|
|
for _, test := range inputs {
|
2025-07-01 05:46:03 -04:00
|
|
|
t.Run(test.in, func(t *testing.T) {
|
|
|
|
|
expr, err := ParseExpr(test.in)
|
|
|
|
|
require.NoError(t, err)
|
2019-10-09 20:06:53 -04:00
|
|
|
|
2025-07-01 05:46:03 -04:00
|
|
|
exp := test.in
|
|
|
|
|
if test.out != "" {
|
|
|
|
|
exp = test.out
|
|
|
|
|
}
|
2019-10-09 20:06:53 -04:00
|
|
|
|
2025-07-01 05:46:03 -04:00
|
|
|
require.Equal(t, exp, expr.String())
|
|
|
|
|
})
|
2015-06-12 03:42:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-01 03:48:18 -04:00
|
|
|
|
2025-05-12 09:27:49 -04:00
|
|
|
func BenchmarkExprString(b *testing.B) {
|
|
|
|
|
inputs := []string{
|
|
|
|
|
`sum by(code) (task:errors:rate10s{job="s"})`,
|
|
|
|
|
`max( 100 * (1 - avg by(instance) (irate(node_cpu_seconds_total{instance=~".*cust01.prd.*",mode="idle"}[86400s]))))`,
|
|
|
|
|
`http_requests_total{job="api-server", group="canary"} + rate(http_requests_total{job="api-server"}[10m]) * 5 * 60`,
|
|
|
|
|
`sum by (pod) ((kube_pod_container_status_restarts_total{namespace="mynamespace",cluster="mycluster"} - kube_pod_container_status_restarts_total{namespace="mynamespace}",cluster="mycluster}"} offset 10m) >= 1 and ignoring (reason) min_over_time(kube_pod_container_status_last_terminated_reason{namespace="mynamespace",cluster="mycluster",reason="OOMKilled"}[10m]) == 1)`,
|
|
|
|
|
`sum by (pod) ((kube_pod_container_status_restarts_total{cluster="mycluster",namespace="mynamespace"} - kube_pod_container_status_restarts_total{cluster="mycluster",namespace="mynamespace}"} offset 10m) >= 1 and ignoring (reason) min_over_time(kube_pod_container_status_last_terminated_reason{cluster="mycluster",namespace="mynamespace",reason="OOMKilled"}[10m]) == 1)`, // Sort matchers.
|
|
|
|
|
`label_replace(testmetric, "dst", "destination-value-$1", "src", "source-value-(.*)")`,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range inputs {
|
|
|
|
|
b.Run(readable(test), func(b *testing.B) {
|
|
|
|
|
expr, err := ParseExpr(test)
|
|
|
|
|
require.NoError(b, err)
|
2025-11-04 00:13:49 -05:00
|
|
|
for b.Loop() {
|
2025-05-12 09:27:49 -04:00
|
|
|
_ = expr.String()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 03:48:18 -04:00
|
|
|
func TestVectorSelector_String(t *testing.T) {
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
vs VectorSelector
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "empty value",
|
|
|
|
|
vs: VectorSelector{},
|
|
|
|
|
expected: ``,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "no matchers with name",
|
|
|
|
|
vs: VectorSelector{Name: "foobar"},
|
|
|
|
|
expected: `foobar`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "one matcher with name",
|
|
|
|
|
vs: VectorSelector{
|
|
|
|
|
Name: "foobar",
|
|
|
|
|
LabelMatchers: []*labels.Matcher{
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "x"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `foobar{a="x"}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "two matchers with name",
|
|
|
|
|
vs: VectorSelector{
|
|
|
|
|
Name: "foobar",
|
|
|
|
|
LabelMatchers: []*labels.Matcher{
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "x"),
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "b", "y"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `foobar{a="x",b="y"}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "two matchers without name",
|
|
|
|
|
vs: VectorSelector{
|
|
|
|
|
LabelMatchers: []*labels.Matcher{
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "x"),
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "b", "y"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `{a="x",b="y"}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "name matcher and name",
|
|
|
|
|
vs: VectorSelector{
|
|
|
|
|
Name: "foobar",
|
|
|
|
|
LabelMatchers: []*labels.Matcher{
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, labels.MetricName, "foobar"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `foobar`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "name matcher only",
|
|
|
|
|
vs: VectorSelector{
|
|
|
|
|
LabelMatchers: []*labels.Matcher{
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, labels.MetricName, "foobar"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `{__name__="foobar"}`,
|
|
|
|
|
},
|
2024-05-06 05:51:08 -04:00
|
|
|
{
|
|
|
|
|
name: "empty name matcher",
|
|
|
|
|
vs: VectorSelector{
|
|
|
|
|
LabelMatchers: []*labels.Matcher{
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, labels.MetricName, ""),
|
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "x"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `{__name__="",a="x"}`,
|
|
|
|
|
},
|
2021-09-01 03:48:18 -04:00
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
require.Equal(t, tc.expected, tc.vs.String())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-14 15:18:06 -05:00
|
|
|
|
|
|
|
|
func TestBinaryExprUTF8Labels(t *testing.T) {
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
input string
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "UTF-8 labels in on clause",
|
|
|
|
|
input: `foo / on("äää") bar`,
|
|
|
|
|
expected: `foo / on ("äää") bar`,
|
|
|
|
|
},
|
2025-12-15 07:38:12 -05:00
|
|
|
{
|
|
|
|
|
name: "UTF-8 labels in ignoring clause",
|
|
|
|
|
input: `foo / ignoring("üüü") bar`,
|
|
|
|
|
expected: `foo / ignoring ("üüü") bar`,
|
|
|
|
|
},
|
2025-12-14 15:18:06 -05:00
|
|
|
{
|
|
|
|
|
name: "UTF-8 labels in group_left clause",
|
|
|
|
|
input: `foo / on("äää") group_left("ööö") bar`,
|
|
|
|
|
expected: `foo / on ("äää") group_left ("ööö") bar`,
|
|
|
|
|
},
|
2025-12-15 07:38:12 -05:00
|
|
|
{
|
|
|
|
|
name: "UTF-8 labels in group_right clause",
|
|
|
|
|
input: `foo / on("äää") group_right("ööö") bar`,
|
|
|
|
|
expected: `foo / on ("äää") group_right ("ööö") bar`,
|
|
|
|
|
},
|
2025-12-14 15:18:06 -05:00
|
|
|
{
|
|
|
|
|
name: "Mixed legacy and UTF-8 labels",
|
|
|
|
|
input: `foo / on(legacy, "üüü") bar`,
|
|
|
|
|
expected: `foo / on (legacy, "üüü") bar`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Legacy labels only (should not quote)",
|
|
|
|
|
input: `foo / on(job, instance) bar`,
|
|
|
|
|
expected: `foo / on (job, instance) bar`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
expr, err := ParseExpr(tc.input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to parse: %v", err)
|
|
|
|
|
}
|
|
|
|
|
result := expr.String()
|
|
|
|
|
if result != tc.expected {
|
|
|
|
|
t.Errorf("Expected: %s\nGot: %s", tc.expected, result)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|