Loki: Fix ad hoc filters when used with number and > and < operators (#66579)

Loki: Fix ad hoc filters when used with number and >< operators
This commit is contained in:
Ivana Huckova 2023-04-17 10:27:38 +02:00 committed by GitHub
parent 075070db3e
commit 2509dec0cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -52,6 +52,8 @@ describe('addLabelToQuery()', () => {
${'{foo="bar"} | logfmt'} | ${'query with parser with an other escaped value'} | ${'bar'} | ${'='} | ${'baz\\\\'} | ${'{foo="bar"} | logfmt | bar=`baz\\`'}
${'{foo="bar"} | logfmt'} | ${'query with parser with escaped value and regex operator'} | ${'bar'} | ${'~='} | ${'\\"baz\\"'} | ${'{foo="bar"} | logfmt | bar~=`"baz"`'}
${'{foo="bar"} | logfmt'} | ${'query with parser with escaped value and regex operator'} | ${'bar'} | ${'~='} | ${'\\"baz\\"'} | ${'{foo="bar"} | logfmt | bar~=`"baz"`'}
${'{foo="bar"} | logfmt'} | ${'query with parser, > operator and number value'} | ${'bar'} | ${'>'} | ${'5'} | ${'{foo="bar"} | logfmt | bar>5'}
${'{foo="bar"} | logfmt'} | ${'query with parser, < operator and non-number value'} | ${'bar'} | ${'<'} | ${'5KiB'} | ${'{foo="bar"} | logfmt | bar<`5KiB`'}
`(
'should add label to query: $query, description: $description',
({ query, description, label, operator, value, expectedResult }) => {

View File

@ -327,9 +327,16 @@ export function addFilterAsLabelFilter(
const start = query.substring(prev, match.to);
const end = isLast ? query.substring(match.to) : '';
// we now unescape all escaped values again, because we are using backticks which can handle those cases.
// we also don't care about the operator here, because we need to unescape for both, regex and equal.
const labelFilter = ` | ${filter.label}${filter.op}\`${unescapeLabelValue(filter.value)}\``;
let labelFilter = '';
// For < and >, if the value is number, we don't add quotes around it and use it as number
if (!Number.isNaN(Number(filter.value)) && (filter.op === '<' || filter.op === '>')) {
labelFilter = ` | ${filter.label}${filter.op}${Number(filter.value)}`;
} else {
// we now unescape all escaped values again, because we are using backticks which can handle those cases.
// we also don't care about the operator here, because we need to unescape for both, regex and equal.
labelFilter = ` | ${filter.label}${filter.op}\`${unescapeLabelValue(filter.value)}\``;
}
newQuery += start + labelFilter + end;
prev = match.to;
}