Class: TingYun::Agent::Agent

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
InstanceMethods
Defined in:
lib/ting_yun/agent/agent.rb

Instance Attribute Summary collapse

Attributes included from InstanceMethods::ContainerDataManager

#error_collector, #middleware, #sql_sampler, #stats_engine, #transaction_sampler

Attributes included from InstanceMethods::Connect

#connect_attempts

Instance Method Summary collapse

Methods included from ClassMethods

config, instance, logger

Methods included from InstanceMethods

#pop_trace_execution_flag, #push_trace_execution_flag, #reset_to_default_configuration, #stop_event_loop

Methods included from InstanceMethods::StartWorkerThread

#create_and_run_event_loop, #deferred_work!, #start_worker_thread

Methods included from InstanceMethods::ContainerDataManager

#container_for_endpoint, #drop_buffered_data, #harvest_and_send_errors, #harvest_and_send_external_errors, #harvest_and_send_from_container, #harvest_and_send_slowest_sql, #harvest_and_send_timeslice_data, #harvest_and_send_transaction_traces, #harvest_from_container, #init_containers, #reset_objects_with_locks, #send_data_to_endpoint, #transmit_data

Methods included from InstanceMethods::Connect

#catch_errors, #connect_in_sync, #connect_retry_period, #connect_settings, #connect_to_server, #connected?, #disconnect, #disconnected?, #environment_for_connect, #finish_setup, #generate_environment_report, #local_host, #log_collector_messages, #log_connection!, #note_connect_failure, #query_server_for_configuration, #sanitize_environment_report, #should_connect?

Methods included from InstanceMethods::HandleErrors

#handle_force_disconnect, #handle_force_restart, #handle_license_error, #handle_other_error, #handle_server_error, #handle_unrecoverable_agent_error, #log_error

Methods included from InstanceMethods::Start

#after_fork, #agent_should_start?, #already_started?, #app_name_configured?, #check_config_and_start_agent, #correct_license_length, #cpu_and_memory, #disabled?, #has_correct_license_key?, #has_license_key?, #is_using_forking_dispatcher?, #log_app_name, #log_dispatcher, #log_environment, #log_startup, #log_version_and_pid, #monitoring?, #setup_and_start_agent, #sinatra_app?, #started?

Constructor Details

#initializeAgent



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ting_yun/agent/agent.rb', line 35

def initialize
  @started = false
  @environment_report = nil
  @service = TingYunService.new
  @connect_state = :pending #[:pending, :connected, :disconnected]
  @connect_attempts = 0
  @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)

  init_containers
end

Instance Attribute Details

#cross_app_monitorObject

service for communicating with collector



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

def cross_app_monitor
  @cross_app_monitor
end

#eventsObject (readonly)

Returns the value of attribute events.



29
30
31
# File 'lib/ting_yun/agent/agent.rb', line 29

def events
  @events
end

#serviceObject

service for communicating with collector



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

def service
  @service
end

Instance Method Details

#connect!(option = {}) ⇒ Object

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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ting_yun/agent/agent.rb', line 78

def connect!(option={})
  defaults = {
      :keep_retrying => ::TingYun::Agent.config[:keep_retrying],
      :force_reconnect => ::TingYun::Agent.config[:force_reconnect]
  }
  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
  @connected_pid = $$
  @connect_state = :connected
rescue TingYun::Support::Exception::LicenseException => e
  handle_license_error(e)
rescue TingYun::Support::Exception::UnrecoverableAgentException => e
  handle_unrecoverable_agent_error(e)
rescue StandardError, Timeout::Error, TingYun::Support::Exception::ServerConnectionException, TingYun::Support::Exception::AgentEnableException => e
  log_error(e)
  if TingYun::Agent.config[:keep_retrying]
    note_connect_failure
    ::TingYun::Agent.logger.info "Will re-attempt in 60 seconds"
    sleep 60
    retry
  else
    disconnect
  end
rescue Exception => e
  ::TingYun::Agent.logger.error "Exception of unexpected type during Agent#connect():", e
  raise
end

#install_exit_handlerObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ting_yun/agent/agent.rb', line 109

def install_exit_handler
  TingYun::Agent.logger.debug("Installing at_exit handler")
  at_exit do
    if need_exit_code_workaround?
      exit_status = $!.status if $!.is_a?(SystemExit)
      shutdown
      exit exit_status if exit_status
    else
      shutdown
    end
  end
end

#need_exit_code_workaround?Boolean



122
123
124
# File 'lib/ting_yun/agent/agent.rb', line 122

def need_exit_code_workaround?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION.match(/^1\.9/)
end

#shutdownObject

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



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ting_yun/agent/agent.rb', line 59

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

#startObject



49
50
51
52
53
54
55
# File 'lib/ting_yun/agent/agent.rb', line 49

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
  log_version_and_pid
end

#untraced_graceful_disconnectObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ting_yun/agent/agent.rb', line 126

def untraced_graceful_disconnect
  begin
    TingYun::Agent.disable_all_tracing do
      if connected?
        transmit_data
      end
    end
  rescue => e
    ::TingYun::Agent.logger.error e
  end
end