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

Fix the # for ELB #76

Merged
merged 14 commits into from
Jul 6, 2020
82 changes: 47 additions & 35 deletions collector/aws/elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewELBManager(collector collector.CollectorDescriber, client ELBClientDescr
#Client: #,
region: region,
namespace: "AWS/ELB",
service#Code: "AmazonEC2",
service#Code: "AWSELB",
Name: fmt.Sprintf("%s_elb", ResourcePrefix),
}
}
Expand All @@ -73,25 +73,32 @@ func (el *ELBManager) Detect() ([]DetectedELB, error) {

detectedELB := []DetectedELB{}

instances, err := el.DescribeLoadbalancers(nil, nil)
#RegionPrefix, err := el.#Client.GetRegionPrefix(el.region)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"region": el.region,
}).Error("Could not get # region prefix")
el.updateErrorServiceStatus(err)
return detectedELB, err
}

el.collector.UpdateServiceStatus(collector.EventCollector{
ResourceName: el.Name,
Data: collector.EventStatusData{
Status: collector.EventError,
ErrorMessage: err.Error(),
},
})
instances, err := el.DescribeLoadbalancers(nil, nil)
if err != nil {
el.updateErrorServiceStatus(err)
return detectedELB, err
}

now := time.Now()

for _, instance := range instances {
log.WithField("name", *instance.LoadBalancerName).Debug("checking elb")

price, _ := el.#Client.GetPrice(el.Get#FilterInput(), "", el.region)
price, _ := el.#Client.GetPrice(el.Get#FilterInput([]*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("usagetype"),
Value: awsClient.String(fmt.Sprintf("%sLoadBalancerUsage", #RegionPrefix)),
},
}), "", el.region)

for _, metric := range el.metrics {

Expand Down Expand Up @@ -196,33 +203,27 @@ func (el *ELBManager) Detect() ([]DetectedELB, error) {
}

// Get#FilterInput prepare document elb # filter
func (el *ELBManager) Get#FilterInput() *#.GetProductsInput {
func (el *ELBManager) Get#FilterInput(extraFilters []*#.Filter) *#.GetProductsInput {
filters := []*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("termType"),
Value: awsClient.String("OnDemand"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("productFamily"),
Value: awsClient.String("Load Balancer"),
},
}

if extraFilters != nil {
filters = append(filters, extraFilters...)
}

return &#.GetProductsInput{
ServiceCode: &el.service#Code,
Filters: []*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("usagetype"),
Value: awsClient.String("LoadBalancerUsage"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("productFamily"),
Value: awsClient.String("Load Balancer-Application"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("TermType"),
Value: awsClient.String("OnDemand"),
},

{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("group"),
Value: awsClient.String("ELB:Balancer"),
},
},
Filters: filters,
}
}

Expand Down Expand Up @@ -251,3 +252,14 @@ func (el *ELBManager) DescribeLoadbalancers(marker *string, loadbalancers []*elb

return loadbalancers, nil
}

// updateErrorServiceStatus reports when elb can't collect data
func (el *ELBManager) updateErrorServiceStatus(err error) {
el.collector.UpdateServiceStatus(collector.EventCollector{
ResourceName: el.Name,
Data: collector.EventStatusData{
Status: collector.EventError,
ErrorMessage: err.Error(),
},
})
}
49 changes: 29 additions & 20 deletions collector/aws/elb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,41 @@ func TestDetectELB(t *testing.T) {

}
func TestDetectELBError(t *testing.T) {

collector := testutils.NewMockCollector()
mockCloudwatchClient := MockAWSCloudwatchClient{
responseMetricStatistics: defaultResponseMetricStatistics,
}
cloutwatchManager := aws.NewCloudWatchManager(&mockCloudwatchClient)
#Manager := aws.New#Manager(&default#Mock, "us-east-1")

mockClient := MockAWSELBClient{
err: errors.New(""),
}

elbManager := aws.NewELBManager(collector, &mockClient, cloutwatchManager, #Manager, defaultMetricConfig, "us-east-1")

response, _ := elbManager.Detect()

if len(response) != 0 {
t.Fatalf("unexpected elb detected, got %d expected %d", len(response), 0)
testCases := []struct {
region string
expectedPrefix string
expectedError error
}{
{"us-east-1", "", mockClient.err},
{"no-region-1", "", aws.ErrRegionNotFound},
}
for _, tc := range testCases {
collector := testutils.NewMockCollector()
mockCloudwatchClient := MockAWSCloudwatchClient{
responseMetricStatistics: defaultResponseMetricStatistics,
}
cloutwatchManager := aws.NewCloudWatchManager(&mockCloudwatchClient)
#Manager := aws.New#Manager(&default#Mock, "us-east-1")
elbManager := aws.NewELBManager(collector, &mockClient, cloutwatchManager, #Manager, defaultMetricConfig, tc.region)
response, err := elbManager.Detect()

if len(collector.Events) != 0 {
t.Fatalf("unexpected collector elb resources, got %d expected %d", len(collector.Events), 0)
}
if len(response) != 0 {
t.Fatalf("unexpected elb detected, got %d expected %d", len(response), 0)
}

if len(collector.EventsCollectionStatus) != 2 {
t.Fatalf("unexpected resource status events count, got %d expected %d", len(collector.EventsCollectionStatus), 2)
}
if len(collector.Events) != 0 {
t.Fatalf("unexpected collector elb resources, got %d expected %d", len(collector.Events), 0)
}

if len(collector.EventsCollectionStatus) != 2 {
t.Fatalf("unexpected resource status events count, got %d expected %d", len(collector.EventsCollectionStatus), 2)
}
if !errors.Is(err, tc.expectedError) {
t.Fatalf("unexpected error response, got: %v, expected: %v", err, tc.expectedError)
}
}
}
126 changes: 85 additions & 41 deletions collector/aws/elbv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,49 @@ type ELBV2Manager struct {
#Client *#Manager
metrics []config.MetricConfig
region string
namespace string
service#Code string
Name string
}

// DetectedELBV2 define the detected AWS ELB instances
// DetectedELBV2 defines the detected AWS ELB instances
type DetectedELBV2 struct {
Metric string
Region string
Type string
collector.PriceDetectedFields
}

// loadBalancerConfig defines loadbalancer's configuration of metrics and #
type loadBalancerConfig struct {
cloudWatchNamespace string
#filters []*#.Filter
}

// loadBalancersConfig defines loadbalancers configuration of metrics and # for
// Multiple types of LoadBalancers.
var loadBalancersConfig = map[string]loadBalancerConfig{
"application": {
cloudWatchNamespace: "AWS/ApplicationELB",
#filters: []*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("productFamily"),
Value: awsClient.String("Load Balancer-Application"),
},
},
},
"network": {
cloudWatchNamespace: "AWS/NetworkELB",
#filters: []*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("productFamily"),
Value: awsClient.String("Load Balancer-Network"),
},
},
},
}

// NewELBV2Manager implements AWS GO SDK
func NewELBV2Manager(collector collector.CollectorDescriber, client ELBV2ClientDescreptor, cloudWatchCLient *CloudwatchManager, # *#Manager, metrics []config.MetricConfig, region string) *ELBV2Manager {

Expand All @@ -51,8 +82,7 @@ func NewELBV2Manager(collector collector.CollectorDescriber, client ELBV2ClientD
metrics: metrics,
#Client: #,
region: region,
namespace: "AWS/ApplicationELB",
service#Code: "AmazonEC2",
service#Code: "AWSELB",
Name: fmt.Sprintf("%s_elbv2", ResourcePrefix),
}
}
Expand All @@ -74,26 +104,39 @@ func (el *ELBV2Manager) Detect() ([]DetectedELBV2, error) {

detectedELBV2 := []DetectedELBV2{}

#RegionPrefix, err := el.#Client.GetRegionPrefix(el.region)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"region": el.region,
}).Error("Could not get # region prefix")
el.updateErrorServiceStatus(err)
return detectedELBV2, err
}

instances, err := el.DescribeLoadbalancers(nil, nil)
if err != nil {
el.collector.UpdateServiceStatus(collector.EventCollector{
ResourceName: el.Name,
Data: collector.EventStatusData{
Status: collector.EventError,
ErrorMessage: err.Error(),
},
})
el.updateErrorServiceStatus(err)
return detectedELBV2, err
}

now := time.Now()

for _, instance := range instances {

log.WithField("name", *instance.LoadBalancerName).Debug("cheking elbV2")

price, _ := el.#Client.GetPrice(el.Get#FilterInput(), "", el.region)

var cloudWatchNameSpace string
var price float64
if loadBalancerConfig, found := loadBalancersConfig[*instance.Type]; found {
cloudWatchNameSpace = loadBalancerConfig.cloudWatchNamespace

log.WithField("name", *instance.LoadBalancerName).Debug("checking elbV2")

loadBalancerConfig.#filters = append(
loadBalancerConfig.#filters, &#.Filter{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("usagetype"),
Value: awsClient.String(fmt.Sprintf("%sLoadBalancerUsage", #RegionPrefix)),
})
price, _ = el.#Client.GetPrice(el.Get#FilterInput(loadBalancerConfig.#filters), "", el.region)
}
for _, metric := range el.metrics {

log.WithFields(log.Fields{
Expand All @@ -110,7 +153,7 @@ func (el *ELBV2Manager) Detect() ([]DetectedELBV2, error) {
elbv2Name := regx.ReplaceAllString(*instance.LoadBalancerArn, "")

metricInput := cloudwatch.GetMetricStatisticsInput{
Namespace: &el.namespace,
Namespace: &cloudWatchNameSpace,
MetricName: &metric.Description,
Period: &period,
StartTime: &metricEndTime,
Expand Down Expand Up @@ -169,6 +212,7 @@ func (el *ELBV2Manager) Detect() ([]DetectedELBV2, error) {
elbv2 := DetectedELBV2{
Region: el.region,
Metric: metric.Description,
Type: *instance.Type,
PriceDetectedFields: collector.PriceDetectedFields{
ResourceID: *instance.LoadBalancerName,
LaunchTime: *instance.CreatedTime,
Expand Down Expand Up @@ -203,33 +247,22 @@ func (el *ELBV2Manager) Detect() ([]DetectedELBV2, error) {
}

// Get#FilterInput prepare document elb # filter
func (el *ELBV2Manager) Get#FilterInput() *#.GetProductsInput {
func (el *ELBV2Manager) Get#FilterInput(extraFilters []*#.Filter) *#.GetProductsInput {
filters := []*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("termType"),
Value: awsClient.String("OnDemand"),
},
}

if extraFilters != nil {
filters = append(filters, extraFilters...)
}

return &#.GetProductsInput{
ServiceCode: &el.service#Code,
Filters: []*#.Filter{
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("usagetype"),
Value: awsClient.String("LoadBalancerUsage"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("productFamily"),
Value: awsClient.String("Load Balancer-Application"),
},
{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("TermType"),
Value: awsClient.String("OnDemand"),
},

{
Type: awsClient.String("TERM_MATCH"),
Field: awsClient.String("group"),
Value: awsClient.String("ELB:Balancer"),
},
},
Filters: filters,
}
}

Expand Down Expand Up @@ -258,3 +291,14 @@ func (el *ELBV2Manager) DescribeLoadbalancers(marker *string, loadbalancers []*e

return loadbalancers, nil
}

// updateErrorServiceStatus reports when elbv2 can't collect data
func (el *ELBV2Manager) updateErrorServiceStatus(err error) {
el.collector.UpdateServiceStatus(collector.EventCollector{
ResourceName: el.Name,
Data: collector.EventStatusData{
Status: collector.EventError,
ErrorMessage: err.Error(),
},
})
}
Loading