Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional context to ActiveJob notifications #528

Merged
merged 4 commits into from Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 34 additions & 19 deletions lib/honeybadger/plugins/active_job.rb
Expand Up @@ -4,27 +4,42 @@ module ActiveJob
# Ignore inline and test adapters, as well as the adapters that we support with their own plugins
EXCLUDED_ADAPTERS = %i[inline test delayed_job faktory karafka resque shoryuken sidekiq sucker_punch].freeze

Plugin.register {
requirement { defined?(::Rails.application) && ::Rails.application }
requirement {
::Rails.application.config.respond_to?(:active_job) &&
class << self
def perform_around(job, block)
Honeybadger.clear!
context = context(job)
block.call
rescue StandardError => e
Honeybadger.notify(e, context: context, parameters: { arguments: job.arguments })
raise e
end

def context(job) # rubocop:disable Metrics/MethodLength
{
component: job.class,
action: 'perform',
enqueued_at: job.try(:enqueued_at),
executions: job.executions,
job_class: job.class,
job_id: job.job_id,
priority: job.priority,
queue_name: job.queue_name,
scheduled_at: job.scheduled_at
}
end
end

Plugin.register do
requirement do
defined?(::Rails.application) &&
::Rails.application.config.respond_to?(:active_job) &&
!EXCLUDED_ADAPTERS.include?(::Rails.application.config.active_job[:queue_adapter])
}
end

execution {
::ActiveJob::Base.class_eval do |base|
base.set_callback :perform, :around do |param, block|
Honeybadger.clear!
begin
block.call
rescue => error
Honeybadger.notify(error, parameters: {job_id: job_id, arguments: arguments})
raise error
end
end
end
}
}
execution do
::ActiveJob::Base.set_callback(:perform, :around, &ActiveJob.method(:perform_around))
end
end
end
end
end
31 changes: 31 additions & 0 deletions spec/integration/rails/active_job_adapter_spec.rb
@@ -0,0 +1,31 @@
require_relative '../rails_helper'

describe 'Rails ActiveJob Adapter Test', if: RAILS_PRESENT, type: :request do
include ActiveJob::TestHelper if RAILS_PRESENT
load_rails_hooks(self)

it 'reports exceptions' do
Honeybadger.flush do
perform_enqueued_jobs do
expect do
ErrorJob.perform_later({ some: 'data' })
end.to raise_error(StandardError)
end
end

expect(Honeybadger::Backend::Test.notifications[:notices].size).to eq(1)
expect(Honeybadger::Backend::Test.notifications[:notices][0].params[:arguments][0]).to eq({ some: 'data' })
expect(Honeybadger::Backend::Test.notifications[:notices][0].context).to \
include(
component: ErrorJob,
action: 'perform',
enqueued_at: anything,
executions: 1,
job_class: ErrorJob,
job_id: anything,
priority: anything,
queue_name: 'default',
scheduled_at: anything
)
end
end
19 changes: 0 additions & 19 deletions spec/integration/rails/async_queue_adapter_spec.rb

This file was deleted.