Skip to content

Commit 30b0f9d

Browse files
authored
fix: Support Active Jobs with keyword args across ruby versions (#852)
* Make active_job instrumentation compatible with jobs defined with keywords * Add addional arg types for activejob tests
1 parent 5318e4a commit 30b0f9d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/patches/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def initialize(*args)
2020
@metadata = {}
2121
super
2222
end
23+
ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true)
2324

2425
def serialize
2526
super.merge('metadata' => serialize_arguments(metadata))

instrumentation/active_job/test/instrumentation/active_job/patches/active_job_callbacks_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@
4646
end
4747
end
4848

49+
describe 'compatibility' do
50+
it 'works with positional args' do
51+
_(PositionalOnlyArgsJob.perform_now('arg1')).must_be_nil # Make sure this runs without raising an error
52+
end
53+
54+
it 'works with keyword args' do
55+
_(KeywordOnlyArgsJob.perform_now(keyword2: :keyword2)).must_be_nil # Make sure this runs without raising an error
56+
end
57+
58+
it 'works with mixed args' do
59+
_(MixedArgsJob.perform_now('arg1', 'arg2', keyword2: :keyword2)).must_be_nil # Make sure this runs without raising an error
60+
end
61+
end
62+
4963
describe 'exception handling' do
5064
it 'sets span status to error' do
5165
_ { ExceptionJob.perform_now }.must_raise StandardError, 'This job raises an exception'

instrumentation/active_job/test/test_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ def perform
3737
end
3838
end
3939

40+
class PositionalOnlyArgsJob < ::ActiveJob::Base
41+
def perform(arg1, arg2 = 'default'); end
42+
end
43+
class KeywordOnlyArgsJob < ::ActiveJob::Base
44+
def perform(keyword1: 'default', keyword2:); end
45+
end
46+
47+
class MixedArgsJob < ::ActiveJob::Base
48+
def perform(arg1, arg2, keyword1: 'default', keyword2:); end
49+
end
50+
4051
::ActiveJob::Base.queue_adapter = :inline
4152
::ActiveJob::Base.logger = Logger.new(File::NULL)
4253

0 commit comments

Comments
 (0)