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

Add sentry-delayed_job integration #1273

Merged
merged 7 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve ActiveJob support
  • Loading branch information
st0012 committed Feb 9, 2021
commit ffc116f0b65a1f06ef9c941a281bb4ec82ff5e57
10 changes: 9 additions & 1 deletion sentry-delayed_job/lib/sentry/delayed_job/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Plugin < ::Delayed::Plugin
end

def self.generate_extra(job)
{
extra = {
"delayed_job.id": job.id,
"delayed_job.priority": job.priority,
"delayed_job.attempts": job.attempts,
Expand All @@ -34,6 +34,14 @@ def self.generate_extra(job)
"delayed_job.last_error": job.last_error&.byteslice(0..1000),
"delayed_job.handler": job.handler&.byteslice(0..1000)
}

if job.payload_object.respond_to?(:job_data)
job.payload_object.job_data.each do |key, value|
extra[:"active_job.#{key}"] = value
end
end

extra
end
end
end
Expand Down
87 changes: 87 additions & 0 deletions sentry-delayed_job/spec/sentry/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,92 @@ def report
expect(Sentry.get_current_scope.tags).to eq({})
end
end

context "with ActiveJob" do
require "rails"
require "active_job"
require "sentry-rails"

class ReportingJob < ActiveJob::Base
self.queue_adapter = :delayed_job

def perform
Sentry.set_tags(number: 1)
Sentry.capture_message("report from ActiveJob")
end
end

class FailedJob < ActiveJob::Base
self.queue_adapter = :delayed_job

def perform
Sentry.set_tags(number: 2)
1 / 0
end
end

before do
ActiveJob::Base.logger = nil

# because we don't create a full Rails app here, we need to apply sentry-rails' ActiveJob extension manually
require "sentry/rails/active_job"
ActiveJob::Base.send(:prepend, Sentry::Rails::ActiveJobExtensions)

if Sentry.configuration.rails.skippable_job_adapters.empty?
Sentry.configuration.rails.skippable_job_adapters << "ActiveJob::QueueAdapters::DelayedJobAdapter"
end
end

context "when the job succeeded" do
before do
ReportingJob.perform_later

enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
end

it "doesn't leak scope data" do
expect(transport.events.count).to eq(1)

expect(Sentry.get_current_scope.tags).to eq({})
end

it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
expect(event[:message]).to eq("report from ActiveJob")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 1 })
expect(event.dig(:extra, :"active_job.job_class")).to eq("ReportingJob")
end
end

context "when the job failed" do
before do
FailedJob.perform_later

enqueued_job = Delayed::Backend::ActiveRecord::Job.last

expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
end

it "doesn't duplicate error reporting" do
expect(transport.events.count).to eq(1)

expect(Sentry.get_current_scope.tags).to eq({})
end

it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 2 })
expect(event.dig(:extra, :"active_job.job_class")).to eq("FailedJob")
end
end
end
end