Module: NewRelic::Helper
- Extended by:
- Helper
- Included in:
- Agent::Instrumentation::Memcache::Chain, Agent::Instrumentation::Memcache::Dalli, Agent::Instrumentation::Memcache::DalliCAS, Helper
- Defined in:
- lib/new_relic/helper.rb
Overview
A singleton for shared generic helper methods
Instance Method Summary collapse
-
#correctly_encoded(string) ⇒ Object
Confirm a string is correctly encoded, If not force the encoding to ASCII-8BIT (binary).
-
#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.
- #instance_method_visibility(klass, method_name) ⇒ Object
- #instance_methods_include?(klass, method_name) ⇒ Boolean
- #run_command(command) ⇒ Object
- #time_to_millis(time) ⇒ Object
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.
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
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 |
#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? = exception ? "#{exception.class} - #{exception.}" : output raise NewRelic::CommandRunFailedError.new("Failed to run command '#{command}': #{}") 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 |