Skip to content

Commit

Permalink
feat: add logic for prometheus scaler X-Scope-OrgID header (#2763)
Browse files Browse the repository at this point in the history
Signed-off-by: Xoán Mallón <xoanmallon@gmail.com>
  • Loading branch information
xoanmm authored Mar 16, 2022
1 parent 4e1b63b commit 092196a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- **Datadog Scaler:** Several improvements, including a new optional parameter `metricUnavailableValue` to fill data when no Datadog metric was returned ([#2657](https://github.com/kedacore/keda/issues/2657))
- **GCP Pubsub Scaler** Adding e2e test for GCP PubSub scaler ([#1528](https://github.com/kedacore/keda/issues/1528))
- **Kafka Scaler** Make "disable" a valid value for tls auth parameter ([#2608](https://github.com/kedacore/keda/issues/2608))
- **Prometheus Scaler:** Support for `X-Scope-OrgID` header in Prometheus scaler ([#2667](https://github.com/kedacore/keda/issues/2667))
- **RabbitMQ Scaler:** Include `vhost` for RabbitMQ when retrieving queue info with `useRegex` ([#2498](https://github.com/kedacore/keda/issues/2498))

### Breaking Changes
Expand Down
21 changes: 16 additions & 5 deletions pkg/scalers/prometheus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import (
)

const (
promServerAddress = "serverAddress"
promMetricName = "metricName"
promQuery = "query"
promThreshold = "threshold"
promNamespace = "namespace"
promServerAddress = "serverAddress"
promMetricName = "metricName"
promQuery = "query"
promThreshold = "threshold"
promNamespace = "namespace"
promCortexScopeOrgID = "cortexOrgID"
promCortexHeaderKey = "X-Scope-OrgID"
)

type prometheusScaler struct {
Expand All @@ -42,6 +44,7 @@ type prometheusMetadata struct {
prometheusAuth *authentication.AuthMeta
namespace string
scalerIndex int
cortexOrgID string
}

type promQueryResult struct {
Expand Down Expand Up @@ -118,6 +121,10 @@ func parsePrometheusMetadata(config *ScalerConfig) (meta *prometheusMetadata, er
meta.namespace = val
}

if val, ok := config.TriggerMetadata[promCortexScopeOrgID]; ok && val != "" {
meta.cortexOrgID = val
}

meta.scalerIndex = config.ScalerIndex

// parse auth configs from ScalerConfig
Expand Down Expand Up @@ -182,6 +189,10 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error
req.SetBasicAuth(s.metadata.prometheusAuth.Username, s.metadata.prometheusAuth.Password)
}

if s.metadata.cortexOrgID != "" {
req.Header.Add(promCortexHeaderKey, s.metadata.cortexOrgID)
}

r, err := s.httpClient.Do(req)
if err != nil {
return -1, err
Expand Down
31 changes: 31 additions & 0 deletions pkg/scalers/prometheus_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,34 @@ func TestPrometheusScalerExecutePromQuery(t *testing.T) {
})
}
}

func TestPrometheusScalerCortexHeader(t *testing.T) {
testData := prometheusQromQueryResultTestData{
name: "no values",
bodyStr: `{"data":{"result":[]}}`,
responseStatus: http.StatusOK,
expectedValue: 0,
isError: false,
}
cortexOrgValue := "my-org"
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
reqHeader := request.Header.Get(promCortexHeaderKey)
assert.Equal(t, reqHeader, cortexOrgValue)
writer.WriteHeader(testData.responseStatus)
if _, err := writer.Write([]byte(testData.bodyStr)); err != nil {
t.Fatal(err)
}
}))

scaler := prometheusScaler{
metadata: &prometheusMetadata{
serverAddress: server.URL,
cortexOrgID: cortexOrgValue,
},
httpClient: http.DefaultClient,
}

_, err := scaler.ExecutePromQuery(context.TODO())

assert.NoError(t, err)
}

0 comments on commit 092196a

Please sign in to comment.