Skip to content

Commit

Permalink
update ElasticAPM::SpanHelpers to use ruby2_keywords for argument del…
Browse files Browse the repository at this point in the history
…egation compatibility with ruby >=3.0. add dependency on ruby2_keywords gem for ruby <2.7 compatibility. (#1294)
  • Loading branch information
danarnold authored Sep 26, 2022
1 parent 5370338 commit bb46d85
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions elastic-apm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|

spec.add_dependency('concurrent-ruby', '~> 1.0')
spec.add_dependency('http', '>= 3.0')
spec.add_runtime_dependency('ruby2_keywords')

spec.require_paths = ['lib']
end
1 change: 1 addition & 0 deletions lib/elastic_apm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'concurrent'
require 'forwardable'
require 'securerandom'
require 'ruby2_keywords'

require 'elastic_apm/version'
require 'elastic_apm/internal_error'
Expand Down
4 changes: 2 additions & 2 deletions lib/elastic_apm/span_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def __span_method_on(klass, method, name = nil, type = nil)
type ||= Span::DEFAULT_TYPE

klass.prepend(Module.new do
define_method(method) do |*args, &block|
ruby2_keywords(define_method(method) do |*args, &block|
unless ElasticAPM.current_transaction
return super(*args, &block)
end

ElasticAPM.with_span name.to_s, type.to_s do
super(*args, &block)
end
end
end)
end)
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/elastic_apm/span_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def do_the_block_thing(&block)
block.call
end
span_method :do_the_block_thing

def do_mixed_args_thing(one, two, three: nil)
[one, two, three]
end
span_method :do_mixed_args_thing
end

context 'on class methods', :intercept do
Expand Down Expand Up @@ -79,6 +84,19 @@ def do_the_block_thing(&block)
expect(@intercepted.spans.length).to be 1
expect(@intercepted.spans.last.name).to eq 'do_the_block_thing'
end

it 'handles mixed positional and keyword arguments' do
thing = Thing.new

with_agent do
ElasticAPM.with_transaction do
thing.do_mixed_args_thing('one', 'two', three: 'three')
end
end

expect(@intercepted.spans.length).to be 1
expect(@intercepted.spans.last.name).to eq 'do_mixed_args_thing'
end
end
end
end

0 comments on commit bb46d85

Please sign in to comment.