Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included inflight messages, take a look if you consider this as an enhancement #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# kube-sqs-autoscaler
Kubernetes pod autoscaler based on queue size in AWS SQS. It periodically retrieves the number of messages in your queue and scales pods accordingly.
Kubernetes pod autoscaler based on queue size in AWS SQS. It periodically retrieves the number of messages(Visible and inflight) in your queue and scales pods accordingly.

## Setting up
Setting up kube-sqs-autoscaler requires two steps:
Expand Down Expand Up @@ -28,7 +28,7 @@ spec:
spec:
containers:
- name: kube-sqs-autoscaler
image: wattpad/kube-sqs-autoscaler:v1.2.1
image: Build and add your tag here
command:
- /kube-sqs-autoscaler
- --sqs-queue-url=https://sqs.your_aws_region.amazonaws.com/your_aws_account_number/your_queue_name # required
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

log "github.com/Sirupsen/logrus"

"github.com/Wattpad/kube-sqs-autoscaler/scale"
"github.com/Wattpad/kube-sqs-autoscaler/sqs"
"kube-sqs-autoscaler/scale"
"kube-sqs-autoscaler/sqs"
)

var (
Expand All @@ -33,7 +33,13 @@ func Run(p *scale.PodAutoScaler, sqs *sqs.SqsClient) {
select {
case <-time.After(pollInterval):
{
numMessages, err := sqs.NumMessages()
numViMessages, err := sqs.NumMessages()
log.Info("Total no. of visible messages: ",numViMessages)
numinMessages, err := sqs.NumInflightMessages()
log.Info("Total no. of inflight messages: ",numinMessages)
numMessages := numinMessages + numViMessages
log.Info("Total messages for processing: ",numMessages)

if err != nil {
log.Errorf("Failed to get SQS messages: %v", err)
continue
Expand Down
23 changes: 23 additions & 0 deletions sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ func (s *SqsClient) NumMessages() (int, error) {

return messages, nil
}

func (s *SqsClient) NumInflightMessages() (int, error) {
params := &sqs.GetQueueAttributesInput{
AttributeNames: []*string{aws.String("ApproximateNumberOfMessagesNotVisible")},
QueueUrl: aws.String(s.QueueUrl),
}


out, err := s.Client.GetQueueAttributes(params)

if err != nil {
return 0, errors.Wrap(err, "Failed to get messages in SQS")
}


messages, err := strconv.Atoi(*out.Attributes["ApproximateNumberOfMessagesNotVisible"])

if err != nil {
return 0, errors.Wrap(err, "Failed to get number of messages in queue")
}

return messages, nil
}