-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/metrics/providers: add AWS CloudWatch metrics provider
- Loading branch information
Showing
22 changed files
with
348 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package providers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/cloudwatch" | ||
|
||
flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1" | ||
) | ||
|
||
const ( | ||
cloudWatchMaxRetries = 3 | ||
cloudWatchStartDeltaMultiplierOnMetricInterval = 10 | ||
) | ||
|
||
type cloudWatchProvider struct { | ||
client cloudWatchClient | ||
startDelta time.Duration | ||
} | ||
|
||
// for the testing purpose | ||
type cloudWatchClient interface { | ||
GetMetricData(input *cloudwatch.GetMetricDataInput) (*cloudwatch.GetMetricDataOutput, error) | ||
} | ||
|
||
func newCloudWatchProvider(metricInterval string, provider flaggerv1.MetricTemplateProvider) (*cloudWatchProvider, error) { | ||
region := strings.TrimLeft(provider.Address, "monitoring.") | ||
region = strings.TrimRight(region, ".amazonaws.com") | ||
sess, err := session.NewSession( | ||
aws.NewConfig(). | ||
WithRegion(region). | ||
WithMaxRetries(cloudWatchMaxRetries). | ||
WithEndpoint(provider.Address), | ||
) | ||
|
||
md, err := time.ParseDuration(metricInterval) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing metric interval: %s", err.Error()) | ||
} | ||
|
||
return &cloudWatchProvider{ | ||
client: cloudwatch.New(sess), | ||
startDelta: cloudWatchStartDeltaMultiplierOnMetricInterval * md, | ||
}, err | ||
} | ||
|
||
func (p *cloudWatchProvider) RunQuery(query string) (float64, error) { | ||
var cq []*cloudwatch.MetricDataQuery | ||
if err := json.Unmarshal([]byte(query), &cq); err != nil { | ||
return 0, fmt.Errorf("error unmarshaling query: %s", err.Error()) | ||
} | ||
|
||
end := time.Now() | ||
start := end.Add(-p.startDelta) | ||
res, err := p.client.GetMetricData(&cloudwatch.GetMetricDataInput{ | ||
EndTime: aws.Time(end), | ||
MaxDatapoints: aws.Int64(20), | ||
StartTime: aws.Time(start), | ||
MetricDataQueries: cq, | ||
}) | ||
|
||
if err != nil { | ||
return 0, fmt.Errorf("error requesting cloudwatch: %s", err.Error()) | ||
} | ||
|
||
mr := res.MetricDataResults | ||
if len(mr) < 1 { | ||
return 0, fmt.Errorf("no values found in response: %s", res.String()) | ||
} | ||
|
||
vs := res.MetricDataResults[0].Values | ||
if len(vs) < 1 { | ||
return 0, fmt.Errorf("no values found in response: %s", res.String()) | ||
} | ||
|
||
return aws.Float64Value(vs[0]), nil | ||
} | ||
|
||
func (p *cloudWatchProvider) IsOnline() (bool, error) { | ||
_, err := p.client.GetMetricData(&cloudwatch.GetMetricDataInput{ | ||
EndTime: aws.Time(time.Time{}), | ||
MetricDataQueries: []*cloudwatch.MetricDataQuery{}, | ||
StartTime: aws.Time(time.Time{}), | ||
}) | ||
|
||
if err == nil { | ||
return true, nil | ||
} | ||
|
||
ae, ok := err.(awserr.RequestFailure) | ||
if !ok { | ||
return false, fmt.Errorf("unexpected error: %v", err) | ||
} else if ae.StatusCode() != http.StatusBadRequest { | ||
return false, fmt.Errorf("unexpected status code: %v", ae) | ||
} | ||
return true, nil | ||
} |
Oops, something went wrong.