class TingYun::Agent::Agent

Attributes

cross_app_monitor[RW]

service for communicating with collector

events[R]
middleware[RW]

service for communicating with collector

service[RW]

service for communicating with collector

Public Class Methods

new() click to toggle source
# File lib/ting_yun/agent/agent.rb, line 36
def initialize
  @started = false
  @environment_report = nil
  @service = TingYunService.new
  @connect_state = :first #[:first, :pending, :connected, :disconnected]
  @events  = TingYun::Agent::Event::EventListener.new
  @after_fork_lock = Mutex.new
  @dispatcher = TingYun::Agent::Dispatcher.new(@events)
  @cross_app_monitor = TingYun::Agent::CrossAppMonitor.new(@events)
  @middleware = TingYun::Agent::Collector::MiddleWareCollector.new(@events)

  init_containers
end

Public Instance Methods

connect!(option={}) click to toggle source

Connect to the server and validate the license. If successful, connected? returns true when finished. If not successful, you can keep calling this. Return false if we could not establish a connection with the server and we should not retry, such as if there's a bad license key.

# File lib/ting_yun/agent/agent.rb, line 79
def connect!(option={})
  defaults = {
      :force_reconnect => ::TingYun::Agent.config[:force_reconnect],
      :keep_retrying => ::TingYun::Agent.config[:keep_retrying]
  }
  opts = defaults.merge(option)
  return unless should_connect?(opts[:force_reconnect])
  TingYun::Agent.logger.debug "Connecting Process to Ting Yun: #$0"
  query_server_for_configuration
  @connect_state = :connected
rescue Exception => error
  ::TingYun::Agent.logger.error "Exception of unexpected type during Agent#connect! :", error
  log_error(error)
  raise
  if opts[:keep_retrying]
    ::TingYun::Agent.logger.info "Will re-attempt in 30 seconds"
    raise
  end
end
install_exit_handler() click to toggle source
# File lib/ting_yun/agent/agent.rb, line 107
def install_exit_handler
  return unless should_install_exit_handler?
  TingYun::Agent.logger.debug("Installing at_exit handler")
  at_exit do
    if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION.match(/^1\.9/)
      exit_status = $!.status if $!.is_a?(SystemExit)
      shutdown
      exit exit_status if exit_status
    else
      shutdown
    end
  end
end
should_install_exit_handler?() click to toggle source
# File lib/ting_yun/agent/agent.rb, line 103
def should_install_exit_handler?
  !sinatra_classic_app?
end
shutdown() click to toggle source

Attempt a graceful shutdown of the agent, flushing any remaining data.

# File lib/ting_yun/agent/agent.rb, line 60
def shutdown
  return unless started?
  TingYun::Agent.logger.info "Starting Agent shutdown"

  stop_event_loop
  untraced_graceful_disconnect
  reset_to_default_configuration

  @started = nil

  TingYun::Frameworks::Framework.reset
end
sinatra_classic_app?() click to toggle source
# File lib/ting_yun/agent/agent.rb, line 99
def sinatra_classic_app?
  defined?(::Sinatra::Base) && ::Sinatra::Base.respond_to?(:run!)
end
start() click to toggle source
# File lib/ting_yun/agent/agent.rb, line 50
def start
  # should hava the vaild app_name, unstart-state and able to start
  return unless agent_should_start?
  log_startup
  check_config_and_start_agent
  TingYun::Agent.logger.debug "Ting Yun Ruby Agent #{TingYun::VERSION::STRING} Initialized: pid = #{$$}" # log_version_and_pid
end
untraced_graceful_disconnect() click to toggle source
# File lib/ting_yun/agent/agent.rb, line 122
def untraced_graceful_disconnect
  begin
    TingYun::Agent.disable_all_tracing do
      if connected?
        transmit_data
      end
    end
  rescue => error
    ::TingYun::Agent.logger.error error
  end
end