Skip to content

Commit be22628

Browse files
leodipJorTurFer
authored andcommitted
Update GetAzureQueueLength in azure storage queue scaler to consider queueLengthStrategy
Signed-off-by: Leonardo D'Ippolito <[email protected]>
1 parent ccadc8a commit be22628

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Here is an overview of all new **experimental** features:
7474

7575
### Improvements
7676

77+
- **Azure queue scaler**: Added new configuration option 'queueLengthStrategy' ([#4478](https://github.com/kedacore/keda/issues/4478))
7778
- **Cassandra Scaler**: Add TLS support for cassandra scaler ([#5802](https://github.com/kedacore/keda/issues/5802))
7879
- **GCP Pub/Sub**: Add optional valueIfNull to allow a default scaling value and prevent errors when GCP metric returns no value. ([#5896](https://github.com/kedacore/keda/issues/5896))
7980
- **GCP Scalers**: Added custom time horizon in GCP scalers ([#5778](https://github.com/kedacore/keda/issues/5778))

pkg/scalers/azure_queue_scaler.go

+48-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"strconv"
23+
"strings"
2324

2425
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
2526
"github.com/go-logr/logr"
@@ -33,10 +34,16 @@ import (
3334
)
3435

3536
const (
36-
queueLengthMetricName = "queueLength"
37-
activationQueueLengthMetricName = "activationQueueLength"
38-
defaultTargetQueueLength = 5
39-
externalMetricType = "External"
37+
queueLengthMetricName = "queueLength"
38+
activationQueueLengthMetricName = "activationQueueLength"
39+
defaultTargetQueueLength = 5
40+
externalMetricType = "External"
41+
QueueLengthStrategyAll string = "all"
42+
QueueLengthStrategyVisibleOnly string = "visibleonly"
43+
)
44+
45+
var (
46+
maxPeekMessages int32 = 32
4047
)
4148

4249
type azureQueueScaler struct {
@@ -53,6 +60,7 @@ type azureQueueMetadata struct {
5360
connection string
5461
accountName string
5562
endpointSuffix string
63+
queueLengthStrategy string
5664
triggerIndex int
5765
}
5866

@@ -123,6 +131,17 @@ func parseAzureQueueMetadata(config *scalersconfig.ScalerConfig, logger logr.Log
123131
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("no queueName given")
124132
}
125133

134+
if val, ok := config.TriggerMetadata["queueLengthStrategy"]; ok && val != "" {
135+
strategy := strings.ToLower(val)
136+
if strategy == QueueLengthStrategyAll || strategy == QueueLengthStrategyVisibleOnly {
137+
meta.queueLengthStrategy = strategy
138+
} else {
139+
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("invalid queueLengthStrategy %s given", val)
140+
}
141+
} else {
142+
meta.queueLengthStrategy = QueueLengthStrategyAll
143+
}
144+
126145
// If the Use AAD Pod Identity is not present, or set to "none"
127146
// then check for connection string
128147
switch config.PodIdentity.Provider {
@@ -172,12 +191,35 @@ func (s *azureQueueScaler) GetMetricSpecForScaling(context.Context) []v2.MetricS
172191

173192
// GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
174193
func (s *azureQueueScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
175-
props, err := s.queueClient.GetProperties(ctx, nil)
194+
queuelen, err := s.getMessageCount(ctx)
176195
if err != nil {
177196
s.logger.Error(err, "error getting queue length")
178197
return []external_metrics.ExternalMetricValue{}, false, err
179198
}
180-
queuelen := int64(*props.ApproximateMessagesCount)
199+
181200
metric := GenerateMetricInMili(metricName, float64(queuelen))
182201
return []external_metrics.ExternalMetricValue{metric}, queuelen > s.metadata.activationTargetQueueLength, nil
183202
}
203+
204+
func (s *azureQueueScaler) getMessageCount(ctx context.Context) (int64, error) {
205+
strategy := strings.ToLower(s.metadata.queueLengthStrategy)
206+
if strategy == QueueLengthStrategyVisibleOnly {
207+
queue, err := s.queueClient.PeekMessages(ctx, &azqueue.PeekMessagesOptions{NumberOfMessages: &maxPeekMessages})
208+
if err != nil {
209+
return 0, err
210+
}
211+
visibleMessageCount := len(queue.Messages)
212+
213+
// Queue has less messages than we allowed to peek for,
214+
// so no need to fall back to the 'all' strategy
215+
if visibleMessageCount < int(maxPeekMessages) {
216+
return int64(visibleMessageCount), nil
217+
}
218+
}
219+
220+
props, err := s.queueClient.GetProperties(ctx, nil)
221+
if err != nil {
222+
return 0, err
223+
}
224+
return int64(*props.ApproximateMessagesCount), nil
225+
}

0 commit comments

Comments
 (0)