From 20b4112b3b4ebba355c87aea1a53e3c098a36bd3 Mon Sep 17 00:00:00 2001 From: Andrej Ocenas Date: Fri, 25 Mar 2022 14:03:30 +0100 Subject: [PATCH] Prometheus: Report error when parens are used and switching to visual builder (#46899) --- .../prometheus/querybuilder/parsing.test.ts | 29 +++++++++++++++++++ .../prometheus/querybuilder/parsing.ts | 5 ++++ 2 files changed, 34 insertions(+) diff --git a/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts b/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts index 89643c4b3a6..96582c9b7c1 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts @@ -541,6 +541,35 @@ describe('buildVisualQueryFromString', () => { }, }); }); + + it('reports error on parenthesis', () => { + expect(buildVisualQueryFromString('foo / (bar + baz)')).toEqual({ + errors: [ + { + from: 6, + parentType: 'Expr', + text: '(bar + baz)', + to: 17, + }, + ], + query: { + metric: 'foo', + labels: [], + operations: [], + binaryQueries: [ + { + operator: '/', + query: { + binaryQueries: [{ operator: '+', query: { labels: [], metric: 'baz', operations: [] } }], + metric: 'bar', + labels: [], + operations: [], + }, + }, + ], + }, + }); + }); }); function noErrors(query: PromVisualQuery) { diff --git a/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts b/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts index eb71fb5688b..f53ed7cb316 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts @@ -155,6 +155,11 @@ export function handleExpression(expr: string, node: SyntaxNode, context: Contex } default: { + if (node.name === 'ParenExpr') { + // We don't support parenthesis in the query to group expressions. We just report error but go on with the + // parsing. + context.errors.push(makeError(expr, node)); + } // Any other nodes we just ignore and go to it's children. This should be fine as there are lot's of wrapper // nodes that can be skipped. // TODO: there are probably cases where we will just skip nodes we don't support and we should be able to