lib/instana/agent.rb in instana-0.8.6 vs lib/instana/agent.rb in instana-0.9.0.pre.slywolf3

- old
+ new

@@ -6,10 +6,11 @@ include Sys module Instana class Agent attr_accessor :state + attr_accessor :agent_uuid LOCALHOST = '127.0.0.1'.freeze MIME_JSON = 'application/json'.freeze DISCOVERY_PATH = 'com.instana.plugin.ruby.discovery'.freeze @@ -60,14 +61,12 @@ # This is usually Process.pid but in the case of docker, the host agent # will return to us the true host pid in which we use to report data. @process[:report_pid] = nil end - ## - # start + # Sets up periodic timers and starts the agent in a background thread. # - # def start # 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 @@ -88,10 +87,11 @@ if (Time.now - @entity_last_seen) > 60 ::Instana.logger.debug "Metrics reporting failed for >1 min. Falling back to unannounced state." transition_to(:unannounced) end end + ::Instana.processor.send end end # Start the background ruby sensor thread. It works off of timers and # is sleeping otherwise @@ -107,13 +107,26 @@ @timers.wait } end end - ## - # announce_sensor + # Indicates if the agent is ready to send metrics + # or data. # + def ready? + # In test, we're always ready :-) + return true if ENV['INSTANA_GEM_TEST'] + + @state == :announced + end + + # Returns the PID that we are reporting to + # + def report_pid + @process[:report_pid] + end + # Collect process ID, name and arguments to notify # the host agent. # def announce_sensor announce_payload = {} @@ -140,15 +153,14 @@ Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" Instana.logger.debug e.backtrace.join("\r\n") return false end - ## - # report_entity_data - # # Method to report metrics data to the host agent. # + # @param paylod [Hash] The collection of metrics to report. + # def report_entity_data(payload) with_snapshot = false path = "com.instana.plugin.ruby.#{@process[:report_pid]}" uri = URI.parse("http://#{@host}:#{@port}/#{path}") req = Net::HTTP::Post.new(uri) @@ -185,13 +197,40 @@ rescue => e Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" Instana.logger.debug e.backtrace.join("\r\n") end - ## - # host_agent_ready? + # Accept and report spans to the host agent. # + # @param traces [Array] An array of [Span] + # @return [Boolean] + # + def report_spans(spans) + return unless @state == :announced + + path = "com.instana.plugin.ruby/traces.#{@process[:report_pid]}" + uri = URI.parse("http://#{@host}:#{@port}/#{path}") + req = Net::HTTP::Post.new(uri) + + req.body = spans.to_json + 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 + rescue => e + Instana.logger.debug "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" + Instana.logger.debug e.backtrace.join("\r\n") + end + # Check that the host agent is available and can be contacted. This will # first check localhost and if not, then attempt on the default gateway # for docker in bridged mode. It will save where it found the host agent # in @host that is used in subsequent HTTP calls. # @@ -227,16 +266,16 @@ return false end private - ## - # transition_to - # # Handles any/all steps required in the transtion # between states. # + # @param state [Symbol] Can be 1 of 2 possible states: + # `:announced`, `:unannounced` + # def transition_to(state) case state when :announced # announce successful; set state @state = :announced @@ -252,17 +291,17 @@ else ::Instana.logger.warn "Uknown agent state: #{state}" end end - ## - # make host_agent_request - # # Centralization of the net/http communications # with the host agent. Pass in a prepared <req> # of type Net::HTTP::Get|Put|Head # + # @param req [Net::HTTP::Req] A prepared Net::HTTP request object of the type + # you wish to make (Get, Put, Post etc.) + # def make_host_agent_request(req) req['Accept'] = MIME_JSON req['Content-Type'] = MIME_JSON response = nil @@ -276,35 +315,26 @@ Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" Instana.logger.debug e.backtrace.join("\r\n") return nil end - ## - # pid_namespace? - # # Indicates whether we are running in a pid namespace (such as # Docker). # def pid_namespace? return false unless @is_linux Process.pid != get_real_pid end - ## - # get_real_pid - # # 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 end - ## - # take_snapshot - # # Method to collect up process info for snapshots. This # is generally used once per process. # def take_snapshot data = {}