module Instana::Util

Constants

ID_RANGE

Public Class Methods

generate_id(size = 1) click to toggle source

Generate a random 64bit/128bit ID

@param size [Integer] Number of 64 bit integers used to generate the id

@return [String] a random 64bit/128bit hex encoded string

# File lib/instana/util.rb, line 134
def generate_id(size = 1)
  Array.new(size) { rand(ID_RANGE) }
    .pack('q>*')
    .unpack('H*')
    .first
end
get_app_name() click to toggle source

Best effort to determine a name for the instrumented application on the dashboard.

:nocov:

# File lib/instana/util.rb, line 72
def get_app_name
  if ENV.key?('INSTANA_SERVICE_NAME')
    return ENV['INSTANA_SERVICE_NAME']
  end

  if defined?(::Resque)
    # Just because Resque is defined doesn't mean this is a resque process necessarily
    # Check arguments for a match
    if ($0 =~ /resque-#{Resque::Version}/)
      return "Resque Worker"
    elsif ($0 =~ /resque-pool-master/)
      return "Resque Pool Master"
    elsif ($0 =~ /resque-scheduler/)
      return "Resque Scheduler"
    end
  end

  if defined?(::RailsLts) || defined?(::Rails)
    return Rails.application.class.to_s.split('::')[0]
  end

  if $0.to_s.empty?
    return "Ruby"
  end

  exe = File.basename($0)
  if exe == "rake"
    return "Rake"
  end

  return exe
rescue Exception => e
  Instana.logger.info "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
  Instana.logger.debug { e.backtrace.join("\r\n") }
  return "Ruby"
end
get_rb_source(file) click to toggle source

Retrieves and returns the source code for any ruby files requested by the UI via the host agent

@param file [String] The fully qualified path to a file

# File lib/instana/util.rb, line 13
def get_rb_source(file)
  if (file =~ /.rb$/).nil?
    { :error => "Only Ruby source files are allowed. (*.rb)" }
  else
    { :data => File.read(file) }
  end
rescue => e
  return { :error => e.inspect }
end
header_to_id(given) click to toggle source

Convert a received header value into a valid ID

@param header_id [String] the header value to be converted

@return [String]

# File lib/instana/util.rb, line 159
def header_to_id(given)
  return '' unless given.is_a?(String)
  return '' unless given.match(/\A[a-z\d]{16,32}\z/i)
  given
end
id_to_header(id) click to toggle source

Convert an ID to a value appropriate to pass in a header.

@param id [String] the id to be converted

@return [String]

# File lib/instana/util.rb, line 147
def id_to_header(id)
  return '' unless id.is_a?(String)
  # Only send 64bit IDs downstream for now
  id.length == 32 ? id[16..-1] : id
end
now_in_ms() click to toggle source

Get the current time in milliseconds from the epoch

@return [Integer] the current time in milliseconds

# File lib/instana/util.rb, line 114
def now_in_ms
  Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
end
Also aliased as: ts_now
take_snapshot() click to toggle source

Method to collect up process info for snapshots. This is generally used once per process.

:nocov:

# File lib/instana/util.rb, line 27
def take_snapshot
  data = {}

  data[:sensorVersion] = ::Instana::VERSION
  data[:ruby_version] = RUBY_VERSION
  data[:rpl] = RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)

  # Framework Detection
  if defined?(::RailsLts::VERSION)
    data[:framework] = "Rails on Rails LTS-#{::RailsLts::VERSION}"

  elsif defined?(::Rails.version)
    data[:framework] = "Ruby on Rails #{::Rails.version}"

  elsif defined?(::Grape::VERSION)
    data[:framework] = "Grape #{::Grape::VERSION}"

  elsif defined?(::Padrino::VERSION)
    data[:framework] = "Padrino #{::Padrino::VERSION}"

  elsif defined?(::Sinatra::VERSION)
    data[:framework] = "Sinatra #{::Sinatra::VERSION}"
  end

  # Report Bundle
  if defined?(::Gem) && Gem.respond_to?(:loaded_specs)
    data[:versions] = {}

    Gem.loaded_specs.each do |k, v|
      data[:versions][k] = v.version.to_s
    end
  end

  data
rescue => e
  ::Instana.logger.debug { "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" }
  ::Instana.logger.debug { e.backtrace.join("\r\n") }
  return data
end
time_to_ms(time) click to toggle source

Convert a Time value to milliseconds

@param time [Time]

# File lib/instana/util.rb, line 124
def time_to_ms(time)
  (time.to_f * 1000).floor
end
ts_now()

Prior method name support. To be deprecated when appropriate.

Alias for: now_in_ms