Module: NewRelic::Helper

Overview

A singleton for shared generic helper methods

Instance Method Summary collapse

Instance Method Details

#correctly_encoded(string) ⇒ Object

Confirm a string is correctly encoded, If not force the encoding to ASCII-8BIT (binary)



18
19
20
21
22
23
24
# File 'lib/new_relic/helper.rb', line 18

def correctly_encoded(string)
  return string unless string.is_a?(String)

  # The .dup here is intentional, since force_encoding mutates the target,
  # and we don't know who is going to use this string downstream of us.
  string.valid_encoding? ? string : string.dup.force_encoding(Encoding::ASCII_8BIT)
end

#executable_in_path?(executable) ⇒ Boolean

TODO: Open3 defers the actual execution of a binary to Process.spawn,

which will raise an Errno::ENOENT exception for a file that
cannot be found. We might want to take the time to evaluate
relying on that Process.spawn behavior instead of checking for
existence ourselves. We'd need to see what it does, how efficient
it is, if it differs in functionality between Ruby versions and
operating systems, etc.

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/new_relic/helper.rb', line 77

def executable_in_path?(executable)
  return false unless ENV['PATH']

  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |bin_path|
    executable_path = File.join(bin_path, executable)
    File.exist?(executable_path) && File.file?(executable_path) && File.executable?(executable_path)
  end
end

#instance_method_visibility(klass, method_name) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/new_relic/helper.rb', line 26

def instance_method_visibility(klass, method_name)
  if klass.private_instance_methods.map { |s| s.to_sym }.include?(method_name.to_sym)
    :private
  elsif klass.protected_instance_methods.map { |s| s.to_sym }.include?(method_name.to_sym)
    :protected
  else
    :public
  end
end

#instance_methods_include?(klass, method_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/new_relic/helper.rb', line 36

def instance_methods_include?(klass, method_name)
  method_name_sym = method_name.to_sym
  (
    klass.instance_methods.map { |s| s.to_sym }.include?(method_name_sym) ||
    klass.protected_instance_methods.map { |s| s.to_sym }.include?(method_name_sym) ||
    klass.private_instance_methods.map { |s| s.to_sym }.include?(method_name_sym)
  )
end

#rubygems_specsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



91
92
93
94
95
96
97
98
99
# File 'lib/new_relic/helper.rb', line 91

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

#run_command(command) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/new_relic/helper.rb', line 49

def run_command(command)
  executable = command.split(' ').first
  unless executable_in_path?(executable)
    raise NewRelic::CommandExecutableNotFoundError.new("Executable not found: '#{executable}'")
  end

  exception = nil
  begin
    output, status = Open3.capture2e(command)
  rescue => exception
  end

  if exception || !status.success?
    message = exception ? "#{exception.class} - #{exception.message}" : output
    raise NewRelic::CommandRunFailedError.new("Failed to run command '#{command}': #{message}")
  end

  # needs else branch coverage
  output.chomp if output # rubocop:disable Style/SafeNavigation
end

#time_to_millis(time) ⇒ Object



45
46
47
# File 'lib/new_relic/helper.rb', line 45

def time_to_millis(time)
  (time.to_f * 1000).round
end