From 6ec7434fde1fd6e67eab7d46aedf94d55cd4a7a8 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 5 May 2022 13:54:41 -0400 Subject: [PATCH] Prometheus: make parsing of Infinity variants case-insensitive (#48660) (#48785) (cherry picked from commit 6de77283c682e8baba133250f67453a03fdbf5bf) Co-authored-by: Leon Sorokin --- .../prometheus/result_transformer.test.ts | 39 ++++++++++++++++++- .../prometheus/result_transformer.ts | 17 ++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/result_transformer.test.ts b/public/app/plugins/datasource/prometheus/result_transformer.test.ts index c58b95c514a..4639861c188 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.test.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.test.ts @@ -1,6 +1,6 @@ import { DataFrame, FieldType, DataQueryRequest, DataQueryResponse, MutableDataFrame } from '@grafana/data'; -import { transform, transformV2, transformDFToTable } from './result_transformer'; +import { transform, transformV2, transformDFToTable, parseSampleValue } from './result_transformer'; import { PromQuery } from './types'; jest.mock('@grafana/runtime', () => ({ @@ -33,6 +33,43 @@ const matrixResponse = { }; describe('Prometheus Result Transformer', () => { + describe('parse variants of "+Inf" and "-Inf" strings', () => { + it('+Inf', () => { + expect(parseSampleValue('+Inf')).toEqual(Number.POSITIVE_INFINITY); + }); + it('Inf', () => { + expect(parseSampleValue('Inf')).toEqual(Number.POSITIVE_INFINITY); + }); + it('inf', () => { + expect(parseSampleValue('inf')).toEqual(Number.POSITIVE_INFINITY); + }); + it('+Infinity', () => { + expect(parseSampleValue('+Infinity')).toEqual(Number.POSITIVE_INFINITY); + }); + it('+infinity', () => { + expect(parseSampleValue('+infinity')).toEqual(Number.POSITIVE_INFINITY); + }); + it('infinity', () => { + expect(parseSampleValue('infinity')).toEqual(Number.POSITIVE_INFINITY); + }); + + it('-Inf', () => { + expect(parseSampleValue('-Inf')).toEqual(Number.NEGATIVE_INFINITY); + }); + + it('-inf', () => { + expect(parseSampleValue('-inf')).toEqual(Number.NEGATIVE_INFINITY); + }); + + it('-Infinity', () => { + expect(parseSampleValue('-Infinity')).toEqual(Number.NEGATIVE_INFINITY); + }); + + it('-infinity', () => { + expect(parseSampleValue('-infinity')).toEqual(Number.NEGATIVE_INFINITY); + }); + }); + describe('transformV2', () => { it('results with time_series format should be enriched with preferredVisualisationType', () => { const request = { diff --git a/public/app/plugins/datasource/prometheus/result_transformer.ts b/public/app/plugins/datasource/prometheus/result_transformer.ts index 103f2500605..ced3875776d 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -38,8 +38,8 @@ import { TransformOptions, } from './types'; -const POSITIVE_INFINITY_SAMPLE_VALUE = '+Inf'; -const NEGATIVE_INFINITY_SAMPLE_VALUE = '-Inf'; +// handles case-insensitive Inf, +Inf, -Inf (with optional "inity" suffix) +const INFINITY_SAMPLE_REGEX = /^[+-]?inf(?:inity)?$/i; interface TimeAndValue { [TIME_SERIES_TIME_FIELD_NAME]: number; @@ -611,13 +611,10 @@ function sortSeriesByLabel(s1: DataFrame, s2: DataFrame): number { return 0; } -function parseSampleValue(value: string): number { - switch (value) { - case POSITIVE_INFINITY_SAMPLE_VALUE: - return Number.POSITIVE_INFINITY; - case NEGATIVE_INFINITY_SAMPLE_VALUE: - return Number.NEGATIVE_INFINITY; - default: - return parseFloat(value); +/** @internal */ +export function parseSampleValue(value: string): number { + if (INFINITY_SAMPLE_REGEX.test(value)) { + return value[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; } + return parseFloat(value); }