From a885001e7f7bef3a7f1824c822b88ae16053c4b7 Mon Sep 17 00:00:00 2001 From: Hannah Ramadan <76922290+hannahramadan@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:13:05 -0700 Subject: [PATCH] Add Bundler version conditions (#2823) * Add Bundler version conditions Co-authored-by: James Bunch --- lib/new_relic/agent/instrumentation/grape.rb | 4 +++- lib/new_relic/control/frameworks/rails4.rb | 6 ++++-- lib/new_relic/environment_report.rb | 6 +++++- lib/new_relic/language_support.rb | 8 +++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/new_relic/agent/instrumentation/grape.rb b/lib/new_relic/agent/instrumentation/grape.rb index 26198326d0..0167794b3c 100644 --- a/lib/new_relic/agent/instrumentation/grape.rb +++ b/lib/new_relic/agent/instrumentation/grape.rb @@ -19,7 +19,9 @@ depends_on do begin - if defined?(Bundler) && Bundler.rubygems.all_specs.map(&:name).include?('newrelic-grape') + if defined?(Bundler) && + ((Gem::Version.new(Bundler::VERSION) >= Gem::Version.new('2.0.0') && Bundler.rubygems.installed_specs.map(&:name).include?('newrelic-grape')) || + Bundler.rubygems.all_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 955749d5d3..3b13d588c1 100644 --- a/lib/new_relic/control/frameworks/rails4.rb +++ b/lib/new_relic/control/frameworks/rails4.rb @@ -9,8 +9,10 @@ class Control module Frameworks class Rails4 < NewRelic::Control::Frameworks::Rails3 def rails_gem_list - Bundler.rubygems.all_specs.map do |gem| - "#{gem.name} (#{gem.version})" + if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new('2.0.0') + Bundler.rubygems.installed_specs.map { |gem| "#{gem.name} (#{gem.version})" } + else + Bundler.rubygems.all_specs.map { |gem| "#{gem.name} (#{gem.version})" } end end diff --git a/lib/new_relic/environment_report.rb b/lib/new_relic/environment_report.rb index 0b6f76290f..7d2b487fc0 100644 --- a/lib/new_relic/environment_report.rb +++ b/lib/new_relic/environment_report.rb @@ -44,7 +44,11 @@ def self.registered_reporters=(logic) #################################### report_on('Gems') do begin - Bundler.rubygems.all_specs.map { |gem| "#{gem.name}(#{gem.version})" } + if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new('2.0.0') + Bundler.rubygems.installed_specs.map { |gem| "#{gem.name}(#{gem.version})" } + else + Bundler.rubygems.all_specs.map { |gem| "#{gem.name}(#{gem.version})" } + end 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/language_support.rb b/lib/new_relic/language_support.rb index 612bc38d68..438d899aa4 100644 --- a/lib/new_relic/language_support.rb +++ b/lib/new_relic/language_support.rb @@ -88,7 +88,13 @@ def snakeize(string) end def bundled_gem?(gem_name) - defined?(Bundler) && Bundler.rubygems.all_specs.map(&:name).include?(gem_name) + return false unless defined?(Bundler) + + if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new('2.0.0') + Bundler.rubygems.installed_specs.map(&:name).include?(gem_name) + else + Bundler.rubygems.all_specs.map(&:name).include?(gem_name) + end rescue => e ::NewRelic::Agent.logger.info("Could not determine if third party #{gem_name} gem is installed", e) false