Skip to content

Commit

Permalink
Add query parameters in prometheus scaler
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriquePiccolo committed Sep 8, 2023
1 parent 791c895 commit 1f01fd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
- **Governance**: KEDA transitioned to CNCF Graduated project ([#63](https://github.com/kedacore/governance/issues/63))

### Improvements
- **General**: Add parameter queryParameters in prometheus-scaler (N/A)
- **General**: Add more events for user checking ([#796](https://github.com/kedacore/keda/issues/3764))
- **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))
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)
}

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return -1, err
Expand Down

0 comments on commit 1f01fd0

Please sign in to comment.