Module: AgentHelpers

Included in:
Instana::Agent
Defined in:
lib/instana/agent/helpers.rb

Instance Method Summary collapse

Instance Method Details

#forked?Boolean

Determine whether the pid has changed since Agent start.

@ return [Boolean] true or false to indicate if forked

Returns:

  • (Boolean)


36
37
38
# File 'lib/instana/agent/helpers.rb', line 36

def forked?
  @process[:pid] != Process.pid
end

#get_real_pidObject

Attempts to determine the true process ID by querying the /proc/<pid>/sched file. This works on linux currently.

Raises:

  • (RuntimeError)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/instana/agent/helpers.rb', line 13

def get_real_pid
  raise RuntimeError.new("Unsupported platform: get_real_pid") unless @is_linux

  sched_file = "/proc/#{Process.pid}/sched"
  pid = Process.pid

  if File.exist?(sched_file)
    v = File.open(sched_file, &:readline)
    pid = v.match(/\d+/).to_s.to_i
  end
  pid
end

#pid_namespace?Boolean

Indicates whether we are running in a pid namespace (such as Docker).

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/instana/agent/helpers.rb', line 5

def pid_namespace?
  return false unless @is_linux
  Process.pid != get_real_pid
end

#ready?Boolean

Indicates if the agent is ready to send metrics and/or data.

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/instana/agent/helpers.rb', line 43

def ready?
  # In test, we're always ready :-)
  return true if ENV.key?('INSTANA_TEST')

  # Occasionally Run Fork detection
  if rand(10) > 8
    if !@is_resque_worker && (@process[:pid] != Process.pid)
      ::Instana.logger.debug "Instana: detected fork. (this pid: #{Process.pid}/#{Process.ppid})  Calling after_fork"
      after_fork
    end
  end

  @state == :announced
rescue => e
  Instana.logger.debug { "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" }
  Instana.logger.debug { e.backtrace.join("\r\n") } unless ENV.key?('INSTANA_TEST')
  return false
end

#report_pidObject

Returns the PID that we are reporting to



28
29
30
# File 'lib/instana/agent/helpers.rb', line 28

def report_pid
  @process[:report_pid]
end