Skip to content

Commit

Permalink
Change metric name when using regex.
Browse files Browse the repository at this point in the history
Signed-off-by: Vighnesh Shenoy <vshenoy@microsoft.com>
  • Loading branch information
v-shenoy committed Aug 29, 2022
1 parent 7c7f3b3 commit be62ef9
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions pkg/scalers/azure_servicebus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type azureServiceBusMetadata struct {
namespace string
endpointSuffix string
useRegex bool
regexPattern *regexp.Regexp
entityNameRegex *regexp.Regexp
operation string
scalerIndex int
}
Expand Down Expand Up @@ -156,13 +156,13 @@ func parseAzureServiceBusMetadata(config *ScalerConfig, logger logr.Logger) (*az
}

if meta.useRegex {
regexPattern, err := regexp.Compile(meta.queueName)
entityNameRegex, err := regexp.Compile(meta.queueName)
if err != nil {
return nil, fmt.Errorf("queueName is not a valid regular expression")
}
regexPattern.Longest()
entityNameRegex.Longest()

meta.regexPattern = regexPattern
meta.entityNameRegex = entityNameRegex
}
}

Expand All @@ -180,13 +180,13 @@ func parseAzureServiceBusMetadata(config *ScalerConfig, logger logr.Logger) (*az
}

if meta.useRegex {
regexPattern, err := regexp.Compile(meta.subscriptionName)
entityNameRegex, err := regexp.Compile(meta.subscriptionName)
if err != nil {
return nil, fmt.Errorf("subscriptionName is not a valid regular expression")
}
regexPattern.Longest()
entityNameRegex.Longest()

meta.regexPattern = regexPattern
meta.entityNameRegex = entityNameRegex
}
}

Expand Down Expand Up @@ -249,14 +249,19 @@ func (s *azureServiceBusScaler) Close(context.Context) error {
// Returns the metric spec to be used by the HPA
func (s *azureServiceBusScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricSpec {
metricName := ""
if s.metadata.entityType == queue {
metricName = s.metadata.queueName
} else {
metricName = s.metadata.topicName
}

if s.metadata.useRegex {
metricName = regexp.QuoteMeta(s.metadata.regexPattern.String())
if s.metadata.entityType == queue {
metricName = "queueWithRegex"
} else {
metricName = "subscriptionWithRegex"
}
} else {
if s.metadata.entityType == queue {
metricName = s.metadata.queueName
} else {
metricName = s.metadata.topicName
}
}

externalMetric := &v2beta2.ExternalMetricSource{
Expand Down Expand Up @@ -369,7 +374,7 @@ func getQueueMessageCount(ctx context.Context, ns *servicebus.Namespace, meta *a

values := make([]int64, 0)
for _, entity := range entities {
if meta.regexPattern.FindString(entity.Name) == entity.Name {
if meta.entityNameRegex.FindString(entity.Name) == entity.Name {
activeMessageCount := int64(*entity.CountDetails.ActiveMessageCount)
values = append(values, activeMessageCount)
}
Expand Down Expand Up @@ -402,7 +407,7 @@ func getSubscriptionMessageCount(ctx context.Context, ns *servicebus.Namespace,

values := make([]int64, 0)
for _, entity := range entities {
if meta.regexPattern.FindString(entity.Name) == entity.Name {
if meta.entityNameRegex.FindString(entity.Name) == entity.Name {
activeMessageCount := int64(*entity.CountDetails.ActiveMessageCount)
values = append(values, activeMessageCount)
}
Expand Down

0 comments on commit be62ef9

Please sign in to comment.