Skip to content

Commit

Permalink
feat(config): Add option to disable autoconfiguration (#419)
Browse files Browse the repository at this point in the history
This adds two functions, load_integrations & load_integration(integration) 
that allow Bugsnag integrations to be loaded during runtime.

By default, load_integrations will be run when the bugsnag module is
required. This behaviour can be disabled using the environment variable
BUGSNAG_DISABLE_AUTOCONFIGURE.
  • Loading branch information
Cawllec authored and kattrali committed Mar 13, 2018
1 parent fe6025e commit a54d065
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
require "bugsnag/delivery/synchronous"
require "bugsnag/delivery/thread_queue"

# Rack is not bundled with the other integrations
# as it doesn't auto-configure when loaded
require "bugsnag/integrations/rack"

require "bugsnag/middleware/rack_request"
Expand All @@ -28,6 +30,7 @@

module Bugsnag
LOCK = Mutex.new
INTEGRATIONS = [:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que]

class << self
##
Expand Down Expand Up @@ -155,13 +158,28 @@ def start_session
def before_notify_callbacks
Bugsnag.configuration.request_data[:before_callbacks] ||= []
end
end
end

require "bugsnag/integrations/railtie" if defined?(Rails::Railtie)
[:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que].each do |integration|
begin
require "bugsnag/integrations/#{integration}"
rescue LoadError
# Attempts to load all integrations through auto-discovery
def load_integrations
require "bugsnag/integrations/railtie" if defined?(Rails::Railtie)
INTEGRATIONS.each do |integration|
begin
require "bugsnag/integrations/#{integration}"
rescue LoadError
end
end
end

# Load a specific integration
def load_integration(integration)
integration = :railtie if integration == :rails
if INTEGRATIONS.include?(integration) || integration == :railtie
require "bugsnag/integrations/#{integration}"
else
configuration.debug("Integration #{integration} is not currently supported")
end
end
end
end

Bugsnag.load_integrations unless ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"]
67 changes: 67 additions & 0 deletions spec/bugsnag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,71 @@
expect(Bugsnag.configuration.logger).to have_received(:warn)
end
end

describe 'loading integrations' do
before do
module Kernel
REQUIRED = []
alias_method :old_require, :require
def require(path)
if path.include?("bugsnag/integrations/")
REQUIRED << path
else
old_require(path)
end
end
end
end

it 'attempts to load integrations' do
Kernel::REQUIRED = []
ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] = nil
load "./lib/bugsnag.rb"
Bugsnag::INTEGRATIONS.each do |integration|
expect(Kernel::REQUIRED).to include("bugsnag/integrations/#{integration}")
end
end

it 'does not load integrations when BUGSNAG_DISABLE_AUTOCONFIGURE is true' do
Kernel::REQUIRED = []
ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] = 'true'
load "./lib/bugsnag.rb"
expect(Kernel::REQUIRED).to eq(["bugsnag/integrations/rack"])
end

it 'loads all integrations if requested' do
Kernel::REQUIRED = []
expect(Kernel::REQUIRED).to eq([])
Bugsnag.load_integrations
Bugsnag::INTEGRATIONS.each do |integration|
expect(Kernel::REQUIRED).to include("bugsnag/integrations/#{integration}")
end
end

it 'loads singular integrations' do
Kernel::REQUIRED = []
expect(Kernel::REQUIRED).to eq([])
Bugsnag::INTEGRATIONS.each do |integration|
Kernel::REQUIRED = []
Bugsnag.load_integration(integration)
expect(Kernel::REQUIRED).to include("bugsnag/integrations/#{integration}")
end
end

it 'loads railtie for :rails or :railtie' do
Kernel::REQUIRED = []
Bugsnag.load_integration(:rails)
expect(Kernel::REQUIRED).to include("bugsnag/integrations/railtie")
Kernel::REQUIRED = []
Bugsnag.load_integration(:railtie)
expect(Kernel::REQUIRED).to include("bugsnag/integrations/railtie")
end

after do
module Kernel
alias_method :require, :old_require
end
Kernel.send(:remove_const, :REQUIRED)
end
end
end

0 comments on commit a54d065

Please sign in to comment.