Skip to content

Commit

Permalink
Add unique name for aws-sqs-queue metrics (#475)
Browse files Browse the repository at this point in the history
Add tests for queueURL to queueName parsing
  • Loading branch information
zach-dunton-sf authored and ahmelsayed committed Dec 4, 2019
1 parent 7618f3b commit 5ab3776
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
19 changes: 18 additions & 1 deletion pkg/scalers/aws_sqs_queue_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package scalers
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
Expand Down Expand Up @@ -31,6 +33,7 @@ type awsSqsQueueScaler struct {
type awsSqsQueueMetadata struct {
targetQueueLength int
queueURL string
queueName string
awsRegion string
awsAuthorization awsAuthorizationMetadata
}
Expand Down Expand Up @@ -69,6 +72,19 @@ func parseAwsSqsQueueMetadata(metadata, resolvedEnv, authParams map[string]strin
return nil, fmt.Errorf("no queueURL given")
}

queueURL, err := url.ParseRequestURI(meta.queueURL)
if err != nil {
return nil, fmt.Errorf("queueURL is not a valid URL")
}

queueURLPath := queueURL.Path
queueURLPathParts := strings.Split(queueURLPath, "/")
if len(queueURLPathParts) != 3 || len(queueURLPathParts[2]) <= 0 {
return nil, fmt.Errorf("cannot get queueName from queueURL")
}

meta.queueName = queueURLPathParts[2]

if val, ok := metadata["awsRegion"]; ok && val != "" {
meta.awsRegion = val
} else {
Expand Down Expand Up @@ -102,7 +118,8 @@ func (s *awsSqsQueueScaler) Close() error {

func (s *awsSqsQueueScaler) GetMetricSpecForScaling() []v2beta1.MetricSpec {
targetQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetQueueLength), resource.DecimalSI)
externalMetric := &v2beta1.ExternalMetricSource{MetricName: awsSqsQueueMetricName, TargetAverageValue: targetQueueLengthQty}
externalMetric := &v2beta1.ExternalMetricSource{MetricName: fmt.Sprintf("%s-%s-%s", "AWS-SQS-Queue", awsSqsQueueMetricName, s.metadata.queueName),
TargetAverageValue: targetQueueLengthQty}
metricSpec := v2beta1.MetricSpec{External: externalMetric, Type: externalMetricType}
return []v2beta1.MetricSpec{metricSpec}
}
Expand Down
41 changes: 30 additions & 11 deletions pkg/scalers/aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"testing"
)

var testAWSSQSRoleArn = "none"
const (
testAWSSQSRoleArn = "none"
testAWSSQSAccessKeyID = "none"
testAWSSQSSecretAccessKey = "none"

var testAWSSQSAccessKeyID = "none"
var testAWSSQSSecretAccessKey = "none"
testAWSSQSProperQueueURL = "https://sqs.eu-west-1.amazonaws.com/account_id/DeleteArtifactQ"
testAWSSQSImproperQueueURL1 = "https://sqs.eu-west-1.amazonaws.com/account_id"
testAWSSQSImproperQueueURL2 = "https://sqs.eu-west-1.amazonaws.com"
)

var testAWSSQSResolvedEnv = map[string]string{
"AWS_ACCESS_KEY": "none",
Expand All @@ -32,35 +37,49 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
true,
"metadata empty"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
false,
"properly formed queue and region"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSImproperQueueURL1,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
true,
"improperly formed queue, missing queueName"},
{map[string]string{
"queueURL": testAWSSQSImproperQueueURL2,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
true,
"improperly formed queue, missing path"},
{map[string]string{
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": ""},
testAWSSQSAuthentication,
true,
"properly formed queue, empty region"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
false,
"properly formed queue, integer queueLength"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "a",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
false,
"properly formed queue, invalid queueLength"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
map[string]string{
Expand All @@ -70,7 +89,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
false,
"with AWS Credentials from TriggerAuthentication"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
map[string]string{
Expand All @@ -80,7 +99,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
true,
"with AWS Credentials from TriggerAuthentication, missing Access Key Id"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
map[string]string{
Expand All @@ -90,7 +109,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
true,
"with AWS Credentials from TriggerAuthentication, missing Secret Access Key"},
{map[string]string{
"queueURL": "myqueue",
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
map[string]string{
Expand Down

0 comments on commit 5ab3776

Please sign in to comment.