Prometheus: Fix quote stripping in parser (#87675)

* Prometheus: Fix quote stripping in parser

Currently only double quotes are stripped from the end, while single quotes are left. Moreover, double quotes are stripped even when part of the value

* Prometheus: Double escape the string, apply linting fixes for files that I touched
This commit is contained in:
Jake Leahy 2024-06-26 01:39:32 +10:00 committed by GitHub
parent 0ea2d6972f
commit 2bd95b2eb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 1 deletions

View File

@ -746,6 +746,43 @@ describe('buildVisualQueryFromString', () => {
},
});
});
it('strips enclosing quotes', () => {
expect(buildVisualQueryFromString("counters_logins{app='frontend', host=`localhost`}")).toEqual(
noErrors({
metric: 'counters_logins',
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
{
op: '=',
value: 'localhost',
label: 'host',
},
],
operations: [],
})
);
});
it('leaves escaped quotes inside string', () => {
expect(buildVisualQueryFromString('counters_logins{app="fron\\"\\"tend"}')).toEqual(
noErrors({
metric: 'counters_logins',
labels: [
{
op: '=',
value: 'fron\\"\\"tend',
label: 'app',
},
],
operations: [],
})
);
});
});
function noErrors(query: PromVisualQuery) {

View File

@ -205,7 +205,7 @@ function isIntervalVariableError(node: SyntaxNode) {
function getLabel(expr: string, node: SyntaxNode): QueryBuilderLabelFilter {
const label = getString(expr, node.getChild(LabelName));
const op = getString(expr, node.getChild(MatchOp));
const value = getString(expr, node.getChild(StringLiteral)).replace(/"/g, '');
const value = getString(expr, node.getChild(StringLiteral)).replace(/^["'`]|["'`]$/g, '');
return {
label,
op,