diff --git a/lib/new_relic/agent/instrumentation/grape.rb b/lib/new_relic/agent/instrumentation/grape.rb index c4b24d7379..75bae0a0df 100644 --- a/lib/new_relic/agent/instrumentation/grape.rb +++ b/lib/new_relic/agent/instrumentation/grape.rb @@ -19,13 +19,7 @@ depends_on do begin - specs = if Bundler.rubygems.respond_to?(:installed_specs) - Bundler.rubygems.installed_specs - else - Bundler.rubygems.all_specs - end - - if specs.map(&:name).include?('newrelic-grape') + if NewRelic::Helper.rubygems_specs.map(&:name).include?('newrelic-grape') NewRelic::Agent.logger.info('Not installing New Relic supported Grape instrumentation because the third party newrelic-grape gem is present') false else diff --git a/lib/new_relic/control/frameworks/rails4.rb b/lib/new_relic/control/frameworks/rails4.rb index 23d403dd69..b8545f92df 100644 --- a/lib/new_relic/control/frameworks/rails4.rb +++ b/lib/new_relic/control/frameworks/rails4.rb @@ -9,11 +9,7 @@ class Control module Frameworks class Rails4 < NewRelic::Control::Frameworks::Rails3 def rails_gem_list - if Bundler.rubygems.respond_to?(:installed_specs) - Bundler.rubygems.installed_specs.map { |gem| "#{gem.name} (#{gem.version})" } - else - Bundler.rubygems.all_specs.map { |gem| "#{gem.name} (#{gem.version})" } - end + NewRelic::Helper.rubygems_specs.map { |gem| "#{gem.name} (#{gem.version})" } end def append_plugin_list diff --git a/lib/new_relic/environment_report.rb b/lib/new_relic/environment_report.rb index 1661bebeb6..301fe83c0f 100644 --- a/lib/new_relic/environment_report.rb +++ b/lib/new_relic/environment_report.rb @@ -44,11 +44,7 @@ def self.registered_reporters=(logic) #################################### report_on('Gems') do begin - if Bundler.rubygems.respond_to?(:installed_specs) - Bundler.rubygems.installed_specs.map { |gem| "#{gem.name}(#{gem.version})" } - else - Bundler.rubygems.all_specs.map { |gem| "#{gem.name}(#{gem.version})" } - end + NewRelic::Helper.rubygems_specs.map { |gem| "#{gem.name}(#{gem.version})" } rescue # There are certain rubygem, bundler, rails combinations (e.g. gem # 1.6.2, rails 2.3, bundler 1.2.3) where the code above throws an error diff --git a/lib/new_relic/helper.rb b/lib/new_relic/helper.rb index 7d17c89908..3f89b9e36b 100644 --- a/lib/new_relic/helper.rb +++ b/lib/new_relic/helper.rb @@ -82,5 +82,20 @@ def executable_in_path?(executable) File.exist?(executable_path) && File.file?(executable_path) && File.executable?(executable_path) end end + + # Bundler version 2.5.12 deprecated all_specs and added installed_specs. + # To support newer Bundler versions, try to use installed_specs first, + # then fall back to all_specs. + # All callers expect this to be an array, so return an array if Bundler isn't defined + # @api private + def rubygems_specs + return [] unless defined?(Bundler) + + if Bundler.rubygems.respond_to?(:installed_specs) + Bundler.rubygems.installed_specs + else + Bundler.rubygems.all_specs + end + end end end diff --git a/lib/new_relic/language_support.rb b/lib/new_relic/language_support.rb index 140d2455d2..31377e831a 100644 --- a/lib/new_relic/language_support.rb +++ b/lib/new_relic/language_support.rb @@ -90,11 +90,7 @@ def snakeize(string) def bundled_gem?(gem_name) return false unless defined?(Bundler) - if Bundler.rubygems.respond_to?(:installed_specs) - Bundler.rubygems.installed_specs.map(&:name).include?(gem_name) - else - Bundler.rubygems.all_specs.map(&:name).include?(gem_name) - end + NewRelic::Helper.rubygems_specs.map(&:name).include?(gem_name) rescue => e ::NewRelic::Agent.logger.info("Could not determine if third party #{gem_name} gem is installed", e) false diff --git a/test/new_relic/helper_test.rb b/test/new_relic/helper_test.rb index e47a838a40..44a66f4480 100644 --- a/test/new_relic/helper_test.rb +++ b/test/new_relic/helper_test.rb @@ -77,4 +77,26 @@ def test_run_command_sad_exception NewRelic::Helper.run_command('executable that existed at detection time but is not there now') end end + + ## rubygems_specs + def test_rubygems_specs_returns_empty_array_without_bundler + stub(:defined?, nil, ['Bundler']) do + result = NewRelic::Helper.rubygems_specs + + assert_instance_of Array, result + assert_empty Array, result + end + end + + def test_rubygems_specs_works_with_all_specs_when_installed_specs_is_absent + Bundler.rubygems.stub(:respond_to?, nil, [':installed_specs']) do + assert_equal Bundler.rubygems.all_specs, NewRelic::Helper.rubygems_specs + end + end + + def test_rubygems_specs_works_with_installed_specs + skip 'running a version of Bundler that has not defined installed_specs' unless Bundler.rubygems.respond_to?(:installed_specs) + + assert_equal Bundler.rubygems.installed_specs, NewRelic::Helper.rubygems_specs + end end