Skip to content

Instantly share code, notes, and snippets.

@azakordonets
Created August 26, 2020 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azakordonets/e19e6e72a113b336164c36ae9a4ca34f to your computer and use it in GitHub Desktop.
Save azakordonets/e19e6e72a113b336164c36ae9a4ca34f to your computer and use it in GitHub Desktop.
Cloudwatch client with retry for a blog post
package aws
// Error implements AWS Error interface
type Error struct {
error
ErrorCode string
ErrorMessage string
OriginalError error
}
// Code Returns the short phrase depicting the classification of the error.
func (a Error) Code() string {
return a.ErrorCode
}
// Message Returns the error details message.
func (a Error) Message() string {
return a.ErrorMessage
}
// OrigErr Returns the original error if one was set. Nil is returned if not set.
func (a Error) OrigErr() error {
return a.OriginalError
}
package aws
func isThrottlingError(err error) bool {
if aerr, ok := err.(awserr.Error); ok {
return aerr.Code() == "ThrottlingException"
}
return false
}
func describeLogGroups(cloudwatchClient cloudwatchlogsiface.CloudWatchLogsAPI, request cloudwatchlogs.DescribeLogGroupsInput, numberOfThrottlingRetries int) (*cloudwatchlogs.DescribeLogGroupsOutput, error) {
res, err := cloudwatchClient.DescribeLogGroupsRequest(&request).Send()
if err != nil {
if isThrottlingError(err) && numberOfThrottlingRetries != 10 {
sleep := time.Second
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
return describeLogGroups(cloudwatchClient, request, numberOfThrottlingRetries+1)
}
return nil, err
}
return res, nil
}
func describeSubscriptionFilter(cloudwatchClient cloudwatchlogsiface.CloudWatchLogsAPI, request cloudwatchlogs.DescribeSubscriptionFiltersInput, numberOfThrottlingRetries int) (*cloudwatchlogs.DescribeSubscriptionFiltersOutput, error) {
res, err := cloudwatchClient.DescribeSubscriptionFiltersRequest(&request).Send()
if err != nil {
if isThrottlingError(err) && numberOfThrottlingRetries != 10 {
sleep := time.Second
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
return describeSubscriptionFilter(cloudwatchClient, request, numberOfThrottlingRetries+1)
}
return nil, err
}
return res, nil
}
func putSubscriptionFilter(cloudwatchClient cloudwatchlogsiface.CloudWatchLogsAPI, logGroupName *string, destArn string, roleName string, numberOfThrottlingRetries int) error {
var input cloudwatchlogs.PutSubscriptionFilterInput
if roleName != "" {
input = cloudwatchlogs.PutSubscriptionFilterInput{
LogGroupName: logGroupName,
DestinationArn: aws.String(destArn),
FilterName: aws.String("Datadog"),
FilterPattern: aws.String(""),
RoleArn: aws.String(roleName),
}
} else {
input = cloudwatchlogs.PutSubscriptionFilterInput{
LogGroupName: logGroupName,
DestinationArn: aws.String(destArn),
FilterName: aws.String("Datadog"),
FilterPattern: aws.String(""),
}
}
_, err := cloudwatchClient.PutSubscriptionFilterRequest(&input).Send()
if err != nil {
if isThrottlingError(err) && numberOfThrottlingRetries != 10 {
sleep := time.Second
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
return putSubscriptionFilter(cloudwatchClient, logGroupName, destArn, roleName, numberOfThrottlingRetries+1)
}
return err
}
return nil
}
func deleteSubscriptionFilter(cloudwatchClient cloudwatchlogsiface.CloudWatchLogsAPI, logGroupName *string, filterName string, numberOfThrottlingRetries int) error {
_, err := cloudwatchClient.DeleteSubscriptionFilterRequest(&cloudwatchlogs.DeleteSubscriptionFilterInput{
LogGroupName: logGroupName,
FilterName: aws.String(filterName),
}).Send()
if err != nil {
if isThrottlingError(err) && numberOfThrottlingRetries != 10 {
sleep := time.Second
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
return deleteSubscriptionFilter(cloudwatchClient, logGroupName, filterName, numberOfThrottlingRetries+1)
}
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment