diff --git a/CHANGELOG.md b/CHANGELOG.md index 4440844cb69..f0e87f6fb6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - **Azure Queue:** Don't call Azure queue GetProperties API unnecessarily ([#2613](https://github.com/kedacore/keda/pull/2613)) - **Datadog Scaler:** Validate query to contain `{` to prevent panic on invalid query ([#2625](https://github.com/kedacore/keda/issues/2625)) - **Kafka Scaler** Make "disable" a valid value for tls auth parameter ([#2608](https://github.com/kedacore/keda/issues/2608)) +- **Metric API Scaler:** Improve error handling on not-ok response ([#2317](https://github.com/kedacore/keda/issues/2317)) - **RabbitMQ Scaler:** Include `vhost` for RabbitMQ when retrieving queue info with `useRegex` ([#2498](https://github.com/kedacore/keda/issues/2498)) ### Breaking Changes diff --git a/pkg/scalers/metrics_api_scaler.go b/pkg/scalers/metrics_api_scaler.go index fd62a5bc773..086e67884ff 100644 --- a/pkg/scalers/metrics_api_scaler.go +++ b/pkg/scalers/metrics_api_scaler.go @@ -210,13 +210,13 @@ func (s *metricsAPIScaler) getMetricValue(ctx context.Context) (*resource.Quanti if err != nil { return nil, err } + defer r.Body.Close() if r.StatusCode != http.StatusOK { - msg := fmt.Sprintf("api returned %d", r.StatusCode) + msg := fmt.Sprintf("%s: api returned %d", r.Request.URL.Path, r.StatusCode) return nil, errors.New(msg) } - defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) if err != nil { return nil, err diff --git a/pkg/scalers/metrics_api_scaler_test.go b/pkg/scalers/metrics_api_scaler_test.go index b83ad955742..27584f9e4ae 100644 --- a/pkg/scalers/metrics_api_scaler_test.go +++ b/pkg/scalers/metrics_api_scaler_test.go @@ -3,10 +3,13 @@ package scalers import ( "context" "fmt" + "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" "time" + + "github.com/stretchr/testify/mock" ) type metricsAPIMetadataTestData struct { @@ -213,3 +216,30 @@ func TestBearerAuth(t *testing.T) { t.Errorf("Error getting the metric") } } + +type MockHTTPRoundTripper struct { + mock.Mock +} + +func (m *MockHTTPRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { + args := m.Called(request) + resp := args.Get(0).(*http.Response) + resp.Request = request + return resp, args.Error(1) +} + +func TestGetMetricValueErrorMessage(t *testing.T) { + // mock roundtripper to return non-ok status code + mockHTTPRoundTripper := MockHTTPRoundTripper{} + mockHTTPRoundTripper.On("RoundTrip", mock.Anything).Return(&http.Response{StatusCode: http.StatusTeapot}, nil) + + httpClient := http.Client{Transport: &mockHTTPRoundTripper} + s := metricsAPIScaler{ + metadata: &metricsAPIScalerMetadata{url: "http://dummy:1230/api/v1/"}, + client: &httpClient, + } + + _, err := s.getMetricValue(context.TODO()) + + assert.Equal(t, err.Error(), "/api/v1/: api returned 418") +}