Skip to content

Commit

Permalink
Add Bundler version conditions (#2823)
Browse files Browse the repository at this point in the history
* Add Bundler version conditions

Co-authored-by: James Bunch <[email protected]>
  • Loading branch information
hannahramadan and fallwith authored Aug 21, 2024
1 parent 77b0838 commit a885001
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/new_relic/agent/instrumentation/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/new_relic/control/frameworks/rails4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/new_relic/environment_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/new_relic/language_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a885001

Please sign in to comment.