Skip to content

Commit

Permalink
AWS SQS Scaler: Add Visible + NotVisible messages for scaling conside…
Browse files Browse the repository at this point in the history
…rations

Signed-off-by: Ty Brown <tbrown@rewardsnetwork.com>
  • Loading branch information
TyBrown committed Mar 12, 2021
1 parent 896ff4d commit 8e766e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- Make `swiftURL` parameter optional for the OpenStack Swift scaler ([#1652](https://github.com/kedacore/keda/pull/1652))
- Fix memory leak of `keda-metrics-apiserver` by setting a controller-runtime logger properly ([#1654](https://github.com/kedacore/keda/pull/1654))
- Add `publishRate` trigger to RabbitMQ scaler ([#1653](https://github.com/kedacore/keda/pull/1653))
- AWS SQS Scaler: Add Visible + NotVisible messages for scaling considerations ([#1664](https://github.com/kedacore/keda/pull/1664))

### Breaking Changes

Expand Down
23 changes: 16 additions & 7 deletions pkg/scalers/aws_sqs_queue_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ import (
)

const (
awsSqsQueueMetricName = "ApproximateNumberOfMessages"
targetQueueLengthDefault = 5
)

var (
awsSqsQueueMetricNames = []string{
"ApproximateNumberOfMessages",
"ApproximateNumberOfMessagesNotVisible",
}
sqsQueueLog = logf.Log.WithName("aws_sqs_queue_scaler")
)

type awsSqsQueueScaler struct {
metadata *awsSqsQueueMetadata
}
Expand All @@ -40,8 +47,6 @@ type awsSqsQueueMetadata struct {
awsAuthorization awsAuthorizationMetadata
}

var sqsQueueLog = logf.Log.WithName("aws_sqs_queue_scaler")

// NewAwsSqsQueueScaler creates a new awsSqsQueueScaler
func NewAwsSqsQueueScaler(config *ScalerConfig) (Scaler, error) {
meta, err := parseAwsSqsQueueMetadata(config)
Expand Down Expand Up @@ -154,7 +159,7 @@ func (s *awsSqsQueueScaler) GetMetrics(ctx context.Context, metricName string, m
// Get SQS Queue Length
func (s *awsSqsQueueScaler) GetAwsSqsQueueLength() (int32, error) {
input := &sqs.GetQueueAttributesInput{
AttributeNames: aws.StringSlice([]string{awsSqsQueueMetricName}),
AttributeNames: aws.StringSlice(awsSqsQueueMetricNames),
QueueUrl: aws.String(s.metadata.queueURL),
}

Expand Down Expand Up @@ -185,9 +190,13 @@ func (s *awsSqsQueueScaler) GetAwsSqsQueueLength() (int32, error) {
return -1, err
}

approximateNumberOfMessages, err := strconv.ParseInt(*output.Attributes[awsSqsQueueMetricName], 10, 32)
if err != nil {
return -1, err
var approximateNumberOfMessages int64
for _, awsSqsQueueMetric := range awsSqsQueueMetricNames {
metricValue, err := strconv.ParseInt(*output.Attributes[awsSqsQueueMetric], 10, 32)
if err != nil {
return -1, err
}
approximateNumberOfMessages += metricValue
}

return int32(approximateNumberOfMessages), nil
Expand Down

0 comments on commit 8e766e3

Please sign in to comment.