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

Support namespaced Prometheus queries #2576

Merged
merged 2 commits into from
Jan 28, 2022
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 @@ -45,6 +45,7 @@
- **CPU Scaler:** Adding e2e test for the cpu scaler ([#2441](https://github.com/kedacore/keda/pull/2441))
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
- **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

Expand Down
12 changes: 12 additions & 0 deletions pkg/scalers/prometheus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
promMetricName = "metricName"
promQuery = "query"
promThreshold = "threshold"
promNamespace = "namespace"
)

type prometheusScaler struct {
Expand All @@ -39,6 +40,7 @@ type prometheusMetadata struct {
query string
threshold int
prometheusAuth *authentication.AuthMeta
namespace string
scalerIndex int
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/scalers/prometheus_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down