diff --git a/CHANGELOG.md b/CHANGELOG.md index c6f36cd2e8b..184b95a2b80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ - **Datadog Scaler:** Several improvements, including a new optional parameter `metricUnavailableValue` to fill data when no Datadog metric was returned ([#2657](https://github.com/kedacore/keda/issues/2657)) - **GCP Pubsub Scaler** Adding e2e test for GCP PubSub scaler ([#1528](https://github.com/kedacore/keda/issues/1528)) - **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)) - **Prometheus Scaler:** Support for `X-Scope-OrgID` header in Prometheus scaler ([#2667](https://github.com/kedacore/keda/issues/2667)) - **RabbitMQ Scaler:** Include `vhost` for RabbitMQ when retrieving queue info with `useRegex` ([#2498](https://github.com/kedacore/keda/issues/2498)) 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") +}