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)



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

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)


84
85
86
87
88
89
90
91
# File 'lib/new_relic/helper.rb', line 84

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

#fetch_metadata(uri, headers) ⇒ Object



50
51
52
53
54
55
# File 'lib/new_relic/helper.rb', line 50

def (uri, headers)
  uri = URI(uri)
  Net::HTTP.start(uri.host, uri.port, open_timeout: 1, read_timeout: 1) do |http|
    http.request(Net::HTTP::Get.new(uri, headers))
  end
end

#instance_method_visibility(klass, method_name) ⇒ Object



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

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)


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

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

#normalize_version(version) ⇒ Object



100
101
102
103
104
105
# File 'lib/new_relic/helper.rb', line 100

def normalize_version(version)
  return Gem::Version.new('0') if version.nil? || version.to_s.empty?
  return version if version.is_a?(Gem::Version)

  Gem::Version.new(version.to_s)
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



124
125
126
127
128
129
130
131
132
# File 'lib/new_relic/helper.rb', line 124

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



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

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

  output&.chomp
end

#time_to_millis(time) ⇒ Object



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

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

#version_equals?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/new_relic/helper.rb', line 115

def version_equals?(left, right)
  version_satisfied?(left, :==, right)
end

#version_greater_than?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/new_relic/helper.rb', line 107

def version_greater_than?(left, right)
  version_satisfied?(left, :>, right)
end

#version_less_than?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/new_relic/helper.rb', line 111

def version_less_than?(left, right)
  version_satisfied?(left, :<, right)
end

#version_satisfied?(left, operator, right) ⇒ Boolean

Returns:

  • (Boolean)


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

def version_satisfied?(left, operator, right)
  left = normalize_version(left)
  right = normalize_version(right)

  left.public_send(operator, right)
end