mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
075070db3e
commit
2509dec0cb
@ -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 }) => {
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user