From 7af095399587e5266cf5b0a6f5bae027c7f40f96 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Fri, 4 Nov 2022 15:58:08 +0100 Subject: [PATCH] fix(prometheus scaler): Detect Inf before casting float to int (#3762) * fix(prometheus scaler): Detect Inf before casting float to int Signed-off-by: Jorge Turrado * Improve the log message Signed-off-by: Jorge Turrado Signed-off-by: Jorge Turrado --- CHANGELOG.md | 1 + pkg/scalers/prometheus_scaler.go | 10 ++++++++ pkg/scalers/prometheus_scaler_test.go | 36 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 036febe38e0..4ae27c0482f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio - **General:** Provide patch for CVE-2022-3172 vulnerability ([#3690](https://github.com/kedacore/keda/issues/3690)) - **General:** Respect optional parameter inside envs for ScaledJobs ([#3568](https://github.com/kedacore/keda/issues/3568)) - **Azure Blob Scaler** Store forgotten logger ([#3811](https://github.com/kedacore/keda/issues/3811)) +- **Prometheus Scaler:** Treat Inf the same as Null result ([#3644](https://github.com/kedacore/keda/issues/3644)) ### Deprecations diff --git a/pkg/scalers/prometheus_scaler.go b/pkg/scalers/prometheus_scaler.go index e8450331d91..4a64c27aca2 100644 --- a/pkg/scalers/prometheus_scaler.go +++ b/pkg/scalers/prometheus_scaler.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "math" "net/http" url_pkg "net/url" "strconv" @@ -297,6 +298,15 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error } } + if math.IsInf(v, 0) { + if s.metadata.ignoreNullValues { + return 0, nil + } + err := fmt.Errorf("promtheus query returns %f", v) + s.logger.Error(err, "Error converting prometheus value") + return -1, err + } + return v, nil } diff --git a/pkg/scalers/prometheus_scaler_test.go b/pkg/scalers/prometheus_scaler_test.go index 2ac606c4615..674622cac7a 100644 --- a/pkg/scalers/prometheus_scaler_test.go +++ b/pkg/scalers/prometheus_scaler_test.go @@ -228,6 +228,42 @@ var testPromQueryResult = []prometheusQromQueryResultTestData{ ignoreNullValues: true, unsafeSsl: true, }, + { + name: "+Inf", + bodyStr: `{"data":{"result":[{"value": ["1", "+Inf"]}]}}`, + responseStatus: http.StatusOK, + expectedValue: 0, + isError: false, + ignoreNullValues: true, + unsafeSsl: true, + }, + { + name: "+Inf but shouldn't ignore ", + bodyStr: `{"data":{"result":[{"value": ["1", "+Inf"]}]}}`, + responseStatus: http.StatusOK, + expectedValue: -1, + isError: true, + ignoreNullValues: false, + unsafeSsl: true, + }, + { + name: "-Inf", + bodyStr: `{"data":{"result":[{"value": ["1", "-Inf"]}]}}`, + responseStatus: http.StatusOK, + expectedValue: 0, + isError: false, + ignoreNullValues: true, + unsafeSsl: true, + }, + { + name: "-Inf but shouldn't ignore ", + bodyStr: `{"data":{"result":[{"value": ["1", "-Inf"]}]}}`, + responseStatus: http.StatusOK, + expectedValue: -1, + isError: true, + ignoreNullValues: false, + unsafeSsl: true, + }, } func TestPrometheusScalerExecutePromQuery(t *testing.T) {