Module: OneApm::Helper

Extended by:
Helper
Included in:
Helper
Defined in:
lib/one_apm/support/helper.rb

Constant Summary collapse

HEX_DIGITS =
(0..15).map{|i| i.to_s(16)}
GUID_LENGTH =
16

Instance Method Summary collapse

Instance Method Details

#correctly_encoded(string) ⇒ Object



10
11
12
13
14
15
# File 'lib/one_apm/support/helper.rb', line 10

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("ASCII-8BIT")
end

#generate_guidObject

generate a random 16 length uuid



53
54
55
56
57
58
59
# File 'lib/one_apm/support/helper.rb', line 53

def generate_guid
  guid = ''
  GUID_LENGTH.times do |a|
    guid << HEX_DIGITS[rand(16)]
  end
  guid
end

#instance_method_visibility(klass, method_name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/one_apm/support/helper.rb', line 22

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)


32
33
34
35
36
37
38
39
# File 'lib/one_apm/support/helper.rb', line 32

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

#milliseconds_to_seconds(milliseconds) ⇒ Object



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

def milliseconds_to_seconds(milliseconds)
  milliseconds / 1000.0
end

#obfuscatorObject



61
62
63
# File 'lib/one_apm/support/helper.rb', line 61

def obfuscator
  @obfuscator ||= OneApm::Agent::Obfuscator.new(OneApm::Manager.config[:encoding_key])
end

#time_to_millis(time) ⇒ Object



41
42
43
# File 'lib/one_apm/support/helper.rb', line 41

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