Module: NewRelic::LanguageSupport

Extended by:
LanguageSupport
Included in:
LanguageSupport
Defined in:
lib/new_relic/language_support.rb

Constant Summary collapse

@@forkable =
nil

Instance Method Summary collapse

Instance Method Details

#bundled_gem?(gem_name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/new_relic/language_support.rb', line 90

def bundled_gem?(gem_name)
  defined?(Bundler) && Bundler.rubygems.all_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
end

#camelize(string) ⇒ Object



76
77
78
79
# File 'lib/new_relic/language_support.rb', line 76

def camelize(string)
  camelized = string.downcase
  camelized.split(/\-|\_/).map(&:capitalize).join
end

#camelize_with_first_letter_downcased(string) ⇒ Object



81
82
83
84
# File 'lib/new_relic/language_support.rb', line 81

def camelize_with_first_letter_downcased(string)
  camelized = camelize(string)
  camelized[0].downcase.concat(camelized[1..-1])
end

#can_fork?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/new_relic/language_support.rb', line 10

def can_fork?
  # this is expensive to check, so we should only check once
  return @@forkable if !@@forkable.nil?

  @@forkable = Process.respond_to?(:fork)
end

#constantize(const_name) ⇒ Object

TODO: OLD RUBIES - RUBY_VERSION < 2.6

Ruby 2.6 introduced an improved version of ‘Object.const_get` that respects the full namespace of the input and doesn’t just grab the first constant matching the string to the right of the last ‘::’. Once we drop support for Ruby 2.5 and below, the only value this custom method will provide beyond ‘Object.const_get` itself is to automatically catch NameError.

see: github.com/rails/rails/commit/7057ccf6565c1cb5354c1906880119276a9d15c0

With Ruby 2.6+, this method can be defined like so: def constantize(constant_as_string_or_symbol)

Object.const_get(constant_as_string_or_symbol)

rescue NameError end



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/new_relic/language_support.rb', line 55

def constantize(const_name)
  const_name.to_s.sub(/\A::/, '').split('::').inject(Object) do |namespace, name|
    begin
      result = namespace.const_get(name)

      # const_get looks up the inheritance chain, so if it's a class
      # in the constant make sure we found the one in our namespace.
      #
      # Can't help if the constant isn't a class...
      if result.is_a?(Module)
        expected_name = "#{namespace}::#{name}".gsub(/^Object::/, '')
        return unless expected_name == result.to_s
      end

      result
    rescue NameError
      nil
    end
  end
end

#gc_profiler_enabled?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/new_relic/language_support.rb', line 21

def gc_profiler_enabled?
  gc_profiler_usable? && ::GC::Profiler.enabled? && !::NewRelic::Agent.config[:disable_gc_profiler]
end

#gc_profiler_usable?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/new_relic/language_support.rb', line 17

def gc_profiler_usable?
  defined?(::GC::Profiler) && !jruby?
end

#jruby?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/new_relic/language_support.rb', line 34

def jruby?
  RUBY_ENGINE == 'jruby'
end

#object_space_usable?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/new_relic/language_support.rb', line 25

def object_space_usable?
  if jruby?
    require 'jruby'
    JRuby.runtime.is_object_space_enabled
  else
    defined?(::ObjectSpace)
  end
end

#snakeize(string) ⇒ Object



86
87
88
# File 'lib/new_relic/language_support.rb', line 86

def snakeize(string)
  string.gsub(/(.)([A-Z])/, '\1_\2').downcase
end