Module: NewRelic::Agent::Agent::InstanceMethods::StartWorkerThread

Included in:
NewRelic::Agent::Agent::InstanceMethods
Defined in:
lib/new_relic/agent/agent.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#catch_errorsObject

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.



532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/new_relic/agent/agent.rb', line 532

def catch_errors
  yield
rescue NewRelic::Agent::ForceRestartException => e
  handle_force_restart(e)
  retry
rescue NewRelic::Agent::ForceDisconnectException => e
  handle_force_disconnect(e)
rescue NewRelic::Agent::ServerConnectionException => e
  handle_server_connection_problem(e)
rescue Exception => e
  handle_other_error(e)
end

#check_sql_sampler_statusObject



468
469
470
471
472
473
474
475
476
# File 'lib/new_relic/agent/agent.rb', line 468

def check_sql_sampler_status
  # disable sql sampling if disabled by the server
  # and we're not in dev mode
  if @sql_sampler.config.fetch('enabled', true) && ['raw', 'obfuscated'].include?(@sql_sampler.config.fetch('record_sql', 'obfuscated').to_s) && @transaction_sampler.config.fetch('enabled', true)
    @sql_sampler.enable
  else
    @sql_sampler.disable
  end
end

#check_transaction_sampler_statusObject

disable transaction sampling if disabled by the server and we’re not in dev mode



460
461
462
463
464
465
466
# File 'lib/new_relic/agent/agent.rb', line 460

def check_transaction_sampler_status
  if control.developer_mode? || @should_send_samples
    @transaction_sampler.enable
  else
    @transaction_sampler.disable
  end
end

#create_and_run_worker_loopObject

Creates the worker loop and loads it with the instructions it should run every @report_period seconds



487
488
489
490
491
492
# File 'lib/new_relic/agent/agent.rb', line 487

def create_and_run_worker_loop
  @worker_loop = WorkerLoop.new
  @worker_loop.run(@report_period) do
    save_or_transmit_data
  end
end

#deferred_work!(connection_options) ⇒ Object

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



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/new_relic/agent/agent.rb', line 553

def deferred_work!(connection_options)
  catch_errors do
    NewRelic::Agent.disable_all_tracing do
      # We try to connect.  If this returns false that means
      # the server rejected us for a licensing reason and we should
      # just exit the thread.  If it returns nil
      # that means it didn't try to connect because we're in the master.
      connect(connection_options)
      if @connected
        check_transaction_sampler_status
        check_sql_sampler_status
        log_worker_loop_start
        create_and_run_worker_loop
        # never reaches here unless there is a problem or
        # the agent is exiting
      else
        log.debug "No connection.  Worker thread ending."
      end
    end
  end
end

#handle_force_disconnect(error) ⇒ Object

when a disconnect is requested, stop the current thread, which is the worker thread that gathers data and talks to the server.



508
509
510
511
# File 'lib/new_relic/agent/agent.rb', line 508

def handle_force_disconnect(error)
  log.error "New Relic forced this agent to disconnect (#{error.message})"
  disconnect
end

#handle_force_restart(error) ⇒ Object

Handles the case where the server tells us to restart - this clears the data, clears connection attempts, and waits a while to reconnect.



497
498
499
500
501
502
503
# File 'lib/new_relic/agent/agent.rb', line 497

def handle_force_restart(error)
  log.info error.message
  reset_stats
  @metric_ids = {}
  @connected = nil
  sleep 30
end

#handle_other_error(error) ⇒ Object

Handles an unknown error in the worker thread by logging it and disconnecting the agent, since we are now in an unknown state



524
525
526
527
# File 'lib/new_relic/agent/agent.rb', line 524

def handle_other_error(error)
  log.error "Terminating worker loop: #{error.class.name}: #{error.message}\n  #{error.backtrace.join("\n  ")}"
  disconnect
end

#handle_server_connection_problem(error) ⇒ Object

there is a problem with connecting to the server, so we stop trying to connect and shut down the agent



515
516
517
518
519
# File 'lib/new_relic/agent/agent.rb', line 515

def handle_server_connection_problem(error)
  log.error "Unable to establish connection with the server.  Run with log level set to debug for more information."
  log.debug("#{error.class.name}: #{error.message}\n#{error.backtrace.first}")
  disconnect
end

#log_worker_loop_startObject

logs info about the worker loop so users can see when the agent actually begins running in the background



480
481
482
483
# File 'lib/new_relic/agent/agent.rb', line 480

def log_worker_loop_start
  log.info "Reporting performance data every #{@report_period} seconds."
  log.debug "Running worker loop"
end