Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error handling on non-ok response for metric server #2739

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions pkg/scalers/metrics_api_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/scalers/metrics_api_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}