Skip to content

Commit

Permalink
Prometheus: Fix quote stripping in parser
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ire4ever1190 committed May 16, 2024
1 parent 4c2c717 commit 22f9328
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions packages/grafana-prometheus/src/querybuilder/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/grafana-prometheus/src/querybuilder/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 22f9328

Please sign in to comment.