Skip to content

Commit

Permalink
[v9.3.x] Loki: Fix wrongly escaped label values when using LabelFilter (
Browse files Browse the repository at this point in the history
grafana#59876)

Loki: Fix wrongly escaped label values when using LabelFilter (grafana#59812)

* change backticks to quotes

* add unescaping to `addFilterAsLabelFilter`

* fix removal of wrong quotes

* change unescape order

(cherry picked from commit 932349b)
  • Loading branch information
svennergr authored and GuaYounesPW committed Feb 8, 2023
1 parent ae7506a commit a5a46a9
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 37 deletions.
26 changes: 25 additions & 1 deletion public/app/plugins/datasource/loki/languageUtils.test.ts
@@ -1,4 +1,4 @@
import { isBytesString } from './languageUtils';
import { escapeLabelValueInExactSelector, isBytesString, unescapeLabelValue } from './languageUtils';

describe('isBytesString', () => {
it('correctly matches bytes string with integers', () => {
Expand All @@ -18,3 +18,27 @@ describe('isBytesString', () => {
expect(isBytesString('1.234')).toBe(false);
});
});

describe('escapeLabelValueInExactSelector', () => {
it.each`
value | escapedValue
${'nothing to escape'} | ${'nothing to escape'}
${'escape quote: "'} | ${'escape quote: \\"'}
${'escape newline: \nend'} | ${'escape newline: \\nend'}
${'escape slash: \\'} | ${'escape slash: \\\\'}
`('when called with $value', ({ value, escapedValue }) => {
expect(escapeLabelValueInExactSelector(value)).toEqual(escapedValue);
});
});

describe('unescapeLabelValueInExactSelector', () => {
it.each`
value | unescapedValue
${'nothing to unescape'} | ${'nothing to unescape'}
${'escape quote: \\"'} | ${'escape quote: "'}
${'escape newline: \\nend'} | ${'escape newline: \nend'}
${'escape slash: \\\\'} | ${'escape slash: \\'}
`('when called with $value', ({ value, unescapedValue }) => {
expect(unescapeLabelValue(value)).toEqual(unescapedValue);
});
});
4 changes: 4 additions & 0 deletions public/app/plugins/datasource/loki/languageUtils.ts
Expand Up @@ -35,6 +35,10 @@ export function escapeLabelValueInExactSelector(labelValue: string): string {
return labelValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"');
}

export function unescapeLabelValue(labelValue: string): string {
return labelValue.replace(/\\n/g, '\n').replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}

export function escapeLabelValueInRegexSelector(labelValue: string): string {
return escapeLabelValueInExactSelector(escapeLokiRegexp(labelValue));
}
Expand Down

0 comments on commit a5a46a9

Please sign in to comment.