Skip to content

Commit

Permalink
Merge pull request #7 from amaabca/ado4905-cloudwatchlogs-rate-limit
Browse files Browse the repository at this point in the history
Version 0.5.0 | CloudWatchLogs API Rate Limits
  • Loading branch information
Jesse Doyle authored Oct 23, 2019
2 parents d3790fa + 005f692 commit d731952
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
30 changes: 29 additions & 1 deletion spec/subscribe/lambda/function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:tags) { { 'test' => 'true' } }
let(:destination_arn) { ENV.fetch('DESTINATION_ARN') }
let(:lambda) { described_class.build_lambda_client(stub_responses: true) }
let(:cloudwatch) { Aws::CloudWatchLogs::Client.new(stub_responses: true) }
let(:cloudwatch) { described_class.build_cloudwatchlogs_client(stub_responses: true) }
let(:tags_stub) { lambda.stub_data(:list_tags, tags: tags) }
let(:function_one_arn) { 'arn:aws:lambda:us-west-2:123456789012:function:One' }
let(:function_two_arn) { 'arn:aws:lambda:us-west-2:123456789012:function:Two' }
Expand Down Expand Up @@ -263,5 +263,33 @@
expect(value.fetch(:changed)).to eq(['two'])
expect(value.fetch(:skipped)).to eq(['one'])
end

context 'when hitting an API rate limit for DescribeSubscriptionFilters' do
before(:each) do
cloudwatch.handlers.add(Aws::Plugins::RetryErrors::Handler)
cloudwatch.stub_responses(
:describe_subscription_filters,
[
'ThrottlingException',
'ThrottlingException',
'ThrottlingException',
log_group_one_subscription
]
)
end

it 'sleeps with exponential backoff' do
# 1 -> throttle
# wait 1s
# 2 -> throttle
# wait 2s
# 3 -> throttle
# wait 2s
# 4 -> success
# >= 5s total
time = Benchmark.measure { described_class.subscribe_all!(lambda, cloudwatch) }
expect(time.real >= 5).to be true
end
end
end
end
22 changes: 12 additions & 10 deletions subscribe/lambda/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Function
attr_accessor :name, :arn, :tags, :lambda, :cloudwatch

class << self
def all(lambda = build_lambda_client, cloudwatch = Aws::CloudWatchLogs::Client.new)
def all(lambda = build_lambda_client, cloudwatch = build_cloudwatchlogs_client)
all_functions(lambda).map do |function|
new(
name: function.function_name,
Expand All @@ -19,7 +19,7 @@ def all(lambda = build_lambda_client, cloudwatch = Aws::CloudWatchLogs::Client.n
end
end

def subscribe_all!(lambda = build_lambda_client, cloudwatch = Aws::CloudWatchLogs::Client.new)
def subscribe_all!(lambda = build_lambda_client, cloudwatch = build_cloudwatchlogs_client)
data = {
timestamp: Time.now.iso8601,
event: 'log.info',
Expand All @@ -36,15 +36,17 @@ def subscribe_all!(lambda = build_lambda_client, cloudwatch = Aws::CloudWatchLog
end
end

def build_lambda_client(opts = {})
def retry_opts
# retries: 1s -> 2s -> 2s -> fail
Aws::Lambda::Client.new(
{
retry_limit: 3,
retry_base_delay: 1,
retry_max_delay: 2
}.merge(opts)
)
{ retry_limit: 3, retry_base_delay: 1, retry_max_delay: 2 }
end

def build_lambda_client(opts = {})
Aws::Lambda::Client.new(retry_opts.merge(opts))
end

def build_cloudwatchlogs_client(opts = {})
Aws::CloudWatchLogs::Client.new(retry_opts.merge(opts))
end

private
Expand Down
2 changes: 1 addition & 1 deletion template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Metadata:
- forwarder
- lambda
HomePageUrl: https://github.com/amaabca/cloudwatch_loggly
SemanticVersion: 0.4.0
SemanticVersion: 0.5.0
SourceCodeUrl: https://github.com/amaabca/cloudwatch_loggly

Parameters:
Expand Down

0 comments on commit d731952

Please sign in to comment.