Skip to content

Commit

Permalink
Skip middleware and plugins when exceptions are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
stympy committed Nov 19, 2024
1 parent 334544d commit 1b112e4
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lib/honeybadger/init/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Honeybadger.load_plugins!

if Hanami::VERSION >= '2.0'
if Hanami::VERSION >= '2.0' && Honeybadger.config[:'exceptions.enabled']
Hanami.app.instance_eval do
config.middleware.use Honeybadger::Rack::UserFeedback
config.middleware.use Honeybadger::Rack::UserInformer
Expand Down
8 changes: 5 additions & 3 deletions lib/honeybadger/init/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class Railtie < ::Rails::Railtie
initializer 'honeybadger.install_middleware' do |app|
honeybadger_config = Honeybadger::Agent.instance.config

app.config.middleware.insert(0, Honeybadger::Rack::ErrorNotifier)
app.config.middleware.insert_before(Honeybadger::Rack::ErrorNotifier, Honeybadger::Rack::UserInformer) if honeybadger_config[:'user_informer.enabled']
app.config.middleware.insert_before(Honeybadger::Rack::ErrorNotifier, Honeybadger::Rack::UserFeedback) if honeybadger_config[:'feedback.enabled']
if honeybadger_config[:'exceptions.enabled']
app.config.middleware.insert(0, Honeybadger::Rack::ErrorNotifier)
app.config.middleware.insert_before(Honeybadger::Rack::ErrorNotifier, Honeybadger::Rack::UserInformer) if honeybadger_config[:'user_informer.enabled']
app.config.middleware.insert_before(Honeybadger::Rack::ErrorNotifier, Honeybadger::Rack::UserFeedback) if honeybadger_config[:'feedback.enabled']
end
end

config.before_initialize do
Expand Down
2 changes: 1 addition & 1 deletion lib/honeybadger/plugins/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def context(job) # rubocop:disable Metrics/MethodLength
end

execution do
::ActiveJob::Base.set_callback(:perform, :around, &ActiveJob.method(:perform_around))
::ActiveJob::Base.set_callback(:perform, :around, &ActiveJob.method(:perform_around)) if Honeybadger.config[:'exceptions.enabled']

if config.load_plugin_insights?(:active_job)
::ActiveSupport::Notifications.subscribe(/(enqueue_at|enqueue|enqueue_retry|enqueue_all|perform|retry_stopped|discard)\.active_job/, Honeybadger::ActiveJobSubscriber.new)
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/plugins/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Honeybadger
end

execution do
return unless Honeybadger.config[:'exceptions.enabled']
require 'honeybadger/plugins/delayed_job/plugin'
::Delayed::Worker.plugins << Plugins::DelayedJob::Plugin
end
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/plugins/faktory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def call(worker, job)
requirement { defined?(::Faktory) }

execution do
return unless Honeybadger.config[:'exceptions.enabled']
::Faktory.configure_worker do |faktory|
faktory.worker_middleware do |chain|
chain.prepend Middleware
Expand Down
8 changes: 5 additions & 3 deletions lib/honeybadger/plugins/karafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ module Plugins
execution do
require 'honeybadger/karafka'

errors_listener = ::Honeybadger::Karafka::ErrorsListener.new
::Karafka.monitor.subscribe(errors_listener)
::Karafka.producer.monitor.subscribe(errors_listener) if ::Karafka.respond_to?(:producer)
if Honeybadger.config[:'exceptions.enabled']
errors_listener = ::Honeybadger::Karafka::ErrorsListener.new
::Karafka.monitor.subscribe(errors_listener)
::Karafka.producer.monitor.subscribe(errors_listener) if ::Karafka.respond_to?(:producer)
end

if config.load_plugin_insights?(:karafka)
::Karafka.monitor.subscribe(::Honeybadger::Karafka::InsightsListener.new)
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/plugins/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def payload_class_with_honeybadger
end

execution do
return unless Honeybadger.config[:'exceptions.enabled']
::Resque::Job.send(:include, Installer)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/plugins/shoryuken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def notification_params(body)
requirement { defined?(::Shoryuken) }

execution do
return unless Honeybadger.config[:'exceptions.enabled']
::Shoryuken.configure_server do |config|
config.server_middleware do |chain|
chain.add Middleware
Expand Down
84 changes: 43 additions & 41 deletions lib/honeybadger/plugins/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,60 +73,62 @@ def call(worker, msg, queue, _redis)
requirement { defined?(::Sidekiq) }

execution do
::Sidekiq.configure_server do |sidekiq|
sidekiq.server_middleware do |chain|
chain.prepend Middleware
if Honeybadger.config[:'exceptions.enabled']
::Sidekiq.configure_server do |sidekiq|
sidekiq.server_middleware do |chain|
chain.prepend Middleware
end
end
end

if defined?(::Sidekiq::VERSION) && ::Sidekiq::VERSION > '3'
::Sidekiq.configure_server do |sidekiq|
if defined?(::Sidekiq::VERSION) && ::Sidekiq::VERSION > '3'
::Sidekiq.configure_server do |sidekiq|

sidekiq_default_configuration = (::Sidekiq::VERSION > '7') ?
::Sidekiq.default_configuration : Class.new
sidekiq_default_configuration = (::Sidekiq::VERSION > '7') ?
::Sidekiq.default_configuration : Class.new

sidekiq.error_handlers << lambda { |ex, sidekiq_params, sidekiq_config = sidekiq_default_configuration|
params = sidekiq_params.dup
if defined?(::Sidekiq::Config)
if params[:_config].is_a?(::Sidekiq::Config) # Sidekiq > 6 and < 7.1.5
params[:_config] = params[:_config].instance_variable_get(:@options)
else # Sidekiq >= 7.1.5
params[:_config] = sidekiq_config.instance_variable_get(:@options)
sidekiq.error_handlers << lambda { |ex, sidekiq_params, sidekiq_config = sidekiq_default_configuration|
params = sidekiq_params.dup
if defined?(::Sidekiq::Config)
if params[:_config].is_a?(::Sidekiq::Config) # Sidekiq > 6 and < 7.1.5
params[:_config] = params[:_config].instance_variable_get(:@options)
else # Sidekiq >= 7.1.5
params[:_config] = sidekiq_config.instance_variable_get(:@options)
end
end
end

job = params[:job] || params
job = params[:job] || params

job_retry = job['retry'.freeze]
job_retry = job['retry'.freeze]

if (threshold = config[:'sidekiq.attempt_threshold'].to_i) > 0 && job_retry
# We calculate the job attempts to determine the need to
# skip. Sidekiq's first job execution will have nil for the
# 'retry_count' job key. The first retry will have 0 set for
# the 'retry_count' key, incrementing on each execution
# afterwards.
retry_count = job['retry_count'.freeze]
attempt = retry_count ? retry_count + 1 : 0
if (threshold = config[:'sidekiq.attempt_threshold'].to_i) > 0 && job_retry
# We calculate the job attempts to determine the need to
# skip. Sidekiq's first job execution will have nil for the
# 'retry_count' job key. The first retry will have 0 set for
# the 'retry_count' key, incrementing on each execution
# afterwards.
retry_count = job['retry_count'.freeze]
attempt = retry_count ? retry_count + 1 : 0

max_retries = (::Sidekiq::VERSION > '7') ?
::Sidekiq.default_configuration[:max_retries] : sidekiq.options[:max_retries]
# Ensure we account for modified max_retries setting
default_max_retry_attempts = defined?(::Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS) ? ::Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS : 25
retry_limit = job_retry == true ? (max_retries || default_max_retry_attempts) : job_retry.to_i
max_retries = (::Sidekiq::VERSION > '7') ?
::Sidekiq.default_configuration[:max_retries] : sidekiq.options[:max_retries]
# Ensure we account for modified max_retries setting
default_max_retry_attempts = defined?(::Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS) ? ::Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS : 25
retry_limit = job_retry == true ? (max_retries || default_max_retry_attempts) : job_retry.to_i

limit = [retry_limit, threshold].min
limit = [retry_limit, threshold].min

return if attempt < limit
end
return if attempt < limit
end

opts = { parameters: params }
if config[:'sidekiq.use_component']
opts[:component] = job['wrapped'.freeze] || job['class'.freeze]
opts[:action] = 'perform' if opts[:component]
end
opts = { parameters: params }
if config[:'sidekiq.use_component']
opts[:component] = job['wrapped'.freeze] || job['class'.freeze]
opts[:action] = 'perform' if opts[:component]
end

Honeybadger.notify(ex, opts)
}
Honeybadger.notify(ex, opts)
}
end
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/plugins/sucker_punch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Honeybadger
requirement { defined?(::SuckerPunch) }

execution do
return unless Honeybadger.config[:'exceptions.enabled']
if SuckerPunch.respond_to?(:exception_handler=) # >= v2
SuckerPunch.exception_handler = ->(ex, klass, args) { Honeybadger.notify(ex, { :component => klass, :parameters => args }) }
else
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/plugins/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def invoke_command_with_honeybadger(*args)
requirement { defined?(::Thor.no_commands) }

execution do
return unless Honeybadger.config[:'exceptions.enabled']
::Thor.send(:include, Thor)
end
end
Expand Down

0 comments on commit 1b112e4

Please sign in to comment.