Skip to content
New issue

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

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

Already on GitHub? # to your account

Support multiple price regions #64

Merged
merged 2 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 61 additions & 23 deletions collector/aws/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,29 @@ func (dd *DynamoDBManager) Detect() ([]DetectedAWSDynamoDB, error) {
})

detectedTables := []DetectedAWSDynamoDB{}
tables, err := dd.DescribeTables()
now := time.Now()
tables, err := dd.DescribeTables(nil, nil)

if err != nil {
log.WithField("error", err).Error("could not describe rds instances")
dd.collector.UpdateServiceStatus(collector.EventCollector{
ResourceName: dd.Name,
Data: collector.EventStatusData{
Status: collector.EventError,
ErrorMessage: err.Error(),
},
})
log.WithField("error", err).Error("could not describe dynamoDB tables")
dd.updateErrorServiceStatus(err)
return detectedTables, err
}

writePricePerHour, err := dd.#Client.GetPrice(dd.Get#WriteFilterInput(), rateCode, dd.region)
if err != nil {
log.WithField("error", err).Error("could not get write dynamoDB price")
dd.updateErrorServiceStatus(err)
return detectedTables, err
}

writePricePerHour, _ := dd.#Client.GetPrice(dd.Get#WriteFilterInput(), rateCode, dd.region)
readPricePerHour, _ := dd.#Client.GetPrice(dd.Get#ReadFilterInput(), rateCode, dd.region)
readPricePerHour, err := dd.#Client.GetPrice(dd.Get#ReadFilterInput(), rateCode, dd.region)
if err != nil {
log.WithField("error", err).Error("could not get read dynamoDB price")
dd.updateErrorServiceStatus(err)
return detectedTables, err
}

now := time.Now()
for _, table := range tables {

log.WithField("table_name", *table.TableName).Debug("checking dynamodb table")
Expand All @@ -120,7 +124,7 @@ func (dd *DynamoDBManager) Detect() ([]DetectedAWSDynamoDB, error) {
StartTime: &metricEndTime,
EndTime: &now,
Dimensions: []*cloudwatch.Dimension{
&cloudwatch.Dimension{
{
Name: awsClient.String("TableName"),
Value: table.TableName,
},
Expand Down Expand Up @@ -233,10 +237,15 @@ func (dd *DynamoDBManager) Get#WriteFilterInput() *#.GetProductsInpu
input := &#.GetProductsInput{
ServiceCode: awsClient.String(dd.service#Code),
Filters: []*#.Filter{
&#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("termType"),
Value: awsClient.String("Reserved"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("usagetype"),
Value: awsClient.String("WriteCapacityUnit-Hrs"),
Field: awsClient.String("group"),
Value: awsClient.String("DDB-WriteUnits"),
},
},
}
Expand All @@ -250,10 +259,15 @@ func (dd *DynamoDBManager) Get#ReadFilterInput() *#.GetProductsInput
input := &#.GetProductsInput{
ServiceCode: &dd.service#Code,
Filters: []*#.Filter{
&#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("usagetype"),
Value: awsClient.String("ReadCapacityUnit-Hrs"),
Field: awsClient.String("termType"),
Value: awsClient.String("Reserved"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("group"),
Value: awsClient.String("DDB-ReadUnits"),
},
},
}
Expand All @@ -262,26 +276,50 @@ func (dd *DynamoDBManager) Get#ReadFilterInput() *#.GetProductsInput
}

// DescribeTables return all dynamoDB tables
func (dd *DynamoDBManager) DescribeTables() ([]*dynamodb.TableDescription, error) {
func (dd *DynamoDBManager) DescribeTables(exclusiveStartTableName *string, tables []*dynamodb.TableDescription) ([]*dynamodb.TableDescription, error) {

input := &dynamodb.ListTablesInput{}
input := &dynamodb.ListTablesInput{
ExclusiveStartTableName: exclusiveStartTableName,
}

resp, err := dd.client.ListTables(input)
if err != nil {
log.WithField("error", err).Error("could not list any dynamoDB tables")
return nil, err
}

tables := []*dynamodb.TableDescription{}
for _, tableName := range resp.TableNames {
if tables == nil {
tables = []*dynamodb.TableDescription{}
}

var lastTableName string
for _, tableName := range resp.TableNames {
lastTableName = *tableName
resp, err := dd.client.DescribeTable(&dynamodb.DescribeTableInput{TableName: tableName})
if err != nil {
log.WithField("error", err).WithField("table", *tableName).Error("could not describe dynamoDB table")
continue
}
if resp.Table.BillingModeSummary == nil {
tables = append(tables, resp.Table)
}

}

if lastTableName != "" {
return dd.DescribeTables(&lastTableName, tables)
}

return tables, nil
}

// updateErrorServiceStatus reports when dynamoDB can't collect data
func (dd *DynamoDBManager) updateErrorServiceStatus(err error) {
dd.collector.UpdateServiceStatus(collector.EventCollector{
ResourceName: dd.Name,
Data: collector.EventStatusData{
Status: collector.EventError,
ErrorMessage: err.Error(),
},
})
}
16 changes: 11 additions & 5 deletions collector/aws/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ var defaultDynamoDBDescribeTableMock = dynamodb.DescribeTableOutput{
type MockAWSDynamoDBClient struct {
responseListTable dynamodb.ListTablesOutput
responseDescribeTable dynamodb.DescribeTableOutput
listTableCountRequest int
err error
}

func (r *MockAWSDynamoDBClient) ListTables(*dynamodb.ListTablesInput) (*dynamodb.ListTablesOutput, error) {

r.listTableCountRequest += 1
if r.listTableCountRequest == 2 {
return &dynamodb.ListTablesOutput{
TableNames: []*string{},
}, r.err
}
return &r.responseListTable, r.err

}
Expand Down Expand Up @@ -65,7 +71,7 @@ func TestDescribeDynamoDBTables(t *testing.T) {

dynamoDBManager := aws.NewDynamoDBManager(collector, &mockClient, nil, nil, metrics, "us-east-1")

result, _ := dynamoDBManager.DescribeTables()
result, _ := dynamoDBManager.DescribeTables(nil, nil)

if len(result) != len(defaultDynamoDBListTableMock.TableNames) {
t.Fatalf("unexpected dynamoDB tables count, got %d expected %d", len(result), len(defaultDynamoDBListTableMock.TableNames))
Expand All @@ -82,7 +88,7 @@ func TestDescribeDynamoDBTables(t *testing.T) {

dynamoDBManager := aws.NewDynamoDBManager(collector, &mockClient, nil, nil, metrics, "us-east-1")

_, err := dynamoDBManager.DescribeTables()
_, err := dynamoDBManager.DescribeTables(nil, nil)

if err == nil {
t.Fatalf("unexpected describe table error, return empty")
Expand All @@ -105,9 +111,9 @@ func TestDetectDynamoDB(t *testing.T) {
},
"Terms": aws.#Terms{
OnDemand: map[string]*aws.#OfferTerm{
"R6PXMNYCEDGZ2EYN.JRTCKXETXF": &aws.#OfferTerm{
"R6PXMNYCEDGZ2EYN.JRTCKXETXF": {
PriceDimensions: map[string]*aws.PriceRateCode{
"R6PXMNYCEDGZ2EYN.JRTCKXETXF.E63J5HTPNN": &aws.PriceRateCode{
"R6PXMNYCEDGZ2EYN.JRTCKXETXF.E63J5HTPNN": {
Unit: "USD",
PricePerUnit: aws.PriceCurrencyCode{
USD: "1.2",
Expand Down