Skip to content

Commit

Permalink
Merge pull request #147 from MindscapeHQ/tl/improve-failsafe-exceptio…
Browse files Browse the repository at this point in the history
…n-retrying

Improve failsafe exception retrying & large breadcrumb support
  • Loading branch information
UberMouse authored Feb 20, 2019
2 parents 1cbca6e + fda0d45 commit c929380
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
7 changes: 2 additions & 5 deletions lib/raygun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
require "rack"
require "ostruct"

begin
require "pry"
rescue LoadError
end

require "raygun/version"
require "raygun/configuration"
require "raygun/client"
Expand Down Expand Up @@ -152,6 +147,8 @@ def track_exception_sync(exception_instance, env, user, retry_count)
env[:custom_data] ||= {}
env[:custom_data].merge!(original_stacktrace: exception_instance.backtrace)

::Raygun::Breadcrumbs::Store.clear

track_exception(new_exception, env, user, retry_count - 1)
else
raise e
Expand Down
4 changes: 4 additions & 0 deletions lib/raygun/breadcrumbs/breadcrumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def build_payload
v != nil
end]
end

def size
return message.length + 100
end
end
end
end
10 changes: 10 additions & 0 deletions lib/raygun/breadcrumbs/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def self.any?
stored != nil && stored.length > 0
end

def self.take_until_size(size)
breadcrumb_size = 0

stored.reverse.take_while do |crumb|
breadcrumb_size += crumb.size

breadcrumb_size < size
end.reverse
end

private

def self.should_record?(crumb)
Expand Down
3 changes: 2 additions & 1 deletion lib/raygun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Client

ENV_IP_ADDRESS_KEYS = %w(action_dispatch.remote_ip raygun.remote_ip REMOTE_ADDR)
NO_API_KEY_MESSAGE = "[RAYGUN] Just a note, you've got no API Key configured, which means we can't report exceptions. Specify your Raygun API key using Raygun#setup (find yours at https://app.raygun.io)"
MAX_BREADCRUMBS_SIZE = 100_000

include HTTParty

Expand Down Expand Up @@ -214,7 +215,7 @@ def build_payload_hash(exception_instance, env = {}, user = nil)
}
}
store = ::Raygun::Breadcrumbs::Store
error_details[:breadcrumbs] = store.stored.map(&:build_payload) if store.any?
error_details[:breadcrumbs] = store.take_until_size(MAX_BREADCRUMBS_SIZE).map(&:build_payload) if store.any?

Raygun.log('set details and breadcrumbs')

Expand Down
2 changes: 1 addition & 1 deletion raygun4ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "webmock"

spec.add_development_dependency 'rails', "= 5.2"
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'sqlite3', '~> 1.3.6'
spec.add_development_dependency 'capybara'
spec.add_development_dependency "rspec-rails"
spec.add_development_dependency "launchy"
Expand Down
37 changes: 37 additions & 0 deletions spec/raygun/breadcrumbs/breadcrumb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ module Breadcrumbs
expect(payload[:CustomData]).to eq(foo: 'bar')
end
end

describe "#size" do
before do
Timecop.freeze
Store.initialize
end
after do
Timecop.return
Store.clear
end

let(:message) { "This is a breadcrumb message" }

let(:breadcrumb) do
Store.record(
message: message,
category: "test",
level: :info,
class_name: "HomeController",
method_name: "index",
line_number: 17,
metadata: {
foo: 'bar'
}
)

Store.stored[0]
end

let(:size) { breadcrumb.size }

it "returns the estimated size of the breadcrumb" do
# Can't check all the fields but message so assume a standard 100 length for all of them
# The message should be the bulk of large breadcrumbs anyway
expect(size).to eq(message.length + 100)
end
end
end
end
end
24 changes: 24 additions & 0 deletions spec/raygun/breadcrumbs/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ module Breadcrumbs
end
end

describe "#take_until_size" do
before do
subject.initialize
end

it "takes the most recent breadcrumbs until the size limit is reached" do
subject.record(message: '1' * 100)
subject.record(message: '2' * 100)
subject.record(message: '3' * 100)

crumbs = subject.take_until_size(500)

expect(crumbs.length).to eq(2)
expect(crumbs[0].message).to eq('2' * 100)
expect(crumbs[1].message).to eq('3' * 100)
end

it "does not crash with no recorded breadcrumbs" do
crumbs = subject.take_until_size(500)

expect(crumbs).to eq([])
end
end

context "adding a breadcrumb" do
class Foo
include ::Raygun::Breadcrumbs
Expand Down

0 comments on commit c929380

Please sign in to comment.