Prometheus: Report error when parens are used and switching to visual builder (#46899)

This commit is contained in:
Andrej Ocenas
2022-03-25 14:03:30 +01:00
committed by GitHub
parent 1c648cb52c
commit 20b4112b3b
2 changed files with 34 additions and 0 deletions
@@ -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) {
@@ -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