We were writing a script to pull error events from the api, to parse some associated information so that we could backfill some data that needed fixed. Upon using the ruby gem, all was going well until I realized that Bugsnag::Api.error_events does not gracefully handle being rate-limited, which ultimately came down to the paginate method.
Our solution was to override that method on Bugsnag::Api::Client, but it's something that I would expect the client library to handle, or at least call out in the docs.
module Bugsnag::Client::GracefulRateLimit
def paginate(url, options = {}, &block)
opts = parse_query_and_convenience_headers(options.dup)
if configuration.auto_paginate || configuration.per_page
opts[:query][:per_page] ||= configuration.per_page || (configuration.auto_paginate ? 100 : nil)
end
data = request(:get, url, opts)
if configuration.auto_paginate
while @last_response.rels[:next]
begin
@last_response = @last_response.rels[:next].get
rescue Bugsnag::Api::RateLimitExceeded => e
sleep 60 and redo
else
if block_given?
yield(data, @last_response)
else
data.concat(@last_response.data) if @last_response.data.is_a?(Array)
end
end
end
end
data
end
end
Bugsnag::Api::Client.prepend Bugsnag::Client::GracefulRateLimit
We were writing a script to pull error events from the api, to parse some associated information so that we could backfill some data that needed fixed. Upon using the ruby gem, all was going well until I realized that
Bugsnag::Api.error_eventsdoes not gracefully handle being rate-limited, which ultimately came down to the paginate method.Our solution was to override that method on
Bugsnag::Api::Client, but it's something that I would expect the client library to handle, or at least call out in the docs.