From 70e4499f83798d7e5bfd41be91bc8898c19c3c5f Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 1 Feb 2023 14:52:46 +0000 Subject: [PATCH] [v9.4.x] Loki: Fix label filter expression treating int as string (#62687) Loki: Fix label filter expression treating int as string (#62496) * fix: label filter expression treats int as string * refactor: labelFilterRenderer * test: add tests to labelFilterRenderer * refactor: use backticks based on the operator not the value * test: labelFilterRenderer uses the correct value type based on the operator * test: use it.every rather than having multiple repetitive tests (cherry picked from commit 42f8f5a9ea906eddb29ab15365b73ef22a224e50) Co-authored-by: Gareth Dawson --- .../loki/querybuilder/operationUtils.test.ts | 30 ++++++++++++++++++- .../loki/querybuilder/operationUtils.ts | 4 ++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/loki/querybuilder/operationUtils.test.ts b/public/app/plugins/datasource/loki/querybuilder/operationUtils.test.ts index ba4e6b8c569..bf6ccdcb17b 100644 --- a/public/app/plugins/datasource/loki/querybuilder/operationUtils.test.ts +++ b/public/app/plugins/datasource/loki/querybuilder/operationUtils.test.ts @@ -1,6 +1,11 @@ import { QueryBuilderOperationDef } from '../../prometheus/querybuilder/shared/types'; -import { createRangeOperation, createRangeOperationWithGrouping, getLineFilterRenderer } from './operationUtils'; +import { + createRangeOperation, + createRangeOperationWithGrouping, + getLineFilterRenderer, + labelFilterRenderer, +} from './operationUtils'; import { LokiVisualQueryOperationCategory } from './types'; describe('createRangeOperation', () => { @@ -156,3 +161,26 @@ describe('getLineFilterRenderer', () => { ); }); }); + +describe('labelFilterRenderer', () => { + const MOCK_MODEL = { id: '__label_filter', params: ['label', '', 'value'] }; + const MOCK_DEF = undefined as unknown as QueryBuilderOperationDef; + const MOCK_INNER_EXPR = '{job="grafana"}'; + + it.each` + operator | type | expected + ${'='} | ${'string'} | ${'`value`'} + ${'!='} | ${'string'} | ${'`value`'} + ${'=~'} | ${'string'} | ${'`value`'} + ${'!~'} | ${'string'} | ${'`value`'} + ${'>'} | ${'number'} | ${'value'} + ${'>='} | ${'number'} | ${'value'} + ${'<'} | ${'number'} | ${'value'} + ${'<='} | ${'number'} | ${'value'} + `("value should be of type '$type' when operator is: $operator", ({ operator, expected }) => { + MOCK_MODEL.params[1] = operator; + expect(labelFilterRenderer(MOCK_MODEL, MOCK_DEF, MOCK_INNER_EXPR)).toBe( + `{job="grafana"} | label ${operator} ${expected}` + ); + }); +}); diff --git a/public/app/plugins/datasource/loki/querybuilder/operationUtils.ts b/public/app/plugins/datasource/loki/querybuilder/operationUtils.ts index 7ee156715af..3a5c486f2b7 100644 --- a/public/app/plugins/datasource/loki/querybuilder/operationUtils.ts +++ b/public/app/plugins/datasource/loki/querybuilder/operationUtils.ts @@ -148,7 +148,9 @@ function operationWithRangeVectorRenderer( } export function labelFilterRenderer(model: QueryBuilderOperation, def: QueryBuilderOperationDef, innerExpr: string) { - if (model.params[1] === '<' || model.params[1] === '>') { + const integerOperators = ['<', '<=', '>', '>=']; + + if (integerOperators.includes(String(model.params[1]))) { return `${innerExpr} | ${model.params[0]} ${model.params[1]} ${model.params[2]}`; }