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
pull/89708/head
Jake Leahy 11 months ago committed by GitHub
parent 0ea2d6972f
commit 2bd95b2eb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      packages/grafana-prometheus/src/querybuilder/parsing.test.ts
  2. 2
      packages/grafana-prometheus/src/querybuilder/parsing.ts

@ -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) {

@ -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,

Loading…
Cancel
Save