| 1 | package metricsqlscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/ |
| 2 | |
| 3 | import ( |
| 4 | "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.24.2 testing/" |
| 6 | ) |
| 7 | |
| 8 | func TestPrettifyErrorscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/TestPrettifyError().(t *testingscip-go gomod github.com/golang/go/src go1.24.2 testing/.Tscip-go gomod github.com/golang/go/src go1.24.2 testing/T#) { |
| 9 | f := func(s string) { |
| 10 | t.Helperscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Helper().() |
| 11 | |
| 12 | result, err := Prettifyscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Prettify().(s) |
| 13 | if err == nil { |
| 14 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("expecting non-nil error") |
| 15 | } |
| 16 | if result != "" { |
| 17 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("expecting empty result; got %q", result) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | f(`foo{`) |
| 22 | f(`invalid query`) |
| 23 | } |
| 24 | |
| 25 | func TestPrettifyOkParseErrorscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/TestPrettifyOkParseError().(t *testingscip-go gomod github.com/golang/go/src go1.24.2 testing/.Tscip-go gomod github.com/golang/go/src go1.24.2 testing/T#) { |
| 26 | f := func(s, wantErr string) { |
| 27 | t.Helperscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Helper().() |
| 28 | |
| 29 | // check prettify ok |
| 30 | // it should not check query for semantic errors |
| 31 | result, err := Prettifyscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Prettify().(s) |
| 32 | if err != nil { |
| 33 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected error when prettifying: %q: %s", s, err) |
| 34 | } |
| 35 | |
| 36 | // Verify that the prettified result is failed to parse due to semantic error |
| 37 | _, err = Parsescip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Parse().(result) |
| 38 | if err == nil { |
| 39 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("expecting non-nil error") |
| 40 | } |
| 41 | errStr := err.Error() |
| 42 | if !stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Containsscip-go gomod github.com/golang/go/src go1.24.2 strings/Contains().(errStr, wantErr) { |
| 43 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected error string:\ngot:\n%q\nwant:\n%q", err, wantErr) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // unknown duration |
| 48 | f(`sum(rate(m{a="b"}[BAZ]))`, `cannot find WITH template for "BAZ"`) |
| 49 | f(`sum(rate(m{a="b"}[5m:BAZ]))`, `cannot find WITH template for "BAZ"`) |
| 50 | f(`sum(rate(m{a="b"}[BAZ:5s]))`, `cannot find WITH template for "BAZ"`) |
| 51 | f(`sum(rate(m{a="b"}[5m] offset BAZ))`, `cannot find WITH template for "BAZ"`) |
| 52 | |
| 53 | // unknown function |
| 54 | f(`WITH (ru(freev, maxv) = bad_func1(maxv - bad_func_2(freev, 0), 0) / clamp_min(maxv, 0) * 100) ru(1,2)`, `unsupported function "bad_func_2"`) |
| 55 | |
| 56 | // composite strings |
| 57 | f(`with (f(x)=foo(m{a="z"+x})) f("abc")`, `unsupported function "foo"`) |
| 58 | f(`with (f(x) = "a" + x + "y" + "z") f(abc)`, `"x" must be string expression; got "abc"`) |
| 59 | } |
| 60 | |
| 61 | func TestPrettifySuccessscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/TestPrettifySuccess().(t *testingscip-go gomod github.com/golang/go/src go1.24.2 testing/.Tscip-go gomod github.com/golang/go/src go1.24.2 testing/T#) { |
| 62 | another := func(s, resultExpected string) { |
| 63 | t.Helperscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Helper().() |
| 64 | |
| 65 | result, err := Prettifyscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Prettify().(s) |
| 66 | if err != nil { |
| 67 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected error when parsing %q: %s", s, err) |
| 68 | } |
| 69 | if result != resultExpected { |
| 70 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected query after prettifying;\ngot\n%s\nwant\n%s", result, resultExpected) |
| 71 | } |
| 72 | |
| 73 | // Verify that prettifying of the result gives the same result |
| 74 | result2, err := Prettifyscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Prettify().(result) |
| 75 | if err != nil { |
| 76 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected error when parsing prettified %q: %s", s, err) |
| 77 | } |
| 78 | if result2 != result { |
| 79 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected result after prettifying already prettified result;\ngot\n%s\nwant\n%s", result2, result) |
| 80 | } |
| 81 | |
| 82 | // Verify that the prettified result is successfully parsed into the same string as the original query |
| 83 | e, err := Parsescip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Parse().(s) |
| 84 | if err != nil { |
| 85 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("cannot parse original string: %s", err) |
| 86 | } |
| 87 | eResult, err := Parsescip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Parse().(result) |
| 88 | if err != nil { |
| 89 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("cannot parse prettified result: %s", err) |
| 90 | } |
| 91 | sExpected := e.AppendStringscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Expr#AppendString.(nil) |
| 92 | sGot := eResult.AppendStringscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Expr#AppendString.(nil) |
| 93 | if string(sExpected) != string(sGot) { |
| 94 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Fatalf().("unexpected prettified string after parsing;\ngot\n%s\nwant\n%s", sGot, sExpected) |
| 95 | } |
| 96 | } |
| 97 | same := func(s string) { |
| 98 | t.Helperscip-go gomod github.com/golang/go/src go1.24.2 testing/common#Helper().() |
| 99 | another(s, s) |
| 100 | } |
| 101 | |
| 102 | // Verify that short queries remain single-line |
| 103 | same(`foo`) |
| 104 | another(`{"foo"}`, "foo") |
| 105 | another(`{"3foo"}`, `{"3foo"}`) |
| 106 | another(`{"foo", bar="baz"}`, `foo{bar="baz"}`) |
| 107 | another(`{"foo", "bar"="baz"}`, `foo{bar="baz"}`) |
| 108 | same(`foo{bar="baz"}`) |
| 109 | same(`foo{"3bar"="baz"}`) |
| 110 | another(`foo{"bar3"="baz"}`, `foo{bar3="baz"}`) |
| 111 | same(`"metr\"ic"`) |
| 112 | same(`'metr"ic'`) |
| 113 | same(`{foo="bar","la\"bel"="val"}`) |
| 114 | same(`{"metr\\ic"}`) |
| 115 | same(`{"3foo", bar="baz"}`) |
| 116 | same(`foo{bar="baz",x="y" or q="w",r="t"}`) |
| 117 | same(`foo{bar="baz"} + rate(x{y="x"}[5m] offset 1h)`) |
| 118 | |
| 119 | // Verify that long escaped metric names are preserved when they are the only selector |
| 120 | same(`{"3foobar_baz_namespace_pod_name_container_name_container_cpu_usage_seconds_total_sum_rate"}`) |
| 121 | |
| 122 | // Verify that empty label filters aren't added to long metric names |
| 123 | same(`foobar_baz:namespace_pod_name_container_name:container_cpu_usage_seconds_total:sum_rate`) |
| 124 | another(`foobar_baz:namespace_pod_name_container_name:container_cpu_usage_seconds_total:sum_rate{}`, |
| 125 | `foobar_baz:namespace_pod_name_container_name:container_cpu_usage_seconds_total:sum_rate`) |
| 126 | |
| 127 | // Verify that long label filters are split into multiple lines |
| 128 | another(`process_cpu_seconds_total{foo="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk"}`, |
| 129 | `process_cpu_seconds_total{ |
| 130 | foo="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk" |
| 131 | }`) |
| 132 | another(`process_cpu_seconds_total{"3foo"="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk"}`, |
| 133 | `process_cpu_seconds_total{ |
| 134 | "3foo"="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk" |
| 135 | }`) |
| 136 | another(`{"process_cpu_seconds_total", "foo"="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk"}`, |
| 137 | `process_cpu_seconds_total{ |
| 138 | foo="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk" |
| 139 | }`) |
| 140 | another(`{"3process_cpu_seconds_total", "foo"="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk"}`, |
| 141 | `{ |
| 142 | "3process_cpu_seconds_total", |
| 143 | foo="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk" |
| 144 | }`) |
| 145 | another(`process_cpu_seconds_total{foo="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk",very_long_label_aaaaaaaaaaaaaaa="fdsfdsffdsfs"}`, |
| 146 | `process_cpu_seconds_total{ |
| 147 | foo="bar", |
| 148 | xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk", |
| 149 | very_long_label_aaaaaaaaaaaaaaa="fdsfdsffdsfs" |
| 150 | }`) |
| 151 | another(`{foo="bar",xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk",very_long_label_aaaaaaaaaaaaaaa="fdsfdsffdsfs"}`, |
| 152 | `{ |
| 153 | foo="bar", |
| 154 | xjljljlkjopiwererrewre="asdfdsfdsfsdfdsfjkljlk", |
| 155 | very_long_label_aaaaaaaaaaaaaaa="fdsfdsffdsfs" |
| 156 | }`) |
| 157 | another(`process_cpu_seconds_total{instance="foobar-baz",job="job1234567" or instance="lkjlkjlkjlkjlkjlkjlkjlkjlkjlk",job="lkjljlkjalkadsfdsffdsfdsfd", |
| 158 | some_very_long_label="very_very_very_long_value_12397787_dfdfdfsds_dsffdfsf"}`, |
| 159 | `process_cpu_seconds_total{ |
| 160 | instance="foobar-baz",job="job1234567" |
| 161 | or |
| 162 | instance="lkjlkjlkjlkjlkjlkjlkjlkjlkjlk", |
| 163 | job="lkjljlkjalkadsfdsffdsfdsfd", |
| 164 | some_very_long_label="very_very_very_long_value_12397787_dfdfdfsds_dsffdfsf" |
| 165 | }`) |
| 166 | |
| 167 | // Verify that long binary operations are split into multiple lines |
| 168 | another(`(sum(rate(process_cpu_seconds_total{instance="foo",job="bar"}[5m] offset 1h @ start())) by (x) / on(x) group_right(y) prefix "x" sum(rate(node_cpu_seconds_total{mode!="idle"}[5m]) keep_metric_names)) keep_metric_names`, |
| 169 | `( |
| 170 | sum( |
| 171 | rate( |
| 172 | process_cpu_seconds_total{instance="foo",job="bar"}[5m] offset 1h @ start() |
| 173 | ) |
| 174 | ) by(x) |
| 175 | / on(x) group_right(y) prefix "x" |
| 176 | sum(rate(node_cpu_seconds_total{mode!="idle"}[5m]) keep_metric_names) |
| 177 | ) keep_metric_names`) |
| 178 | |
| 179 | another(`(sum(rate({"3process_cpu_seconds_total", instance="foo",job="bar"}[5m] offset 1h @ start())) by (x) / on(x) group_right(y) prefix "x" sum(rate(node_cpu_seconds_total{mode!="idle"}[5m]) keep_metric_names)) keep_metric_names`, |
| 180 | `( |
| 181 | sum( |
| 182 | rate( |
| 183 | {"3process_cpu_seconds_total", instance="foo",job="bar"}[5m] offset 1h @ start() |
| 184 | ) |
| 185 | ) by(x) |
| 186 | / on(x) group_right(y) prefix "x" |
| 187 | sum(rate(node_cpu_seconds_total{mode!="idle"}[5m]) keep_metric_names) |
| 188 | ) keep_metric_names`) |
| 189 | another(`process_cpu_seconds_total{aaaaaaaaaaaaaaaaaa="bbbbbb"} offset 5m + (rate(xxxxxxxxxxxxxxxx{yyyyyyyy="aaaaaaa"}) keep_metric_names)`, |
| 190 | `(process_cpu_seconds_total{aaaaaaaaaaaaaaaaaa="bbbbbb"} offset 5m) |
| 191 | + |
| 192 | (rate(xxxxxxxxxxxxxxxx{yyyyyyyy="aaaaaaa"}) keep_metric_names)`) |
| 193 | another(`process_cpu_seconds_total{aaaaaaaaaaaaaaaaaa="bbbbbb",cccccccccccccccccccccc!~"ddddddddddddddddddddddd"} offset 5m + (rate(xxxxxxxxxxxxxxxx{yyyyyyyy="aaaaaaa"}) keep_metric_names)`, |
| 194 | `( |
| 195 | process_cpu_seconds_total{ |
| 196 | aaaaaaaaaaaaaaaaaa="bbbbbb", |
| 197 | cccccccccccccccccccccc!~"ddddddddddddddddddddddd" |
| 198 | } offset 5m |
| 199 | ) |
| 200 | + |
| 201 | (rate(xxxxxxxxxxxxxxxx{yyyyyyyy="aaaaaaa"}) keep_metric_names)`) |
| 202 | |
| 203 | // Verify that long rollup expression is properly split into multiple lines |
| 204 | another(`process_cpu_seconds_total{foo="bar",aaaaaaaaaaaaaaaaaaaaaaaa="bbbbbbbbbbbbbbbbbbbb",c="dddddddddddd"}[5m:3s] offset 5h3m @ 12345`, |
| 205 | `process_cpu_seconds_total{ |
| 206 | foo="bar",aaaaaaaaaaaaaaaaaaaaaaaa="bbbbbbbbbbbbbbbbbbbb",c="dddddddddddd" |
| 207 | }[5m:3s] offset 5h3m @ 12345`) |
| 208 | another(`process_cpu_seconds_total{foo="bar",aaaaaaaaaaaaaaaaaaaaaaaa="bbbbbbbbbbbbbbbbbbbb",ccccccccccccccc="dddddddddddd"}[5m:3s] offset 5h3m @ 12345`, |
| 209 | `process_cpu_seconds_total{ |
| 210 | foo="bar", |
| 211 | aaaaaaaaaaaaaaaaaaaaaaaa="bbbbbbbbbbbbbbbbbbbb", |
| 212 | ccccccccccccccc="dddddddddddd" |
| 213 | }[5m:3s] offset 5h3m @ 12345`) |
| 214 | |
| 215 | // Verify that aggregate expression is properly split into multiple lines |
| 216 | another(`sum without(x,y) (process_cpu_seconds_total{foo="bar",aaaaaaaaaaaaaaaaaaaaaaaa="bbbbbbbbbbbbbbbbbbbb",c="dddddddddddd"}[5m:3s] offset 5h3m @ 12345)`, |
| 217 | `sum( |
| 218 | process_cpu_seconds_total{ |
| 219 | foo="bar",aaaaaaaaaaaaaaaaaaaaaaaa="bbbbbbbbbbbbbbbbbbbb",c="dddddddddddd" |
| 220 | }[5m:3s] offset 5h3m @ 12345 |
| 221 | ) without(x,y)`) |
| 222 | |
| 223 | // Verify that an ordinary function args are split into multiple lines |
| 224 | another(`clamp_min(process_cpu_seconds_total{aaaaaaaaaaaaaaaaaaaaaaaaa="bbbb",cccccc="dddd",ppppppppppppppppppppppppp=~"xxxxxxx"}, 123, "456")`, |
| 225 | `clamp_min( |
| 226 | process_cpu_seconds_total{ |
| 227 | aaaaaaaaaaaaaaaaaaaaaaaaa="bbbb", |
| 228 | cccccc="dddd", |
| 229 | ppppppppppppppppppppppppp=~"xxxxxxx" |
| 230 | }, |
| 231 | 123, |
| 232 | "456" |
| 233 | )`) |
| 234 | |
| 235 | // Verify how prettifier works with very long string |
| 236 | same(`"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`) |
| 237 | |
| 238 | // Verify short WITH expressions |
| 239 | same(`WITH (x = {a="b"}) x`) |
| 240 | same(`WITH (x = {a="b"}) {x,y="z"}`) |
| 241 | same(`WITH (x = {a="b"}) {y="z",x}`) |
| 242 | same(`WITH (x = {a="b"}) x{y="z"}`) |
| 243 | same(`WITH (f(s) = {x="foo"+s+"bar"}) f("x")`) |
| 244 | same(`WITH (x = sum(rate({a="b"}[1m]))) x`) |
| 245 | |
| 246 | // Verify long WITH expressions |
| 247 | another(`with (f(a)=foo{a,qw="ert"}, x = process_cpu_seconds_total{aaaaaaaaaaaaaaaaaaaaaaaaa="bbbb",cccccc="dddd",ppppppppppppppppppppppppp=~"xxxxxxx"}, y=f({ab="cde"}) + efg{h="j"}) x + sum(y)`, |
| 248 | `WITH ( |
| 249 | f(a) = foo{a,qw="ert"}, |
| 250 | x = ( |
| 251 | process_cpu_seconds_total{ |
| 252 | aaaaaaaaaaaaaaaaaaaaaaaaa="bbbb", |
| 253 | cccccc="dddd", |
| 254 | ppppppppppppppppppppppppp=~"xxxxxxx" |
| 255 | } |
| 256 | ), |
| 257 | y = f({ab="cde"}) + efg{h="j"}, |
| 258 | ) |
| 259 | x + sum(y)`) |
| 260 | |
| 261 | // Embedded WITH expressions |
| 262 | another(`with ( x = (a{b="c"}) + (with (q=we{rt="z"}) q)) ((abc) / x) + (with(rt=234+234) 2*rt+ poasdfklkjlkjfdsfjklfdfdsfdsfddfsfd)`, |
| 263 | `WITH ( |
| 264 | x = a{b="c"} + WITH (q = we{rt="z"}) q, |
| 265 | ) |
| 266 | (abc / x) + WITH (rt = 234 + 234) (2 * rt) + poasdfklkjlkjfdsfjklfdfdsfdsfddfsfd`) |
| 267 | |
| 268 | // duration replacement in WITH expression |
| 269 | another(`WITH(BAR=1m,x(BAZ)=sum(rate({a="b"}[BAR:BAZ])) offset BAR) x`, `WITH (BAR = 1m, x(BAZ) = sum(rate({a="b"}[BAR:BAZ])) offset BAR) x`) |
| 270 | |
| 271 | // function replacement in WITH expression |
| 272 | same(`WITH (f(x) = abc{x}, z = f({foo="bar"})) z`) |
| 273 | |
| 274 | // Test for issue #60: __name__ with regex operator should not be treated as metric name |
| 275 | same(`count({__name__=~"node_arp_entries|node_cooling_device_cur_state"})`) |
| 276 | same(`{__name__=~"foo|bar"}`) |
| 277 | same(`{__name__!~"foo|bar"}`) |
| 278 | same(`sum({__name__=~"metric.*"})`) |
| 279 | |
| 280 | // Verify that exact match __name__ still works |
| 281 | another(`{__name__="foo"}`, `foo`) |
| 282 | another(`{__name__="foo",bar="baz"}`, `foo{bar="baz"}`) |
| 283 | |
| 284 | another(`10 - (3 + 3 + 4)`, `10 - ((3 + 3) + 4)`) |
| 285 | } |