Skip to content

Commit

Permalink
Merge pull request #176 from nikz/pass-breadcrumb-context
Browse files Browse the repository at this point in the history
Pass breadcrumb context through to Future
  • Loading branch information
nikz committed May 2, 2024
2 parents 94a8bb6 + f233b86 commit 453e79f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
15 changes: 13 additions & 2 deletions lib/raygun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class << self
# Configuration Object (instance of Raygun::Configuration)
attr_writer :configuration

# List of futures that are currently running
@@active_futures = []

def setup
yield(configuration)

Expand Down Expand Up @@ -116,15 +119,23 @@ def deprecation_warning(message)
end
end

def wait_for_futures
@@active_futures.each(&:value)
end

private

def track_exception_async(*args)
future = Concurrent::Future.execute { track_exception_sync(*args) }
def track_exception_async(exception_instance, env, user, retry_count)
env[:rg_breadcrumb_store] = Raygun::Breadcrumbs::Store.take_until_size(Client::MAX_BREADCRUMBS_SIZE) if Raygun::Breadcrumbs::Store.any?

future = Concurrent::Future.execute { track_exception_sync(exception_instance, env, user, retry_count) }
future.add_observer(lambda do |_, value, reason|
if value == nil || !value.responds_to?(:response) || value.response.code != "202"
log("unexpected response from Raygun, could indicate error: #{value.inspect}")
end
@@active_futures.delete(future)
end, :call)
@@active_futures << future
end

def track_exception_sync(exception_instance, env, user, retry_count)
Expand Down
5 changes: 3 additions & 2 deletions lib/raygun/breadcrumbs/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Raygun
module Breadcrumbs
class Store
def self.initialize
Thread.current[:breadcrumbs] ||= []
def self.initialize(with: [])
Thread.current[:breadcrumbs] ||= with
end

def self.clear
Expand Down Expand Up @@ -71,6 +71,7 @@ def self.should_record?(crumb)
levels = Raygun::Breadcrumbs::BREADCRUMB_LEVELS

active_level = levels.index(Raygun.configuration.breadcrumb_level)
puts crumb.inspect if crumb.is_a?(Array)
crumb_level = levels.index(crumb.level) || -1

discard = crumb_level < active_level
Expand Down
10 changes: 9 additions & 1 deletion lib/raygun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ def build_payload_hash(exception_instance, env = {}, user = nil)
utcOffset: Time.now.utc_offset / 3600
}
}
store = ::Raygun::Breadcrumbs::Store

# If we have breadcrumbs passed to us as context from another thread, then include them
# Otherwise, use the default store (which is thread-local)
store = if env.key?(:rg_breadcrumb_store)
::Raygun::Breadcrumbs::Store.initialize(with: env.delete(:rg_breadcrumb_store))
else
::Raygun::Breadcrumbs::Store
end

error_details[:breadcrumbs] = store.take_until_size(MAX_BREADCRUMBS_SIZE).map(&:build_payload) if store.any?

Raygun.log('set details and breadcrumbs')
Expand Down
47 changes: 0 additions & 47 deletions spec/raygun/raygun_spec.rb

This file was deleted.

52 changes: 52 additions & 0 deletions test/unit/raygun_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,56 @@ def test_should_report_logs_ignored_exceptions_when_debug_is_on

assert logger.get =~ /skipping reporting of.*Exception.*/, "ignored exception was not logged"
end

class BackgroundSendTest < Raygun::UnitTest
def setup
@failsafe_logger = FakeLogger.new
Raygun.setup do |c|
c.silence_reporting = false
c.send_in_background = true
c.api_url = "http://example.api"
c.api_key = "foo"
c.debug = false
c.failsafe_logger = @failsafe_logger
end
end

def test_breadcrumb_context_passed
Raygun::Breadcrumbs::Store.initialize
Raygun.record_breadcrumb(message: "mmm crumbly")
assert Raygun::Breadcrumbs::Store.any?

stub_request(:post, "http://example.api/entries")
.with(body: hash_including(breadcrumbs: [ hash_including(message: "mmm crumbly") ]))
.to_return(status: 202)

Raygun.track_exception(StandardError.new)
Raygun.wait_for_futures
ensure
Raygun::Breadcrumbs::Store.clear
end

def test_failsafe_reported_on_timeout
stub_request(:post, "http://example.api/entries").to_timeout

error = StandardError.new

Raygun.track_exception(error)

Raygun.wait_for_futures
assert_match(/Problem reporting exception to Raygun/, @failsafe_logger.get)
end

end

def test_reset_configuration
Raygun.setup do |c|
c.api_url = "http://test.api"
end

original_api_url = Raygun.configuration.api_url
Raygun.reset_configuration
assert_equal Raygun.default_configuration.api_url, Raygun.configuration.api_url
refute_equal original_api_url, Raygun.configuration.api_url
end
end

0 comments on commit 453e79f

Please sign in to comment.