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