Skip to content

Commit

Permalink
Merge branch 'master' into cawllec/https-http-proxy-vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Mar 29, 2018
2 parents d1155fb + 8fb2117 commit 2e99f93
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 12 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"]
11 changes: 7 additions & 4 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Configuration
/cookie/i,
/password/i,
/secret/i,
/warden\.user\.([^.]+)\.key/,
"rack.request.form_vars"
].freeze

Expand Down Expand Up @@ -84,7 +85,7 @@ def initialize
self.logger = Logger.new(STDOUT)
self.logger.level = Logger::INFO
self.logger.formatter = proc do |severity, datetime, progname, msg|
"** [Bugsnag] #{datetime}: #{msg}\n"
"** #{progname} #{datetime}: #{msg}\n"
end

# Configure the bugsnag middleware stack
Expand Down Expand Up @@ -164,19 +165,19 @@ def clear_request_data
##
# Logs an info level message
def info(message)
logger.info(message)
logger.info(PROG_NAME) { message }
end

##
# Logs a warning level message
def warn(message)
logger.warn(message)
logger.warn(PROG_NAME) { message }
end

##
# Logs a debug level message
def debug(message)
logger.debug(message)
logger.debug(PROG_NAME) { message }
end

##
Expand All @@ -191,6 +192,8 @@ def parse_proxy(uri)

private

PROG_NAME = "[Bugsnag]"

def default_hostname
# Send the heroku dyno name instead of hostname if available
ENV["DYNO"] || Socket.gethostname;
Expand Down
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
80 changes: 80 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,86 @@
end
end

describe "logger" do
class TestLogger
attr_accessor :logs

def initialize
@logs = []
end

def log(level, name, &block)
message = block.call
@logs << {
:level => level,
:name => name,
:message => message
}
end

def info(name, &block)
log('info', name, &block)
end

def warn(name, &block)
log('warning', name, &block)
end

def debug(name, &block)
log('debug', name, &block)
end
end

before do
@logger = TestLogger.new
Bugsnag.configure do |bugsnag|
bugsnag.logger = @logger
end
end

it "should log info messages to the set logger" do
expect(@logger.logs.size).to eq(0)
Bugsnag.configuration.info("Info message")
expect(@logger.logs.size).to eq(1)
log = @logger.logs.first
expect(log).to eq({
:level => "info",
:name => "[Bugsnag]",
:message => "Info message"
})
end

it "should log warning messages to the set logger" do
expect(@logger.logs.size).to eq(0)
Bugsnag.configuration.warn("Warning message")
expect(@logger.logs.size).to eq(1)
log = @logger.logs.first
expect(log).to eq({
:level => "warning",
:name => "[Bugsnag]",
:message => "Warning message"
})
end

it "should log debug messages to the set logger" do
expect(@logger.logs.size).to eq(0)
Bugsnag.configuration.debug("Debug message")
expect(@logger.logs.size).to eq(1)
log = @logger.logs.first
expect(log).to eq({
:level => "debug",
:name => "[Bugsnag]",
:message => "Debug message"
})
end

after do
Bugsnag.configure do |bugsnag|
bugsnag.logger = Logger.new(StringIO.new)
end
end
end

it "should have exit exception classes ignored by default" do
expect(subject.ignore_classes).to eq(Set.new([SystemExit, Interrupt]))
end
Expand Down
31 changes: 30 additions & 1 deletion spec/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,27 @@ def gloops
it "filters params from all payload hashes if they are set in default meta_data_filters" do

Bugsnag.notify(BugsnagTestException.new("It crashed")) do |report|
report.meta_data.merge!({:request => {:params => {:password => "1234", :other_password => "12345", :other_data => "123456"}}})
report.meta_data.merge!({
:request => {
:params => {
:password => "1234",
:other_password => "12345",
:other_data => "123456"
},
:cookie => "1234567890",
:authorization => "token",
:user_authorization => "token",
:secret_key => "key",
:user_secret => "key"
}
})
report.meta_data.merge!({
:session => {
:"warden.user.user.key" => "1234",
:"warden.user.foobar.key" => "1234",
:"warden.user.test" => "1234"
}
})
end

expect(Bugsnag).to have_sent_notification{ |payload, headers|
Expand All @@ -585,6 +605,15 @@ def gloops
expect(event["metaData"]["request"]["params"]["password"]).to eq("[FILTERED]")
expect(event["metaData"]["request"]["params"]["other_password"]).to eq("[FILTERED]")
expect(event["metaData"]["request"]["params"]["other_data"]).to eq("123456")
expect(event["metaData"]["request"]["cookie"]).to eq("[FILTERED]")
expect(event["metaData"]["request"]["authorization"]).to eq("[FILTERED]")
expect(event["metaData"]["request"]["user_authorization"]).to eq("[FILTERED]")
expect(event["metaData"]["request"]["secret_key"]).to eq("[FILTERED]")
expect(event["metaData"]["request"]["user_secret"]).to eq("[FILTERED]")
expect(event["metaData"]["session"]).not_to be_nil
expect(event["metaData"]["session"]["warden.user.user.key"]).to eq("[FILTERED]")
expect(event["metaData"]["session"]["warden.user.foobar.key"]).to eq("[FILTERED]")
expect(event["metaData"]["session"]["warden.user.test"]).to eq("1234")
}
end

Expand Down

0 comments on commit 2e99f93

Please sign in to comment.