2015-06-12 07:42:36 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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 16:23:44 +00:00
|
|
|
package parser
|
2015-06-12 07:42:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2024-08-01 14:07:08 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-22 08:19:38 +00:00
|
|
|
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2015-06-12 07:42:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExprString(t *testing.T) {
|
|
|
|
// 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 17:38:26 +00:00
|
|
|
{
|
2018-01-22 10:14:59 +00:00
|
|
|
in: `sum by() (task:errors:rate10s{job="s"})`,
|
2016-10-19 17:38:26 +00:00
|
|
|
out: `sum(task:errors:rate10s{job="s"})`,
|
|
|
|
},
|
2015-06-12 07:42:36 +00: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 22:09:56 +00:00
|
|
|
in: `sum by(code) (task:errors:rate10s{job="s"})`,
|
|
|
|
out: `sum by (code) (task:errors:rate10s{job="s"})`,
|
2015-06-12 07:42:36 +00:00
|
|
|
},
|
2018-01-21 21:22:55 +00: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 22:09:56 +00:00
|
|
|
in: `sum without() (task:errors:rate10s{job="s"})`,
|
|
|
|
out: `sum without () (task:errors:rate10s{job="s"})`,
|
2018-01-21 21:22:55 +00:00
|
|
|
},
|
2016-02-07 18:03:16 +00: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 22:09:56 +00:00
|
|
|
in: `sum without(instance) (task:errors:rate10s{job="s"})`,
|
|
|
|
out: `sum without (instance) (task:errors:rate10s{job="s"})`,
|
2016-02-07 18:03:16 +00:00
|
|
|
},
|
2024-08-01 14:07:08 +00: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 12:10:42 +00:00
|
|
|
{
|
|
|
|
in: `topk(5, task:errors:rate10s{job="s"})`,
|
|
|
|
},
|
2016-07-05 16:12:19 +00:00
|
|
|
{
|
|
|
|
in: `count_values("value", task:errors:rate10s{job="s"})`,
|
|
|
|
},
|
2016-10-19 17:38:26 +00: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 22:09:56 +00:00
|
|
|
in: `a - on() c`,
|
|
|
|
out: `a - on () c`,
|
2016-10-19 17:38:26 +00:00
|
|
|
},
|
2016-04-21 10:45:06 +00: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 22:09:56 +00:00
|
|
|
in: `a - on(b) c`,
|
|
|
|
out: `a - on (b) c`,
|
2016-04-21 10:45:06 +00:00
|
|
|
},
|
2016-05-28 17:45:35 +00: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 22:09:56 +00:00
|
|
|
in: `a - on(b) group_left(x) c`,
|
|
|
|
out: `a - on (b) group_left (x) c`,
|
2016-05-28 17:45:35 +00: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 22:09:56 +00:00
|
|
|
in: `a - on(b) group_left(x, y) c`,
|
|
|
|
out: `a - on (b) group_left (x, y) c`,
|
2016-05-28 17:45:35 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-22 10:14:59 +00: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 22:09:56 +00:00
|
|
|
out: `a - on (b) group_left () c`,
|
2016-11-04 11:06:07 +00: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 22:09:56 +00:00
|
|
|
in: `a - on(b) group_left() (c)`,
|
|
|
|
out: `a - on (b) group_left () (c)`,
|
2016-05-28 17:45:35 +00:00
|
|
|
},
|
2016-04-21 10:45:06 +00: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 22:09:56 +00:00
|
|
|
in: `a - ignoring(b) c`,
|
|
|
|
out: `a - ignoring (b) c`,
|
2016-04-21 10:45:06 +00:00
|
|
|
},
|
2016-10-19 17:38:26 +00:00
|
|
|
{
|
2018-01-22 10:14:59 +00:00
|
|
|
in: `a - ignoring() c`,
|
2016-10-19 17:38:26 +00:00
|
|
|
out: `a - c`,
|
|
|
|
},
|
2015-09-09 23:37:05 +00:00
|
|
|
{
|
2018-01-22 10:14:59 +00:00
|
|
|
in: `up > bool 0`,
|
2015-09-09 23:37:05 +00:00
|
|
|
},
|
2015-11-15 09:26:38 +00:00
|
|
|
{
|
2018-01-22 10:14:59 +00:00
|
|
|
in: `a offset 1m`,
|
2015-11-15 09:26:38 +00:00
|
|
|
},
|
2021-02-24 02:16:28 +00:00
|
|
|
{
|
|
|
|
in: `a offset -7m`,
|
|
|
|
},
|
2015-11-15 09:26:38 +00:00
|
|
|
{
|
2018-01-22 10:14:59 +00:00
|
|
|
in: `a{c="d"}[5m] offset 1m`,
|
2015-11-15 09:26:38 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-22 10:14:59 +00:00
|
|
|
in: `a[5m] offset 1m`,
|
2015-11-15 09:26:38 +00:00
|
|
|
},
|
2021-02-24 02:16:28 +00:00
|
|
|
{
|
|
|
|
in: `a[12m] offset -3m`,
|
|
|
|
},
|
2019-11-26 06:45:51 +00:00
|
|
|
{
|
|
|
|
in: `a[1h:5m] offset 1m`,
|
|
|
|
},
|
2019-05-10 23:46:15 +00:00
|
|
|
{
|
|
|
|
in: `{__name__="a"}`,
|
|
|
|
},
|
2020-07-24 10:21:42 +00:00
|
|
|
{
|
|
|
|
in: `a{b!="c"}[1m]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a{b=~"c"}[1m]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a{b!~"c"}[1m]`,
|
|
|
|
},
|
2021-02-09 16:03:16 +00: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 09:51:08 +00:00
|
|
|
{
|
|
|
|
in: `{__name__="",a="x"}`,
|
|
|
|
},
|
2024-05-08 14:24:58 +00:00
|
|
|
{
|
|
|
|
in: `{"a.b"="c"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `{"0"="1"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `{"_0"="1"}`,
|
|
|
|
out: `{_0="1"}`,
|
|
|
|
},
|
2024-06-13 16:46:35 +00:00
|
|
|
{
|
|
|
|
in: `{""="0"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "{``=\"0\"}",
|
|
|
|
out: `{""="0"}`,
|
|
|
|
},
|
2015-06-12 07:42:36 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 14:07:08 +00:00
|
|
|
model.NameValidationScheme = model.UTF8Validation
|
|
|
|
|
2015-06-12 07:42:36 +00:00
|
|
|
for _, test := range inputs {
|
|
|
|
expr, err := ParseExpr(test.in)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2015-06-12 07:42:36 +00:00
|
|
|
exp := test.in
|
|
|
|
if test.out != "" {
|
|
|
|
exp = test.out
|
|
|
|
}
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, exp, expr.String())
|
2015-06-12 07:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 07:48:18 +00: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 09:51:08 +00: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 07:48:18 +00:00
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.expected, tc.vs.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|