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, #sql_sampler, #stats_engine, #transaction_sampler

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_exceptions, #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_settings, #connect_to_server, #connected?, #disconnect, #disconnected?, #environment_for_connect, #finish_setup, #generate_environment_report, #local_host, #log_collector_messages, #log_connection!, #query_server_for_configuration, #sanitize_environment_report, #should_connect?

Methods included from InstanceMethods::HandleErrors

#handle_delay_restart, #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, #has_correct_license_key?, #is_using_forking_dispatcher?, #log_startup, #setup_and_start_agent, #started?

Constructor Details

#initializeAgent

Returns a new instance of Agent.



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

def initialize
  @started = false
  @environment_report = nil
  @service = TingYunService.new
  @connect_state = :pending #[: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

Instance Attribute Details

#cross_app_monitorObject

service for communicating with collector



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

def cross_app_monitor
  @cross_app_monitor
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#middlewareObject

service for communicating with collector



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

def middleware
  @middleware
end

#serviceObject

service for communicating with collector



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

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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)
  if opts[:keep_retrying]
    ::TingYun::Agent.logger.info "Will re-attempt in 60 seconds"
    raise
  end
end

#install_exit_handlerObject



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

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?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/ting_yun/agent/agent.rb', line 102

def should_install_exit_handler?
  !sinatra_classic_app?
end

#shutdownObject

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



60
61
62
63
64
65
66
67
68
69
70
71
# 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?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ting_yun/agent/agent.rb', line 98

def sinatra_classic_app?
  defined?(::Sinatra::Base) && ::Sinatra::Base.respond_to?(:run!)
end

#startObject



50
51
52
53
54
55
56
# 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_disconnectObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ting_yun/agent/agent.rb', line 121

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