Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(/\\\\/g, '\\').replace(/\\n/g, '\n').replace(/\\"/g, '"');
Fixed Show fixed Hide fixed
}

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