[Bps-public-commit] app-aws-cloudwatch-monitor branch add-retry-arguments-to-script created. f712777ba29392225144292b5b4c3c275ede600b

BPS Git Server git at git.bestpractical.com
Thu Mar 28 11:53:52 UTC 2024


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "app-aws-cloudwatch-monitor".

The branch, add-retry-arguments-to-script has been created
        at  f712777ba29392225144292b5b4c3c275ede600b (commit)

- Log -----------------------------------------------------------------
commit f712777ba29392225144292b5b4c3c275ede600b
Author: Ronaldo Richieri <ronaldo at bestpractical.com>
Date:   Thu Mar 28 08:51:18 2024 -0300

    Add arguments to specify retries and retry delay
    
    The script already had a retry mechanism, but the number of retries and
    the delay between them were hardcoded. This commit adds command line
    arguments to specify the number of retries, initial delay and maximum
    delay between retries.

diff --git a/bin/aws-cloudwatch-monitor b/bin/aws-cloudwatch-monitor
index 0768834..15b2799 100755
--- a/bin/aws-cloudwatch-monitor
+++ b/bin/aws-cloudwatch-monitor
@@ -28,6 +28,9 @@ Getopt::Long::GetOptions(
     'check=s@',
     'from-cron',
     'verify',
+    'retries=s',
+    'initial-delay=s',
+    'max-delay=s',
     'verbose',
     'version' => sub { print "aws-cloudwatch-monitor version $VERSION\n"; exit 0 },
     'help',
@@ -60,6 +63,7 @@ aws-cloudwatch-monitor - collect and send metrics to AWS CloudWatch
 
  aws-cloudwatch-monitor [--check <module>]
                         [--from-cron] [--verify] [--verbose]
+                        [--retries <count>] [--initial-delay <seconds>] [--max-delay <seconds>]
                         [--version] [--help]
 
 =head1 DESCRIPTION
@@ -76,6 +80,28 @@ Defines the checks to run.
 
 Multiple C<--check> options may be defined and are run in the order they're passed.
 
+=item --retries <count>
+
+Specifies the number of times to retry sending metrics to CloudWatch if the initial attempt fails.
+It will retry if getting a 5xx response or a 400 with a message containing "Throttling".
+If not specified, the default is 2 (so it will attempt to send metrics 3 times)
+
+=item --initial-delay <seconds>
+
+Specifies the initial delay in seconds before retrying to send metrics to CloudWatch.
+If not specified, the default is 4.
+Each subsequent retry will double the delay up to the maximum delay.
+
+=item --max-delay <seconds>
+
+Specifies the maximum delay in seconds before retrying to send metrics to CloudWatch.
+If not specified, the default is 16.
+
+Note: if you want to have a constant delay between retries, set --initial-delay and --max-delay
+to the same value.
+
+    aws-cloudwatch-monitor --retries 5 --initial-delay 30 --max-delay 30
+
 =item --from-cron
 
 Specifies that this script is running from cron.
diff --git a/lib/App/AWS/CloudWatch/Monitor.pm b/lib/App/AWS/CloudWatch/Monitor.pm
index 2411d9e..bdc0cee 100644
--- a/lib/App/AWS/CloudWatch/Monitor.pm
+++ b/lib/App/AWS/CloudWatch/Monitor.pm
@@ -106,7 +106,7 @@ sub run {
 
     $opt->{'aws-access-key-id'} = $self->config->{aws}{aws_access_key_id};
     $opt->{'aws-secret-key'}    = $self->config->{aws}{aws_secret_access_key};
-    $opt->{retries}             = 2;
+    $opt->{retries}             ||= 2;
     $opt->{'user-agent'}        = CLIENT_NAME . "/$VERSION";
 
     my $response = App::AWS::CloudWatch::Monitor::CloudWatchClient::call_json( 'PutMetricData', $param, $opt );
diff --git a/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm b/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm
index e078f3d..426b212 100644
--- a/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm
+++ b/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm
@@ -808,8 +808,8 @@ sub call {
     print_out( "Payload: $payload",   $outfile ) if $verbose;
 
     # initial and max delay in seconds between retries
-    my $delay     = 4;
-    my $max_delay = 16;
+    my $delay     = $opts->{'initial-delay'} || 4;
+    my $max_delay = $opts->{'max-delay'} || 16;
 
     if ( defined( $opts->{'retries'} ) ) {
         $call_attempts += $opts->{'retries'};
-----------------------------------------------------------------------


hooks/post-receive
-- 
app-aws-cloudwatch-monitor


More information about the Bps-public-commit mailing list