Skip to content

Commit

Permalink
fix(prometheus scaler): Detect Inf before casting float to int (#3762)
Browse files Browse the repository at this point in the history
* fix(prometheus scaler): Detect Inf before casting float to int

Signed-off-by: Jorge Turrado <[email protected]>

* Improve the log message

Signed-off-by: Jorge Turrado <[email protected]>

Signed-off-by: Jorge Turrado <[email protected]>
  • Loading branch information
JorTurFer authored Nov 4, 2022
1 parent 6195641 commit 7af0953
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions pkg/scalers/prometheus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
url_pkg "net/url"
"strconv"
Expand Down Expand Up @@ -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
}

Expand Down
36 changes: 36 additions & 0 deletions pkg/scalers/prometheus_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7af0953

Please sign in to comment.