Created
August 26, 2020 21:04
-
-
Save azakordonets/e19e6e72a113b336164c36ae9a4ca34f to your computer and use it in GitHub Desktop.
Cloudwatch client with retry for a blog post
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
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