From 415288d2ca861f4acb7daafad0f5b23896cea394 Mon Sep 17 00:00:00 2001 From: Ratnadeep Debnath Date: Thu, 22 Jul 2021 19:24:05 +0200 Subject: [PATCH] Add suport custom metricName in RabbitMQ scaler Fixes #1975 Signed-off-by: Ratnadeep Debnath --- CHANGELOG.md | 1 + pkg/scalers/rabbitmq_scaler.go | 37 ++++++++++++++++------------- pkg/scalers/rabbitmq_scaler_test.go | 3 +++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d658344a826..8fbc4c34790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - Introduce Idle Replica Mode ([#1958](https://github.com/kedacore/keda/pull/1958)) - Add new scaler for Selenium Grid ([#1971](https://github.com/kedacore/keda/pull/1971)) - Support using regex to select the queues in RabbitMQ Scaler ([#1957](https://github.com/kedacore/keda/pull/1957)) +- Support custom metric name in RabbitMQ Scaler ([#1976](https://github.com/kedacore/keda/pull/1976)) ### Improvements diff --git a/pkg/scalers/rabbitmq_scaler.go b/pkg/scalers/rabbitmq_scaler.go index dc293c03a21..c62a35d6580 100644 --- a/pkg/scalers/rabbitmq_scaler.go +++ b/pkg/scalers/rabbitmq_scaler.go @@ -52,14 +52,15 @@ type rabbitMQScaler struct { } type rabbitMQMetadata struct { - queueName string - mode string // QueueLength or MessageRate - value int // trigger value (queue length or publish/sec. rate) - host string // connection string for either HTTP or AMQP protocol - protocol string // either http or amqp protocol - vhostName *string // override the vhost from the connection info - useRegex bool // specify if the queueName contains a rexeg - operation string // specify the operation to apply in case of multiples queues + queueName string + mode string // QueueLength or MessageRate + value int // trigger value (queue length or publish/sec. rate) + host string // connection string for either HTTP or AMQP protocol + protocol string // either http or amqp protocol + vhostName *string // override the vhost from the connection info + useRegex bool // specify if the queueName contains a rexeg + operation string // specify the operation to apply in case of multiples queues + metricName string // Custom metric name for trigger } type queueInfo struct { @@ -197,6 +198,17 @@ func parseRabbitMQMetadata(config *ScalerConfig) (*rabbitMQMetadata, error) { return nil, fmt.Errorf("unable to parse trigger: %s", err) } + // Resolve metricName + if val, ok := config.TriggerMetadata["metricName"]; ok { + meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "rabbitmq", val)) + } else { + if meta.mode == rabbitModeQueueLength { + meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "rabbitmq", meta.queueName)) + } else { + meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "rabbitmq-rate", meta.queueName)) + } + } + return &meta, nil } @@ -383,18 +395,11 @@ func (s *rabbitMQScaler) getQueueInfoViaHTTP() (*queueInfo, error) { // GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler func (s *rabbitMQScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec { - var metricName string - - if s.metadata.mode == rabbitModeQueueLength { - metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "rabbitmq", s.metadata.queueName)) - } else { - metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "rabbitmq-rate", s.metadata.queueName)) - } metricValue := resource.NewQuantity(int64(s.metadata.value), resource.DecimalSI) externalMetric := &v2beta2.ExternalMetricSource{ Metric: v2beta2.MetricIdentifier{ - Name: metricName, + Name: s.metadata.metricName, }, Target: v2beta2.MetricTarget{ Type: v2beta2.AverageValueMetricType, diff --git a/pkg/scalers/rabbitmq_scaler_test.go b/pkg/scalers/rabbitmq_scaler_test.go index 65cbb4bd0f5..46befbdf4e2 100644 --- a/pkg/scalers/rabbitmq_scaler_test.go +++ b/pkg/scalers/rabbitmq_scaler_test.go @@ -92,11 +92,14 @@ var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{ {map[string]string{"mode": "MessageRate", "value": "1000", "queueName": "sample", "host": "http://", "useRegex": "true"}, false, map[string]string{}}, // queue length and useRegex {map[string]string{"mode": "QueueLength", "value": "1000", "queueName": "sample", "host": "http://", "useRegex": "true"}, false, map[string]string{}}, + // custom metric name + {map[string]string{"mode": "QueueLength", "value": "1000", "queueName": "sample", "host": "http://", "useRegex": "true", "metricName": "host1-sample"}, false, map[string]string{}}, } var rabbitMQMetricIdentifiers = []rabbitMQMetricIdentifier{ {&testRabbitMQMetadata[1], "rabbitmq-sample"}, {&testRabbitMQMetadata[7], "rabbitmq-namespace-name"}, + {&testRabbitMQMetadata[31], "rabbitmq-host1-sample"}, } func TestRabbitMQParseMetadata(t *testing.T) {