From 2509dec0cbd7b433359cd0d4d01d6ba354ca1d37 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 17 Apr 2023 10:27:38 +0200 Subject: [PATCH] Loki: Fix ad hoc filters when used with number and > and < operators (#66579) Loki: Fix ad hoc filters when used with number and >< operators --- .../app/plugins/datasource/loki/modifyQuery.test.ts | 2 ++ public/app/plugins/datasource/loki/modifyQuery.ts | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/loki/modifyQuery.test.ts b/public/app/plugins/datasource/loki/modifyQuery.test.ts index 8b76c465263..608fc610992 100644 --- a/public/app/plugins/datasource/loki/modifyQuery.test.ts +++ b/public/app/plugins/datasource/loki/modifyQuery.test.ts @@ -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 }) => { diff --git a/public/app/plugins/datasource/loki/modifyQuery.ts b/public/app/plugins/datasource/loki/modifyQuery.ts index 85c46d8d9b6..8f8ee3a880c 100644 --- a/public/app/plugins/datasource/loki/modifyQuery.ts +++ b/public/app/plugins/datasource/loki/modifyQuery.ts @@ -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; }