Module: Instana::Util

Defined in:
lib/instana/util.rb

Constant Summary collapse

ID_RANGE =
-2**63..2**63-1

Class Method Summary collapse

Class Method Details

.generate_id(size = 1) ⇒ String

Generate a random 64bit/128bit ID

Parameters:

  • size (Integer) (defaults to: 1)

    Number of 64 bit integers used to generate the id

Returns:

  • (String)

    a random 64bit/128bit hex encoded string



143
144
145
146
147
148
# File 'lib/instana/util.rb', line 143

def generate_id(size = 1)
  Array.new(size) { rand(ID_RANGE) }
    .pack('q>*')
    .unpack('H*')
    .first
end

.get_app_nameObject

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

:nocov:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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

  rails_module = if defined?(::RailsLts)
                   then ::RailsLts
                 elsif defined?(::Rails)
                   then ::Rails
                 end

  if rails_module &&
     rails_module.respond_to?(:application_name) &&
     rails_module.instance_variables.include?(:@application) &&
     rails_module.application
    return rails_module.application_name
  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) ⇒ Object

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

Parameters:

  • file (String)

    The fully qualified path to a file



13
14
15
16
17
18
19
20
21
# 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) ⇒ String

Convert a received header value into a valid ID

Parameters:

  • header_id (String)

    the header value to be converted

Returns:

  • (String)


168
169
170
171
172
# File 'lib/instana/util.rb', line 168

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) ⇒ String

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

Parameters:

  • id (String)

    the id to be converted

Returns:

  • (String)


156
157
158
159
160
# File 'lib/instana/util.rb', line 156

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

.maybe_timeout(timeout, start_time) ⇒ Object



178
179
180
181
182
183
# File 'lib/instana/util.rb', line 178

def maybe_timeout(timeout, start_time)
  return nil if timeout.nil?

  timeout -= (timeout_timestamp - start_time)
  timeout.positive? ? timeout : 0
end

.now_in_msInteger Also known as: ts_now

Get the current time in milliseconds from the epoch

Returns:

  • (Integer)

    the current time in milliseconds



123
124
125
# File 'lib/instana/util.rb', line 123

def now_in_ms
  Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
end

.take_snapshotObject

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

:nocov:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Convert a Time value to milliseconds

Parameters:

  • time (Time)


133
134
135
# File 'lib/instana/util.rb', line 133

def time_to_ms(time)
  (time.to_f * 1000).floor
end

.timeout_timestampObject



174
175
176
# File 'lib/instana/util.rb', line 174

def timeout_timestamp
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end