Skip to content

Commit

Permalink
Delay serialization until delivery in thread queue
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Aug 5, 2020
1 parent 8182fa5 commit b32525d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Changelog
| [#571](https://github.com/bugsnag/bugsnag-ruby/pull/571)
| [t-richards](https://github.com/t-richards)

* Move serialization of Reports onto the background thread when using the thread_queue delivery method
| [#623](https://github.com/bugsnag/bugsnag-ruby/pull/623)

## Fixes

* The `app_type` configuration option should no longer be overwritten by Bugsnag and integrations should set this more consistently
Expand Down
30 changes: 20 additions & 10 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,25 @@ def abort_reason(exception, auto_notify)
def deliver_notification(report)
configuration.info("Notifying #{configuration.notify_endpoint} of #{report.exceptions.last[:errorClass]}")

payload = report_to_json(report)
options = {:headers => report.headers}

Bugsnag::Delivery[configuration.delivery_method].deliver(
configuration.notify_endpoint,
payload,
configuration,
options
)
options = { headers: report.headers }

delivery_method = Bugsnag::Delivery[configuration.delivery_method]

if delivery_method.respond_to?(:serialize_and_deliver)
delivery_method.serialize_and_deliver(
configuration.notify_endpoint,
proc { report_to_json(report) },
configuration,
options
)
else
delivery_method.deliver(
configuration.notify_endpoint,
report_to_json(report),
configuration,
options
)
end

leave_breadcrumb(
report.summary[:error_class],
Expand Down Expand Up @@ -353,7 +363,7 @@ def check_endpoint_setup
# encoding errors and redacting metadata according to "meta_data_filters"
#
# @param report [Report]
# @return string
# @return [String]
def report_to_json(report)
cleaned = cleaner.clean_object(report.as_json)
trimmed = Bugsnag::Helpers.trim_if_needed(cleaned)
Expand Down
21 changes: 18 additions & 3 deletions lib/bugsnag/delivery/thread_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ class ThreadQueue < Synchronous

class << self
##
# Queues a given payload to be delivered asynchronously.
def deliver(url, body, configuration, options={})
# Queues a given payload to be delivered asynchronously
#
# @param url [String]
# @param get_payload [Proc] A Proc that will return the payload.
# @param configuration [Bugsnag::Configuration]
# @param options [Hash]
# @return [void]
def serialize_and_deliver(url, get_payload, configuration, options={})
@configuration = configuration

start_once!
Expand All @@ -21,7 +27,16 @@ def deliver(url, body, configuration, options={})
end

# Add delivery to the worker thread
@queue.push proc { super(url, body, configuration, options) }
@queue.push(proc do
begin
payload = get_payload.call
rescue StandardError => e
configuration.warn("Notification to #{url} failed, #{e.inspect}")
configuration.warn(e.backtrace)
end

Synchronous.deliver(url, payload, configuration, options) unless payload.nil?
end)
end

private
Expand Down

0 comments on commit b32525d

Please sign in to comment.