Skip to content

Commit

Permalink
Support serializing ActiveRecord job arguments in global id form
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Jan 14, 2022
1 parent 4e840e6 commit e0079ae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 25 additions & 2 deletions sentry-rails/lib/sentry/rails/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def capture_and_reraise_with_sentry(scope, &block)
extra: sentry_context,
tags: {
job_id: job_id,
provider_job_id:provider_job_id
provider_job_id: provider_job_id
}
)
raise e
Expand All @@ -57,13 +57,36 @@ def already_supported_by_sentry_integration?
def sentry_context
{
active_job: self.class.name,
arguments: arguments,
arguments: sentry_serialize_arguments(arguments),
scheduled_at: scheduled_at,
job_id: job_id,
provider_job_id: provider_job_id,
locale: locale
}
end

def sentry_serialize_arguments(arguments)
arguments.map do |argument|
sentry_serialize_argument(argument)
end
end

def sentry_serialize_argument(argument)
case argument
when Hash
argument.reduce({}) do |h, (k, v)|
h.merge(k => sentry_serialize_argument(v))
end
when Array, Enumerable
argument.map { |v| sentry_serialize_argument(v) }
when ->(v) { v.respond_to?(:to_global_id) }
argument.to_global_id.to_s
else
argument
end
rescue StandardError
argument
end
end
end
end
15 changes: 15 additions & 0 deletions sentry-rails/spec/sentry/rails/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def perform
end
end

class JobWithArgument < ActiveJob::Base
def perform(*args, integer:, post:)
raise "foo"
end
end

class QueryPostJob < ActiveJob::Base
self.logger = nil

Expand Down Expand Up @@ -76,6 +82,15 @@ def rescue_callback(error)
expect(NormalJob.perform_now).to eq("foo")
end

it "serializes ActiveRecord arguments by default" do
post = Post.create!

expect { JobWithArgument.perform_now("foo", { bar: Sentry }, integer: 1, post: post) }.to raise_error(RuntimeError)

event = transport.events.last.to_json_compatible
expect(event.dig("extra", "arguments")).to eq(["foo", { "bar" => "Sentry" }, { "integer" => 1, "post" => "gid://rails-test-app/Post/#{post.id}" }])
end

it "adds useful context to extra" do
expect { FailedJob.perform_now }.to raise_error(FailedJob::TestError)

Expand Down

0 comments on commit e0079ae

Please sign in to comment.