Skip to content

Commit

Permalink
Merge 5f763e3 into 8fa587b
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Dec 7, 2018
2 parents 8fa587b + 5f763e3 commit b11e5e5
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require "bugsnag/middleware/suggestion_data"
require "bugsnag/middleware/classify_error"
require "bugsnag/middleware/session_data"
require "bugsnag/utility/circular_buffer"
require "bugsnag/breadcrumbs/breadcrumbs"

module Bugsnag
class Configuration
Expand Down Expand Up @@ -37,6 +39,15 @@ class Configuration
attr_accessor :track_sessions
attr_accessor :session_endpoint

##
# @param [Array] array of strings indicating allowable automatic breadcrumb types
# @return [Array] indicates the allowable automatic breadcrumbs
attr_accessor :automatic_breadcrumb_types

##
# @return [Integer] the maximum allowable amount of breadcrumbs per thread
attr_reader :max_breadcrumbs

API_KEY_REGEX = /[0-9a-f]{32}/i
THREAD_LOCAL_NAME = "bugsnag_req_data"
DEFAULT_ENDPOINT = "https://notify.bugsnag.com"
Expand All @@ -51,6 +62,8 @@ class Configuration
"rack.request.form_vars"
].freeze

DEFAULT_MAX_BREADCRUMBS = 25

alias :track_sessions :auto_capture_sessions
alias :track_sessions= :auto_capture_sessions=

Expand All @@ -68,6 +81,12 @@ def initialize
self.notify_release_stages = nil
self.auto_capture_sessions = false
self.session_endpoint = DEFAULT_SESSION_ENDPOINT
# All valid breadcrumb types should be allowable initially
self.automatic_breadcrumb_types = Bugsnag::Breadcrumbs::VALID_BREADCRUMB_TYPES.clone

# Store max_breadcrumbs here instead of outputting breadcrumbs.max_items
# to avoid infinite recursion when creating breadcrumb buffer
@max_breadcrumbs = DEFAULT_MAX_BREADCRUMBS

# SystemExit and Interrupt are common Exception types seen with successful
# exits and are not automatically reported to Bugsnag
Expand Down Expand Up @@ -190,6 +209,31 @@ def parse_proxy(uri)
self.proxy_password = proxy.password
end

##
# Sets the maximum allowable amount of breadcrumbs
#
# @param [Integer] the new maximum breadcrumb limit
def max_breadcrumbs=(new_max_breadcrumbs)
@max_breadcrumbs = new_max_breadcrumbs
breadcrumbs.max_items = new_max_breadcrumbs
end

##
# Returns the breadcrumb circular buffer
#
# @return [Bugsnag::Utility::CircularBuffer] a thread based circular buffer containing breadcrumbs
def breadcrumbs
request_data[:breadcrumbs] ||= Bugsnag::Utility::CircularBuffer.new(@max_breadcrumbs)
end

##
# Stores callbacks that will be run before a breadcrumb is left
#
# @return [Array] a thread independent array of callback functions
def before_breadcrumb_callbacks
@before_breadcrumb_callbacks ||= []
end

private

PROG_NAME = "[Bugsnag]"
Expand Down
88 changes: 88 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,92 @@ def debug(name, &block)
expect(subject.ignore_classes).to eq(Set.new([SystemExit, Interrupt]))
end

describe "#breadcrumbs" do
it "first returns a new circular buffer" do
buffer = subject.breadcrumbs

expect(buffer).to be_a(Bugsnag::Utility::CircularBuffer)
expect(buffer.to_a).to eq([])
end

it "returns the same buffer in repeated calls" do
buffer = subject.breadcrumbs
buffer << 1
second_buffer = subject.breadcrumbs

expect(second_buffer.to_a).to eq([1])
end

it "returns a different buffer on different threads" do
buffer = subject.breadcrumbs
buffer << 1

second_buffer = nil
Thread.new { second_buffer = subject.breadcrumbs; second_buffer << 2 }.join

expect(buffer.to_a).to eq([1])
expect(second_buffer.to_a).to eq([2])
expect(buffer).to_not eq(second_buffer)
end

it "sets max_items to the current max_breadcrumbs size" do
expect(subject.breadcrumbs.max_items).to eq(subject.max_breadcrumbs)
end
end

describe "#max_breadcrumbs" do
it "defaults to DEFAULT_MAX_BREADCRUMBS" do
expect(subject.max_breadcrumbs).to eq(Bugsnag::Configuration::DEFAULT_MAX_BREADCRUMBS)
end
end

describe "#max_breadcrumbs=" do
it "sets the value of max_breadcrumbs" do
subject.max_breadcrumbs = 10
expect(subject.max_breadcrumbs).to eq(10)
end

it "sets the max_items property of the breadcrumbs buffer" do
buffer = subject.breadcrumbs

expect(buffer.max_items).to eq(Bugsnag::Configuration::DEFAULT_MAX_BREADCRUMBS)

subject.max_breadcrumbs = 5

expect(buffer.max_items).to eq(5)
end
end

describe "#automatic_breadcrumb_types" do
it "defaults to Bugsnag::Breadcrumbs::VALID_BREADCRUMB_TYPES" do
expect(subject.automatic_breadcrumb_types).to eq(Bugsnag::Breadcrumbs::VALID_BREADCRUMB_TYPES)
end
end

describe "#before_breadcrumb_callbacks" do
it "initially returns an empty array" do
expect(subject.before_breadcrumb_callbacks).to eq([])
end

it "stores the array between subsequent calls" do
first_call = subject.before_breadcrumb_callbacks
first_call << 1

second_call = subject.before_breadcrumb_callbacks

expect(second_call).to eq([1])
end

it "stays the same across threads" do
first_array = subject.before_breadcrumb_callbacks
first_array << 1

second_array = nil
Thread.new { second_array = subject.before_breadcrumb_callbacks; second_array << 2}.join

expect(first_array).to eql(second_array)
expect(first_array).to eq([1, 2])
expect(second_array).to eq([1, 2])
end
end
end

0 comments on commit b11e5e5

Please sign in to comment.