Skip to content

Commit 4217838

Browse files
authored
Support serializing ActiveRecord job arguments in global id form (#1688)
* Support serializing ActiveRecord job arguments in global id form * Update changelog
1 parent d4b17f1 commit 4217838

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### Features
4+
5+
- Support serializing ActiveRecord job arguments in global id form [#1688](https://github.com/getsentry/sentry-ruby/pull/1688)
6+
17
## 5.0.2
28

39
- Respect port info provided in user's DSN [#1702](https://github.com/getsentry/sentry-ruby/pull/1702)

sentry-rails/lib/sentry/rails/active_job.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def capture_and_reraise_with_sentry(scope, &block)
3737
extra: sentry_context,
3838
tags: {
3939
job_id: job_id,
40-
provider_job_id:provider_job_id
40+
provider_job_id: provider_job_id
4141
}
4242
)
4343
raise e
@@ -57,13 +57,26 @@ def already_supported_by_sentry_integration?
5757
def sentry_context
5858
{
5959
active_job: self.class.name,
60-
arguments: arguments,
60+
arguments: sentry_serialize_arguments(arguments),
6161
scheduled_at: scheduled_at,
6262
job_id: job_id,
6363
provider_job_id: provider_job_id,
6464
locale: locale
6565
}
6666
end
67+
68+
def sentry_serialize_arguments(argument)
69+
case argument
70+
when Hash
71+
argument.transform_values { |v| sentry_serialize_arguments(v) }
72+
when Array, Enumerable
73+
argument.map { |v| sentry_serialize_arguments(v) }
74+
when ->(v) { v.respond_to?(:to_global_id) }
75+
argument.to_global_id.to_s rescue argument
76+
else
77+
argument
78+
end
79+
end
6780
end
6881
end
6982
end

sentry-rails/spec/sentry/rails/activejob_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def perform
2727
end
2828
end
2929

30+
class JobWithArgument < ActiveJob::Base
31+
def perform(*args, integer:, post:, **options)
32+
raise "foo"
33+
end
34+
end
35+
3036
class QueryPostJob < ActiveJob::Base
3137
self.logger = nil
3238

@@ -76,6 +82,52 @@ def rescue_callback(error)
7682
expect(NormalJob.perform_now).to eq("foo")
7783
end
7884

85+
describe "ActiveJob arguments serialization" do
86+
it "serializes ActiveRecord arguments in globalid form" do
87+
post = Post.create!
88+
post2 = Post.create!
89+
90+
expect do
91+
JobWithArgument.perform_now("foo", { bar: Sentry }, integer: 1, post: post, nested: { another_level: { post: post2 } })
92+
end.to raise_error(RuntimeError)
93+
94+
event = transport.events.last.to_json_compatible
95+
expect(event.dig("extra", "arguments")).to eq(
96+
[
97+
"foo",
98+
{ "bar" => "Sentry" },
99+
{
100+
"integer" => 1,
101+
"post" => "gid://rails-test-app/Post/#{post.id}",
102+
"nested" => { "another_level" => { "post" => "gid://rails-test-app/Post/#{post2.id}" } }
103+
}
104+
]
105+
)
106+
end
107+
108+
it "handles problematic globalid conversion gracefully" do
109+
post = Post.create!
110+
111+
def post.to_global_id
112+
raise
113+
end
114+
115+
expect do
116+
JobWithArgument.perform_now(integer: 1, post: post)
117+
end.to raise_error(RuntimeError)
118+
119+
event = transport.events.last.to_json_compatible
120+
expect(event.dig("extra", "arguments")).to eq(
121+
[
122+
{
123+
"integer" => 1,
124+
"post" => post.to_s,
125+
}
126+
]
127+
)
128+
end
129+
end
130+
79131
it "adds useful context to extra" do
80132
expect { FailedJob.perform_now }.to raise_error(FailedJob::TestError)
81133

0 commit comments

Comments
 (0)