lib/new_relic/agent/agent.rb in newrelic_rpm-2.9.2 vs lib/new_relic/agent/agent.rb in newrelic_rpm-2.9.3

- old
+ new

@@ -3,19 +3,21 @@ require 'net/http' require 'logger' require 'zlib' require 'stringio' -# The NewRelic Agent collects performance data from ruby applications in realtime as the -# application runs, and periodically sends that data to the NewRelic server. +# The NewRelic Agent collects performance data from ruby applications +# in realtime as the application runs, and periodically sends that +# data to the NewRelic server. module NewRelic::Agent - - # The Agent is a singleton that is instantiated when the plugin is activated. + + # The Agent is a singleton that is instantiated when the plugin is + # activated. class Agent - # Specifies the version of the agent's communication protocol - # with the NewRelic hosted site. + # Specifies the version of the agent's communication protocol with + # the NewRelic hosted site. PROTOCOL_VERSION = 5 attr_reader :obfuscator attr_reader :stats_engine @@ -32,22 +34,23 @@ def manual_start(ignored=nil, also_ignored=nil) raise "This method no longer supported. Instead use the class method NewRelic::Agent.manual_start" end # this method makes sure that the agent is running. it's important - # for passenger where processes are forked and the agent is dormant + # for passenger where processes are forked and the agent is + # dormant # def ensure_worker_thread_started return unless control.agent_enabled? && control.monitor_mode? && !@invalid_license if !running? launch_worker_thread @stats_engine.spawn_sampler_thread end end # True if the worker thread has been started. Doesn't necessarily - # mean we are connected + # mean we are connected def running? control.agent_enabled? && control.monitor_mode? && @worker_loop && @worker_loop.pid == $$ end # True if we have initialized and completed 'start' @@ -61,11 +64,12 @@ if @worker_loop @worker_loop.stop log.debug "Starting Agent shutdown" - # if litespeed, then ignore all future SIGUSR1 - it's litespeed trying to shut us down + # if litespeed, then ignore all future SIGUSR1 - it's + # litespeed trying to shut us down if control.dispatcher == :litespeed Signal.trap("SIGUSR1", "IGNORE") Signal.trap("SIGTERM", "IGNORE") end @@ -127,14 +131,15 @@ end def log NewRelic::Control.instance.log end - - # Start up the agent. This verifies that the agent_enabled? is true - # and initializes the sampler based on the current controluration settings. - # Then it will fire up the background thread for sending data to the server if applicable. + + # Start up the agent. This verifies that the agent_enabled? is + # true and initializes the sampler based on the current + # controluration settings. Then it will fire up the background + # thread for sending data to the server if applicable. def start if started? control.log! "Agent Started Already!", :error return end @@ -153,12 +158,14 @@ sampler_config = control.fetch('transaction_tracer', {}) @use_transaction_sampler = sampler_config.fetch('enabled', true) @record_sql = sampler_config.fetch('record_sql', :obfuscated).to_sym - # use transaction_threshold: 4.0 to force the TT collection threshold to 4 seconds - # use transaction_threshold: apdex_f to use your apdex t value multiplied by 4 + # use transaction_threshold: 4.0 to force the TT collection + # threshold to 4 seconds + # use transaction_threshold: apdex_f to use your apdex t value + # multiplied by 4 # undefined transaction_threshold defaults to 2.0 apdex_f = 4 * NewRelic::Control.instance['apdex_t'].to_f @slowest_transaction_threshold = sampler_config.fetch('transaction_threshold', 2.0) if @slowest_transaction_threshold =~ /apdex_f/i @slowest_transaction_threshold = apdex_f @@ -186,12 +193,13 @@ elsif control.license_key.length != 40 @invalid_license = true control.log! "Invalid license key: #{control.license_key}", :error else launch_worker_thread - # When the VM shuts down, attempt to send a message to the server that - # this agent run is stopping, assuming it has successfully connected + # When the VM shuts down, attempt to send a message to the + # server that this agent run is stopping, assuming it has + # successfully connected at_exit { shutdown } end end control.log! "New Relic RPM Agent #{NewRelic::VERSION::STRING} Initialized: pid = #{$$}" control.log! "Agent Log found in #{NewRelic::Control.instance.log_file}" @@ -200,23 +208,24 @@ private def collector @collector ||= control.server end - # Connect to the server, and run the worker loop forever. Will not return. + # Connect to the server, and run the worker loop forever. + # Will not return. def run_worker_loop - # connect to the server. this will keep retrying until successful or - # it determines the license is bad. + # connect to the server. this will keep retrying until + # successful or it determines the license is bad. connect # We may not be connected now but keep going for dev mode if @connected begin - # determine the reporting period (server based) - # note if the agent attempts to report more frequently than the specified - # report data, then it will be ignored. + # determine the reporting period (server based) + # note if the agent attempts to report more frequently than + # the specified report data, then it will be ignored. control.log! "Reporting performance data every #{@report_period} seconds." @worker_loop.add_task(@report_period) do harvest_and_send_timeslice_data end @@ -269,13 +278,15 @@ control.log! e.backtrace.join("\n "), :error end end @worker_thread['newrelic_label'] = 'Worker Loop' - # This code should be activated to check that no dependency loading is occuring in the background thread - # by stopping the foreground thread after the background thread is created. Turn on dependency loading logging - # and make sure that no loading occurs. + # This code should be activated to check that no dependency + # loading is occuring in the background thread by stopping the + # foreground thread after the background thread is created. Turn + # on dependency loading logging and make sure that no loading + # occurs. # # control.log! "FINISHED AGENT INIT" # while true # sleep 1 # end @@ -300,16 +311,15 @@ @invalid_license = false @last_harvest_time = Time.now end - # Connect to the server and validate the license. - # If successful, @connected has 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. + # Connect to the server and validate the license. If successful, + # @connected has 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. def connect # wait a few seconds for the web server to boot, necessary in development connect_retry_period = 5 connect_attempts = 0 @@ -329,11 +339,12 @@ @report_period = invoke_remote :get_data_report_period, @agent_id control.log! "Connected to NewRelic Service at #{@collector}" log.debug "Agent ID = #{@agent_id}." - # Ask the server for permission to send transaction samples. determined by subscription license. + # Ask the server for permission to send transaction samples. + # determined by subscription license. @should_send_samples = invoke_remote :should_collect_samples, @agent_id if @should_send_samples sampling_rate = invoke_remote :sampling_rate, @agent_id if @random_sample @transaction_sampler.sampling_rate = sampling_rate @@ -444,17 +455,19 @@ invoke_remote :transaction_sample_data, @agent_id, traces log.debug "#{now}: sent slowest sample (#{@agent_id}) in #{Time.now - now} seconds" end - # if we succeed sending this sample, then we don't need to keep the slowest sample - # around - it has been sent already and we can collect the next one + # if we succeed sending this sample, then we don't need to keep + # the slowest sample around - it has been sent already and we + # can collect the next one @traces = nil - # note - exceptions are logged in invoke_remote. If an exception is encountered here, - # then the slowest sample of is determined of the entire period since the last - # reported sample. + # note - exceptions are logged in invoke_remote. If an + # exception is encountered here, then the slowest sample of is + # determined of the entire period since the last reported + # sample. end def harvest_and_send_errors @unsent_errors = @error_collector.harvest_errors(@unsent_errors) if @unsent_errors && @unsent_errors.length > 0 @@ -520,12 +533,13 @@ log.debug "Unexpected response from server: #{response.code}: #{response.message}" raise IgnoreSilentlyException end rescue ForceDisconnectException => e log.error "RPM forced this agent to disconnect (#{e.message})\n" \ - "Restart this process to resume monitoring via rpm.newrelic.com." - # when a disconnect is requested, stop the current thread, which is the worker thread that - # gathers data and talks to the server. + "Restart this process to resume monitoring via rpm.newrelic.com." + # when a disconnect is requested, stop the current thread, which + # is the worker thread that gathers data and talks to the + # server. @connected = false Thread.exit rescue SystemCallError, SocketError => e # These include Errno connection errors log.debug "Recoverable error connecting to the server: #{e}"