From 9820f7a0d84738f368e7a784be516fd3449e298d Mon Sep 17 00:00:00 2001 From: Daniel Arnold Date: Wed, 10 Aug 2022 17:07:11 -0400 Subject: [PATCH] update ElasticAPM::SpanHelpers to use ruby2_keywords for argument delegation compatibility with ruby >=3.0. use ruby2_keywords gem for ruby <2.7. --- Gemfile | 4 ++++ lib/elastic_apm/span_helpers.rb | 4 ++-- spec/elastic_apm/span_helpers_spec.rb | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 11e7fc628..45f2eaf45 100644 --- a/Gemfile +++ b/Gemfile @@ -126,4 +126,8 @@ group :bench do gem 'stackprof', require: nil, platforms: %i[ruby] end +if RUBY_VERSION < '2.7' + gem 'ruby2_keywords' +end + gemspec diff --git a/lib/elastic_apm/span_helpers.rb b/lib/elastic_apm/span_helpers.rb index a835868ca..2f60db5a8 100644 --- a/lib/elastic_apm/span_helpers.rb +++ b/lib/elastic_apm/span_helpers.rb @@ -37,7 +37,7 @@ 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 @@ -45,7 +45,7 @@ def __span_method_on(klass, method, name = nil, type = nil) ElasticAPM.with_span name.to_s, type.to_s do super(*args, &block) end - end + end) end) end end diff --git a/spec/elastic_apm/span_helpers_spec.rb b/spec/elastic_apm/span_helpers_spec.rb index 4e6009846..8d8583cfa 100644 --- a/spec/elastic_apm/span_helpers_spec.rb +++ b/spec/elastic_apm/span_helpers_spec.rb @@ -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 @@ -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