diff --git a/CHANGELOG.md b/CHANGELOG.md index e256f99ea10..1e473066534 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - **CPU Scaler:** Adding e2e test for the cpu scaler ([#2441](https://github.com/kedacore/keda/pull/2441)) - **AWS SQS Scaler:** allow using simple queue name instead of URL ([#2457](https://github.com/kedacore/keda/pull/2457)) - **AWS Scalers:** Support temporary AWS credentials using session tokens ([#2573](https://github.com/kedacore/keda/pull/2573)) +- **Prometheus Scaler:** Support namespaced Prometheus queries ([#2575](https://github.com/kedacore/keda/issues/2575)) ### Breaking Changes diff --git a/pkg/scalers/prometheus_scaler.go b/pkg/scalers/prometheus_scaler.go index 9a311dc038e..beec6f965c6 100644 --- a/pkg/scalers/prometheus_scaler.go +++ b/pkg/scalers/prometheus_scaler.go @@ -26,6 +26,7 @@ const ( promMetricName = "metricName" promQuery = "query" promThreshold = "threshold" + promNamespace = "namespace" ) type prometheusScaler struct { @@ -39,6 +40,7 @@ type prometheusMetadata struct { query string threshold int prometheusAuth *authentication.AuthMeta + namespace string scalerIndex int } @@ -112,6 +114,10 @@ func parsePrometheusMetadata(config *ScalerConfig) (meta *prometheusMetadata, er meta.threshold = t } + if val, ok := config.TriggerMetadata[promNamespace]; ok && val != "" { + meta.namespace = val + } + meta.scalerIndex = config.ScalerIndex // parse auth configs from ScalerConfig @@ -159,6 +165,12 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error t := time.Now().UTC().Format(time.RFC3339) queryEscaped := url_pkg.QueryEscape(s.metadata.query) url := fmt.Sprintf("%s/api/v1/query?query=%s&time=%s", s.metadata.serverAddress, queryEscaped, t) + + // set 'namespace' parameter for namespaced Prometheus requests (eg. for Thanos Querier) + if s.metadata.namespace != "" { + url = fmt.Sprintf("%s&namespace=%s", url, s.metadata.namespace) + } + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return -1, err diff --git a/pkg/scalers/prometheus_scaler_test.go b/pkg/scalers/prometheus_scaler_test.go index 3a73a7d10d4..2caf0bb8046 100644 --- a/pkg/scalers/prometheus_scaler_test.go +++ b/pkg/scalers/prometheus_scaler_test.go @@ -25,6 +25,8 @@ var testPromMetadata = []parsePrometheusMetadataTestData{ {map[string]string{}, true}, // all properly formed {map[string]string{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up"}, false}, + // all properly formed, with namespace + {map[string]string{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up", "namespace": "foo"}, false}, // missing serverAddress {map[string]string{"serverAddress": "", "metricName": "http_requests_total", "threshold": "100", "query": "up"}, true}, // missing metricName