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

Add query parameters in prometheus scaler #4957

Merged
Merged
Show file tree
Hide file tree
Changes from 16 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 @@ -50,6 +50,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

### New

- **General**: Add parameter queryParameters in prometheus-scaler ([#4962](https://github.com/kedacore/keda/issues/4962))
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
- **General**: TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))

#### Experimental
Expand Down
20 changes: 20 additions & 0 deletions pkg/scalers/prometheus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
url_pkg "net/url"
"strconv"
"strings"
"time"

"github.com/go-logr/logr"
Expand All @@ -25,6 +26,7 @@ import (
const (
promServerAddress = "serverAddress"
promQuery = "query"
promQueryParameters = "queryParameters"
promThreshold = "threshold"
promActivationThreshold = "activationThreshold"
promNamespace = "namespace"
Expand All @@ -48,6 +50,7 @@ type prometheusScaler struct {
type prometheusMetadata struct {
serverAddress string
query string
queryParameters map[string]string
threshold float64
activationThreshold float64
prometheusAuth *authentication.AuthMeta
Expand Down Expand Up @@ -151,6 +154,15 @@ func parsePrometheusMetadata(config *ScalerConfig) (meta *prometheusMetadata, er
return nil, fmt.Errorf("no %s given", promQuery)
}

if val, ok := config.TriggerMetadata[promQueryParameters]; ok && val != "" {
queryParameters, err := kedautil.ParseStringList(val)
if err != nil {
return nil, fmt.Errorf("error parsing %s: %w", promQueryParameters, err)
}

meta.queryParameters = queryParameters
}

if val, ok := config.TriggerMetadata[promThreshold]; ok && val != "" {
t, err := strconv.ParseFloat(val, 64)
if err != nil {
Expand Down Expand Up @@ -266,6 +278,14 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error
url = fmt.Sprintf("%s&namespace=%s", url, s.metadata.namespace)
}

for queryParameterKey, queryParameterValue := range s.metadata.queryParameters {
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
test := queryParameterValue[len(queryParameterValue)-1:]
if test == "?" || test == "&" {
queryParameterValue = strings.TrimSuffix(queryParameterValue, test)
}
url = fmt.Sprintf("%s&%s=%s", url, queryParameterKey, queryParameterValue)
}

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return -1, err
Expand Down
48 changes: 48 additions & 0 deletions pkg/scalers/prometheus_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -60,6 +61,10 @@ var testPromMetadata = []parsePrometheusMetadataTestData{
{map[string]string{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up", "customHeaders": "key1=value1,key2"}, true},
// deprecated cortexOrgID
{map[string]string{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up", "cortexOrgID": "my-org"}, true},
// queryParameters
{map[string]string{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up", "queryParameters": "key1=value1,key2=value2"}, false},
// queryParameters with wrong format
{map[string]string{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up", "queryParameters": "key1=value1,key2"}, true},
}

var prometheusMetricIdentifiers = []prometheusMetricIdentifier{
Expand Down Expand Up @@ -372,6 +377,49 @@ func TestPrometheusScalerCustomHeaders(t *testing.T) {
assert.NoError(t, err)
}

func TestPrometheusScalerExecutePromQueryParameters(t *testing.T) {
testData := prometheusQromQueryResultTestData{
name: "no values",
bodyStr: `{"data":{"result":[]}}`,
responseStatus: http.StatusOK,
expectedValue: 0,
isError: false,
ignoreNullValues: true,
}
queryParametersValue := map[string]string{
"first": "foo",
"second": "bar",
}
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
time := time.Now().UTC().Format(time.RFC3339)
expectedPath := fmt.Sprintf("/api/v1/query?query=&time=%s&first=foo&second=bar", time)
for queryParameterName, queryParameterValue := range queryParametersValue {
queryParameter := request.URL.Query()
queryParameter.Add(queryParameterName, queryParameterValue)
}

if request.RequestURI != expectedPath {
t.Error("Expect request path to =", expectedPath, "but it is", request.RequestURI)
}

writer.WriteHeader(testData.responseStatus)
if _, err := writer.Write([]byte(testData.bodyStr)); err != nil {
t.Fatal(err)
}
}))
scaler := prometheusScaler{
metadata: &prometheusMetadata{
serverAddress: server.URL,
queryParameters: queryParametersValue,
ignoreNullValues: testData.ignoreNullValues,
},
httpClient: http.DefaultClient,
}
_, err := scaler.ExecutePromQuery(context.TODO())

assert.NoError(t, err)
}

func TestPrometheusScaler_ExecutePromQuery_WithGCPNativeAuthentication(t *testing.T) {
fakeGoogleOAuthServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"token_type": "Bearer", "access_token": "fake_access_token"}`)
Expand Down