lib/instana/agent.rb in instana-1.3.3 vs lib/instana/agent.rb in instana-1.4.0

- old
+ new

@@ -1,10 +1,11 @@ -require 'net/http' -require 'uri' require 'json' -require 'timers' +require 'net/http' +require 'socket' require 'sys/proctable' +require 'timers' +require 'uri' include Sys module Instana class Agent attr_accessor :state @@ -171,10 +172,19 @@ announce_payload = {} announce_payload[:pid] = pid_namespace? ? get_real_pid : Process.pid announce_payload[:args] = @process[:arguments] + + if @is_linux && !::Instana.test? + # We create an open socket to the host agent in case we are running in a container + # and the real pid needs to be detected. + socket = TCPSocket.new @discovered[:agent_host], @discovered[:agent_port] + announce_payload[:fd] = socket.fileno + announce_payload[:inode] = File.readlink("/proc/#{Process.pid}/fd/#{socket.fileno}") + end + uri = URI.parse("http://#{@discovered[:agent_host]}:#{@discovered[:agent_port]}/#{DISCOVERY_PATH}") req = Net::HTTP::Put.new(uri) req.body = announce_payload.to_json ::Instana.logger.agent "Announce: http://#{@discovered[:agent_host]}:#{@discovered[:agent_port]}/#{DISCOVERY_PATH} - payload: #{req.body}" @@ -191,10 +201,12 @@ end rescue => e Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" Instana.logger.debug e.backtrace.join("\r\n") return false + ensure + socket.close if socket end # Method to report metrics data to the host agent. # # @param paylod [Hash] The collection of metrics to report. @@ -452,11 +464,18 @@ # Attempts to determine the true process ID by querying the # /proc/<pid>/sched file. This works on linux currently. # def get_real_pid raise RuntimeError.new("Unsupported platform: get_real_pid") unless @is_linux - v = File.open("/proc/#{Process.pid}/sched", &:readline) - v.match(/\d+/).to_s.to_i + + 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 # Determine whether the pid has changed since Agent start. # # @ return [Boolean] true or false to indicate if forked