lib/instana/agent.rb in instana-0.10.1 vs lib/instana/agent.rb in instana-0.11.0

- old
+ new

@@ -92,11 +92,11 @@ # Used post fork to re-initialize state and restart communications with # the host agent. # def after_fork - ::Instana.logger.debug "after_fork hook called. Falling back to unannounced state and spawning a new background agent thread." + ::Instana.logger.agent "after_fork hook called. Falling back to unannounced state and spawning a new background agent thread." # Re-collect process information post fork @pid = Process.pid collect_process_info @@ -135,11 +135,11 @@ # The announce timer # We attempt to announce this ruby sensor to the host agent. # In case of failure, we try again in 30 seconds. @announce_timer = @timers.now_and_every(30) do if host_agent_ready? && announce_sensor - ::Instana.logger.debug "Announce successful. Switching to metrics collection. pid: #{Process.pid}" + ::Instana.logger.debug "Announce successful. Switching to metrics collection." transition_to(:announced) end end # The collect timer @@ -163,10 +163,11 @@ # Starts the timer loop for the timers that were initialized # in the setup method. This is blocking and should only be # called from an already initialized background thread. # def start + ::Instana.logger.warn "Host agent not available. Will retry periodically." unless host_agent_ready? loop do if @state == :unannounced @collect_timer.pause @announce_timer.resume else @@ -174,22 +175,30 @@ @collect_timer.resume end @timers.wait end ensure - Instana.logger.debug "Agent start method exiting. state: #{@state} pid: #{Process.pid}" + if @state == :announced + # Pause the timers so they don't fire while we are + # reporting traces + @collect_timer.pause + @announce_timer.pause + + ::Instana.logger.debug "Agent exiting. Reporting final #{::Instana.processor.queue_count} trace(s)." + ::Instana.processor.send + end end # Indicates if the agent is ready to send metrics # and/or data. # def ready? # In test, we're always ready :-) return true if ENV['INSTANA_GEM_TEST'] if forked? - ::Instana.logger.debug "Instana: detected fork. Calling after_fork" + ::Instana.logger.agent "Instana: detected fork. Calling after_fork" after_fork end @state == :announced rescue => e @@ -214,11 +223,11 @@ uri = URI.parse("http://#{@host}:#{@port}/#{DISCOVERY_PATH}") req = Net::HTTP::Put.new(uri) req.body = announce_payload.to_json - # ::Instana.logger.debug "Announce: http://#{@host}:#{@port}/#{DISCOVERY_PATH} - payload: #{req.body}" + ::Instana.logger.agent "Announce: http://#{@host}:#{@port}/#{DISCOVERY_PATH} - payload: #{req.body}" response = make_host_agent_request(req) if response && (response.code.to_i == 200) data = JSON.parse(response.body) @@ -261,12 +270,10 @@ response = make_host_agent_request(req) if response last_entity_response = response.code.to_i - #::Instana.logger.debug "entity http://#{@host}:#{@port}/#{path}: response=#{last_entity_response}: #{payload.to_json}" - if last_entity_response == 200 @entity_last_seen = Time.now @last_snapshot = Time.now if with_snapshot return true @@ -294,12 +301,10 @@ response = make_host_agent_request(req) if response last_trace_response = response.code.to_i - #::Instana.logger.debug "traces response #{last_trace_response}: #{spans.to_json}" - if [200, 204].include?(last_trace_response) return true end end false @@ -352,10 +357,11 @@ # # @param state [Symbol] Can be 1 of 2 possible states: # `:announced`, `:unannounced` # def transition_to(state) + ::Instana.logger.agent("Transitioning to #{state}") case state when :announced # announce successful; set state @state = :announced @@ -389,9 +395,10 @@ response = nil Net::HTTP.start(req.uri.hostname, req.uri.port, :open_timeout => 1, :read_timeout => 1) do |http| response = http.request(req) end + ::Instana.logger.agent_comm "#{req.method} Req -> -body-: #{req.uri} -> -#{req.body}- Resp -> body:#{response} -> -#{response.body}-" response rescue Errno::ECONNREFUSED => e return nil rescue => e Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"