module NewRelic::Agent::Agent::InstanceMethods::StartWorkerThread
All of this module used to be contained in the start_worker_thread method - this is an artifact of refactoring and can be moved, renamed, etc at will
Constants
- LOG_ONCE_KEYS_RESET_PERIOD
- MIN_ALLOWED_REPORT_PERIOD
Never allow any data type to be reported more frequently than once per second.
Public Instance Methods
a wrapper method to handle all the errors that can happen in the connection and worker thread system. This guarantees a no-throw from the background thread.
# File lib/new_relic/agent/agent.rb, line 669 def catch_errors yield rescue NewRelic::Agent::ForceRestartException => e handle_force_restart(e) retry rescue NewRelic::Agent::ForceDisconnectException => e handle_force_disconnect(e) rescue => e handle_other_error(e) end
# File lib/new_relic/agent/agent.rb, line 618 def create_and_run_event_loop @event_loop = create_event_loop @event_loop.on(:report_data) do transmit_data end @event_loop.on(:report_event_data) do transmit_event_data end @event_loop.on(:reset_log_once_keys) do ::NewRelic::Agent.logger.clear_already_logged end @event_loop.fire_every(Agent.config[:data_report_period], :report_data) @event_loop.fire_every(report_period_for(:analytic_event_data), :report_event_data) @event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD, :reset_log_once_keys) @event_loop.run end
# File lib/new_relic/agent/agent.rb, line 594 def create_event_loop EventLoop.new end
This is the method that is run in a new thread in order to background the harvesting and sending of data during the normal operation of the agent.
Takes connection options that determine how we should connect to the server, and loops endlessly - typically we never return from this method unless we're shutting down the agent
# File lib/new_relic/agent/agent.rb, line 688 def deferred_work!(connection_options) catch_errors do NewRelic::Agent.disable_all_tracing do connect(connection_options) if connected? create_and_run_event_loop # never reaches here unless there is a problem or # the agent is exiting else ::NewRelic::Agent.logger.debug "No connection. Worker thread ending." end end end end
when a disconnect is requested, stop the current thread, which is the worker thread that gathers data and talks to the server.
# File lib/new_relic/agent/agent.rb, line 650 def handle_force_disconnect(error) ::NewRelic::Agent.logger.warn "New Relic forced this agent to disconnect (#{error.message})" disconnect end
Handles the case where the server tells us to restart - this clears the data, clears connection attempts, and waits a while to reconnect.
# File lib/new_relic/agent/agent.rb, line 639 def handle_force_restart(error) ::NewRelic::Agent.logger.debug error.message drop_buffered_data @service.force_restart if @service @connect_state = :pending sleep 30 end
Handles an unknown error in the worker thread by logging it and disconnecting the agent, since we are now in an unknown state.
# File lib/new_relic/agent/agent.rb, line 658 def handle_other_error(error) ::NewRelic::Agent.logger.error "Unhandled error in worker thread, disconnecting this agent process:" # These errors are fatal (that is, they will prevent the agent from # reporting entirely), so we really want backtraces when they happen ::NewRelic::Agent.logger.log_exception(:error, error) disconnect end
# File lib/new_relic/agent/agent.rb, line 602 def report_period_for(method) config_key = "data_report_periods.#{method}".to_sym period = Agent.config[config_key] if !period period = Agent.config[:data_report_period] ::NewRelic::Agent.logger.warn("Could not find configured period for #{method}, falling back to data_report_period (#{period} s)") end if period < MIN_ALLOWED_REPORT_PERIOD ::NewRelic::Agent.logger.warn("Configured #{config_key} was #{period}, but minimum allowed is #{MIN_ALLOWED_REPORT_PERIOD}, using #{MIN_ALLOWED_REPORT_PERIOD}.") period = MIN_ALLOWED_REPORT_PERIOD end period end