Skip to content

Commit b157ed3

Browse files
committed
Adds support for a retry count and raising
Adds two new configuration settings: * Configure how many times raygun4ruby should retry sending the error to the Raygun servers using `error_report_max_attempts` (defaults to 1) * Configure whether or not raygun4ruby should re-raise an exception if the send fails using `raise_on_failed_error_report` (defaults to false)
1 parent 7ee2ca5 commit b157ed3

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

Diff for: lib/raygun.rb

+17-10
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ def configured?
6060
!!configuration.api_key
6161
end
6262

63-
def track_exception(exception_instance, env = {}, user = nil, retry_count = 1)
63+
def track_exception(exception_instance, env = {}, user = nil, retries_remaining = configuration.error_report_max_attempts - 1)
6464
log('tracking exception')
6565

6666
exception_instance.set_backtrace(caller) if exception_instance.is_a?(Exception) && exception_instance.backtrace.nil?
6767

6868
result = if configuration.send_in_background
69-
track_exception_async(exception_instance, env, user, retry_count)
69+
track_exception_async(exception_instance, env, user, retries_remaining)
7070
else
71-
track_exception_sync(exception_instance, env, user, retry_count)
71+
track_exception_sync(exception_instance, env, user, retries_remaining)
7272
end
7373

7474
result
@@ -128,10 +128,10 @@ def wait_for_futures
128128

129129
private
130130

131-
def track_exception_async(exception_instance, env, user, retry_count)
131+
def track_exception_async(exception_instance, env, user, retries_remaining)
132132
env[:rg_breadcrumb_store] = Raygun::Breadcrumbs::Store.take_until_size(Client::MAX_BREADCRUMBS_SIZE) if Raygun::Breadcrumbs::Store.any?
133133

134-
future = Concurrent::Future.execute { track_exception_sync(exception_instance, env, user, retry_count) }
134+
future = Concurrent::Future.execute { track_exception_sync(exception_instance, env, user, retries_remaining) }
135135
future.add_observer(lambda do |_, value, reason|
136136
if value == nil || !value.responds_to?(:response) || value.response.code != "202"
137137
log("unexpected response from Raygun, could indicate error: #{value.inspect}")
@@ -143,7 +143,7 @@ def track_exception_async(exception_instance, env, user, retry_count)
143143
future
144144
end
145145

146-
def track_exception_sync(exception_instance, env, user, retry_count)
146+
def track_exception_sync(exception_instance, env, user, retries_remaining)
147147
if should_report?(exception_instance)
148148
log('attempting to send exception')
149149
resp = Client.new.track_exception(exception_instance, env, user)
@@ -158,18 +158,25 @@ def track_exception_sync(exception_instance, env, user, retry_count)
158158
failsafe_log("Problem reporting exception to Raygun: #{e.class}: #{e.message}\n\n#{e.backtrace.join("\n")}")
159159
end
160160

161-
if retry_count > 0
161+
if retries_remaining > 0
162162
new_exception = e.exception("raygun4ruby encountered an exception processing your exception")
163163
new_exception.set_backtrace(e.backtrace)
164164

165165
env[:custom_data] ||= {}
166-
env[:custom_data].merge!(original_stacktrace: exception_instance.backtrace)
166+
env[:custom_data].merge!(original_stacktrace: exception_instance.backtrace, retries_remaining: retries_remaining)
167167

168168
::Raygun::Breadcrumbs::Store.clear
169169

170-
track_exception(new_exception, env, user, retry_count - 1)
170+
track_exception(new_exception, env, user, retries_remaining - 1)
171171
else
172-
raise e
172+
if configuration.raise_on_failed_error_report
173+
raise e
174+
else
175+
retries = configuration.error_report_max_attempts - retries_remaining
176+
if configuration.failsafe_logger
177+
failsafe_log("Gave up reporting exception to Raygun after #{retries} #{retries == 1 ? "retry" : "retries"}: #{e.class}: #{e.message}\n\n#{e.backtrace.join("\n")}")
178+
end
179+
end
173180
end
174181
end
175182

Diff for: lib/raygun/configuration.rb

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def self.proc_config_option(name)
8686
# How long to wait for the POST request to the API server before timing out
8787
config_option :error_report_send_timeout
8888

89+
# How many times to try sending to Raygun before giving up
90+
config_option :error_report_max_attempts
91+
92+
# Whether or not we should raise an exception if the error reporting fails
93+
config_option :raise_on_failed_error_report
94+
8995
# Should we register an error handler with [Rails' built in API](https://edgeguides.rubyonrails.org/error_reporting.html)
9096
config_option :register_rails_error_handler
9197

@@ -147,6 +153,8 @@ def initialize
147153
record_raw_data: false,
148154
send_in_background: false,
149155
error_report_send_timeout: 10,
156+
error_report_max_attempts: 1,
157+
raise_on_failed_error_report: false,
150158
track_retried_sidekiq_jobs: true
151159
)
152160
end

Diff for: test/unit/raygun_test.rb

+39
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,43 @@ def test_reset_configuration
103103
assert_equal Raygun.default_configuration.api_url, Raygun.configuration.api_url
104104
refute_equal original_api_url, Raygun.configuration.api_url
105105
end
106+
107+
def test_retries
108+
failsafe_logger = FakeLogger.new
109+
Raygun.setup do |c|
110+
c.error_report_max_attempts = 3
111+
c.failsafe_logger = failsafe_logger
112+
end
113+
114+
WebMock.reset!
115+
report_request = stub_request(:post, "https://api.raygun.com/entries").to_timeout
116+
117+
error = StandardError.new
118+
Raygun.track_exception(error)
119+
120+
assert_requested report_request, times: 3
121+
122+
assert_match(/Gave up reporting exception to Raygun after 3 retries/, failsafe_logger.get)
123+
ensure
124+
Raygun.reset_configuration
125+
end
126+
127+
def test_raising_on_retry_failure
128+
Raygun.setup do |c|
129+
c.error_report_max_attempts = 1
130+
c.raise_on_failed_error_report = true
131+
end
132+
133+
report_request = stub_request(:post, "https://api.raygun.com/entries").to_timeout
134+
135+
error = StandardError.new
136+
137+
assert_raises(StandardError) do
138+
Raygun.track_exception(error)
139+
assert_requested report_request
140+
end
141+
142+
ensure
143+
Raygun.reset_configuration
144+
end
106145
end

Diff for: test/unit/sidekiq_failure_test.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ def test_failure_backend_unwraps_retries
4848
raise Sidekiq::JobRetry::Handled
4949
end
5050

51-
rescue Sidekiq::JobRetry::Handled => e
51+
rescue Sidekiq::JobRetry::Handled => e
5252

53-
response = Raygun::SidekiqReporter.call(
54-
e,
55-
{ sidekick_name: "robin" },
56-
{} # config
57-
)
53+
response = Raygun::SidekiqReporter.call(
54+
e,
55+
{ sidekick_name: "robin" },
56+
{} # config
57+
)
5858

59-
assert_requested unwrapped_stub
60-
assert response && response.success?, "Expected success, got #{response.class}: #{response.inspect}"
59+
assert_requested unwrapped_stub
60+
assert response && response.success?, "Expected success, got #{response.class}: #{response.inspect}"
6161
end
6262

6363
def test_failured_backend_ignores_retries_if_configured
@@ -69,12 +69,12 @@ def test_failured_backend_ignores_retries_if_configured
6969
raise Sidekiq::JobRetry::Handled
7070
end
7171

72-
rescue Sidekiq::JobRetry::Handled => e
72+
rescue Sidekiq::JobRetry::Handled => e
7373

74-
refute Raygun::SidekiqReporter.call(e,
75-
{ sidekick_name: "robin" },
76-
{} # config
77-
)
74+
refute Raygun::SidekiqReporter.call(e,
75+
{ sidekick_name: "robin" },
76+
{} # config
77+
)
7878
ensure
7979
Raygun.configuration.track_retried_sidekiq_jobs = true
8080
end

0 commit comments

Comments
 (0)