Skip to content

Commit

Permalink
Merge pull request #129 from MindscapeHQ/tl/timeout-post-call
Browse files Browse the repository at this point in the history
Timeout network request to send crash reports to API
  • Loading branch information
UberMouse authored Feb 19, 2018
2 parents deea6a0 + d8fac63 commit 2244996
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.7.0 (19/02/2018)

Features
- Add configuration option to control network timeouts when sending error reports, default value is 10 seconds ([#129](https://github.com/MindscapeHQ/raygun4ruby/pull/129))

## 2.6.0 (25/10/2017)

Features
Expand Down
2 changes: 0 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ namespace :test do
desc "Test the basics of the adapter"
Rake::TestTask.new(:units) do |t|
t.test_files = FileList["test/unit/*_test.rb", "specs/**/*_spec.rb"]
t.verbose = true
end

desc "Run a test against the live API"
Rake::TestTask.new(:integration) do |t|
t.test_files = FileList["test/integration/*_test.rb"]
t.verbose = true
end

end
Expand Down
11 changes: 9 additions & 2 deletions lib/raygun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def default_configuration
configuration.defaults
end

def reset_configuration
@configuration = Configuration.new
end

def configured?
!!configuration.api_key
end
Expand Down Expand Up @@ -118,7 +122,7 @@ def deprecation_warning(message)
def track_exception_async(*args)
future = Concurrent::Future.execute { track_exception_sync(*args) }
future.add_observer(lambda do |_, value, reason|
if value == nil || value.response.code != "202"
if value == nil || !value.responds_to?(:response) || value.response.code != "202"
log("unexpected response from Raygun, could indicate error: #{value.inspect}")
end
end, :call)
Expand All @@ -127,7 +131,10 @@ def track_exception_async(*args)
def track_exception_sync(exception_instance, env, user, retry_count)
if should_report?(exception_instance)
log('attempting to send exception')
Client.new.track_exception(exception_instance, env, user)
resp = Client.new.track_exception(exception_instance, env, user)
log('sent payload to api')

resp
end
rescue Exception => e
log('error sending exception to raygun, see failsafe logger for more information')
Expand Down
9 changes: 8 additions & 1 deletion lib/raygun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize

enable_http_proxy if Raygun.configuration.proxy_settings[:address]
self.class.base_uri Raygun.configuration.api_url
self.class.default_timeout(Raygun.configuration.error_report_send_timeout)
end

def require_api_key
Expand Down Expand Up @@ -242,7 +243,13 @@ def build_payload_hash(exception_instance, env = {}, user = nil)
def create_entry(payload_hash)
Raygun.log('sending payload to api')

self.class.post("/entries", verify_peer: true, verify: true, headers: @headers, body: JSON.generate(payload_hash))
self.class.post(
"/entries",
verify_peer: true,
verify: true,
headers: @headers,
body: JSON.generate(payload_hash),
)
end

def filter_params_with_blacklist(params_hash = {}, extra_filter_keys = nil)
Expand Down
6 changes: 5 additions & 1 deletion lib/raygun/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def self.proc_config_option(name)
# Should the exceptions to Raygun be sent asynchronously?
config_option :send_in_background

# How long to wait for the POST request to the API server before timing out
config_option :error_report_send_timeout

# Exception classes to ignore by default
IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
'ActionController::RoutingError',
Expand Down Expand Up @@ -129,7 +132,8 @@ def initialize
api_url: 'https://api.raygun.io/',
breadcrumb_level: :info,
record_raw_data: false,
send_in_background: false
send_in_background: false,
error_report_send_timeout: 10
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/raygun/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Raygun
VERSION = "2.6.0"
VERSION = "2.7.0"
end
4 changes: 2 additions & 2 deletions raygun4ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "concurrent-ruby"

spec.add_development_dependency "bundler", ">= 1.1"
spec.add_development_dependency "rake", "0.9.6"
spec.add_development_dependency "fakeweb", ["~> 1.3"]
spec.add_development_dependency "rake", "~> 11"
spec.add_development_dependency "timecop"
spec.add_development_dependency "minitest", "~> 4.2"
spec.add_development_dependency "redis-namespace", ">= 1.3.1"
spec.add_development_dependency "resque"
spec.add_development_dependency "sidekiq", [">= 3", "< 3.2.2"]
spec.add_development_dependency "mocha"
spec.add_development_dependency "pry"
spec.add_development_dependency "webmock"
end
50 changes: 50 additions & 0 deletions specs/raygun/raygun_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require_relative '../spec_helper'
require 'stringio'

describe Raygun do
let(:failsafe_logger) { FakeLogger.new }

describe '#track_exception' do
context 'send in background' do
before do
Raygun.setup do |c|
c.send_in_background = true
c.api_url = 'http://example.api'
c.api_key = 'foo'
c.debug = true
c.failsafe_logger = failsafe_logger
end
end

context 'request times out' do
before do
stub_request(:post, 'http://example.api/entries').to_timeout
end

it 'logs the failure to the failsafe logger' do
error = StandardError.new

Raygun.track_exception(error)

# Occasionally doesn't write to the failsafe logger, add small timeout to add some safety
sleep 0.1
failsafe_logger.get.must_match /Problem reporting exception to Raygun/
end
end
end
end

describe '#reset_configuration' do
it 'clears any customized configuration options' do
Raygun.setup do |c|
c.api_url = 'http://test.api'
end

Raygun.configuration.api_url.must_equal 'http://test.api'

Raygun.reset_configuration

Raygun.configuration.api_url.must_equal Raygun.default_configuration.api_url
end
end
end
22 changes: 21 additions & 1 deletion specs/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@
require_relative "../lib/raygun.rb"
require "minitest/autorun"
require "minitest/pride"
require "fakeweb"
require "timecop"
require "mocha/mini_test"
require "webmock/minitest"
require 'stringio'

alias :context :describe

class FakeLogger
def initialize
@logger = StringIO.new
end

def info(message)
@logger.write(message)
end

def reset
@logger.string = ""
end

def get
@logger.string
end
end

12 changes: 4 additions & 8 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require_relative "../lib/raygun.rb"
require "minitest/autorun"
require "minitest/pride"
require "fakeweb"
require "timecop"
require "mocha/mini_test"
require 'stringio'
Expand Down Expand Up @@ -47,20 +46,17 @@ def teardown
class Raygun::UnitTest < MiniTest::Unit::TestCase

def setup
FakeWeb.allow_net_connect = false
Raygun.configuration.api_key = "test api key"
end

def fake_successful_entry
FakeWeb.register_uri(:post, "https://api.raygun.io/entries", body: "", status: 202)
end

def teardown
FakeWeb.clean_registry
FakeWeb.allow_net_connect = true
reset_configuration
end

def fake_successful_entry
stub_request(:post, 'https://api.raygun.io/entries').to_return(status: 202)
end

def reset_configuration
Raygun.configuration = Raygun::Configuration.new
end
Expand Down
12 changes: 12 additions & 0 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def setup
end
end

def teardown
Raygun.reset_configuration
end

def test_setting_api_key_and_version
assert_equal 9.9, Raygun.configuration.version
assert_equal "a test api key", Raygun.configuration.api_key
Expand Down Expand Up @@ -179,4 +183,12 @@ def test_record_raw_data_default
def test_send_in_background_default
assert_equal false, Raygun.configuration.send_in_background
end

def test_error_report_send_timeout_default
assert_equal 10, Raygun.configuration.error_report_send_timeout
end

def test_enable_reporting_default
assert_equal true, Raygun.configuration.enable_reporting
end
end
4 changes: 3 additions & 1 deletion test/unit/resque_failure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ResqueFailureTest < Raygun::UnitTest

def setup
super

stub_request(:post, 'https://api.raygun.io/entries').to_return(status: 202)
fake_successful_entry
end

Expand All @@ -19,4 +21,4 @@ def test_failure_backend_appears_to_work
).save.success?
end

end
end
4 changes: 3 additions & 1 deletion test/unit/sidekiq_failure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class SidekiqFailureTest < Raygun::UnitTest

def setup
super

stub_request(:post, 'https://api.raygun.io/entries').to_return(status: 202)
fake_successful_entry
end

Expand All @@ -27,4 +29,4 @@ def test_we_are_in_sidekiqs_list_of_error_handlers
assert Sidekiq.error_handlers.include?(Raygun::SidekiqReporter)
end

end
end

0 comments on commit 2244996

Please sign in to comment.