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 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

### Improvements
- **General**: Add more events for user checking ([#796](https://github.com/kedacore/keda/issues/3764))
- **General**: Add parameter queryParameters in prometheus-scaler ([#4962](https://github.com/kedacore/keda/issues/4962))
- **General**: Add ScaledObject/ScaledJob names to output of `kubectl get triggerauthentication/clustertriggerauthentication` ([#796](https://github.com/kedacore/keda/issues/796))
- **General**: Add standalone CRD generation to release workflow ([#2726](https://github.com/kedacore/keda/issues/2726))
- **General**: Adding a changelog validating script to check for formatting and order ([#3190](https://github.com/kedacore/keda/issues/3190))
Expand Down
15 changes: 15 additions & 0 deletions pkg/scalers/prometheus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
const (
promServerAddress = "serverAddress"
promQuery = "query"
promQueryParameters = "queryParameters"
promThreshold = "threshold"
promActivationThreshold = "activationThreshold"
promNamespace = "namespace"
Expand All @@ -48,6 +49,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 +153,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 @@ -262,6 +273,10 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error
url = fmt.Sprintf("%s&namespace=%s", url, s.metadata.namespace)
}

if len(s.metadata.queryParameters) > 0 {
url = fmt.Sprintf("%s&%s", url, s.metadata.queryParameters)
}
zroubalik marked this conversation as resolved.
Show resolved Hide resolved

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return -1, err
Expand Down
44 changes: 44 additions & 0 deletions pkg/scalers/prometheus_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -370,6 +374,46 @@ 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) {
for queryParameterName, queryParameterValue := range queryParametersValue {
queryParameter := request.URL.Query()
queryParameter.Add(queryParameterName, queryParameterValue)
}


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