Module: NewRelic::Agent::Agent::InstanceMethods::Start
- Included in:
- NewRelic::Agent::Agent::InstanceMethods
- Defined in:
- lib/new_relic/agent/agent.rb
Overview
Herein lies the corpse of the former ‘start’ method. May it’s unmatched flog score rest in pieces.
Instance Method Summary collapse
-
#already_started? ⇒ Boolean
Check whether we have already started, which is an error condition.
-
#check_config_and_start_agent ⇒ Object
Sanity-check the agent configuration and start the agent, setting up the worker thread and the exit handler to shut down the agent.
-
#connect_in_foreground ⇒ Object
Connecting in the foreground blocks further startup of the agent until we have a connection - useful in cases where you’re trying to log a very-short-running process and want to get statistics from before a server connection (typically 20 seconds) exists.
-
#correct_license_length ⇒ Object
A license key is an arbitrary 40 character string, usually looks something like a SHA1 hash.
-
#disabled? ⇒ Boolean
The agent is disabled when it is not force enabled by the ‘agent_enabled’ option (e.g. in a manual start), or enabled normally through the configuration file.
-
#has_correct_license_key? ⇒ Boolean
A correct license key exists and is of the proper length.
-
#has_license_key? ⇒ Boolean
Tell the user when the license key is missing so they can fix it by adding it to the file.
-
#install_exit_handler ⇒ Object
Installs our exit handler, which exploits the weird behavior of at_exit blocks to make sure it runs last, by doing an at_exit within an at_exit block.
-
#log_app_names ⇒ Object
Logs the configured application names.
-
#log_dispatcher ⇒ Object
Logs the dispatcher to the log file to assist with debugging.
-
#log_if(boolean, level, message) ⇒ Object
A helper method that logs a condition if that condition is true.
-
#log_unless(boolean, level, message) ⇒ Object
A helper method that logs a condition unless that condition is true.
-
#log_version_and_pid ⇒ Object
Classy logging of the agent version and the current pid, so we can disambiguate processes in the log file and make sure they’re running a reasonable version.
-
#monitoring? ⇒ Boolean
Warn the user if they have configured their agent not to send data, that way we can see this clearly in the log file.
-
#notify_log_file_location ⇒ Object
Tells us in the log file where the log file is located.
-
#using_forking_dispatcher? ⇒ Boolean
If we’re using a dispatcher that forks before serving requests, we need to wait until the children are forked before connecting, otherwise the parent process sends odd data.
-
#using_sinatra? ⇒ Boolean
If we’re using sinatra, old versions run in an at_exit block so we should probably know that.
-
#weird_ruby? ⇒ Boolean
we should not set an at_exit block if people are using these as they don’t do standard at_exit behavior per MRI/YARV.
Instance Method Details
#already_started? ⇒ Boolean
Check whether we have already started, which is an error condition
286 287 288 289 290 291 |
# File 'lib/new_relic/agent/agent.rb', line 286 def already_started? if started? control.log!("Agent Started Already!", :error) true end end |
#check_config_and_start_agent ⇒ Object
Sanity-check the agent configuration and start the agent, setting up the worker thread and the exit handler to shut down the agent
414 415 416 417 418 419 420 |
# File 'lib/new_relic/agent/agent.rb', line 414 def check_config_and_start_agent return unless monitoring? && has_correct_license_key? return if using_forking_dispatcher? connect_in_foreground if control.sync_startup start_worker_thread install_exit_handler end |
#connect_in_foreground ⇒ Object
Connecting in the foreground blocks further startup of the agent until we have a connection - useful in cases where you’re trying to log a very-short-running process and want to get statistics from before a server connection (typically 20 seconds) exists
319 320 321 |
# File 'lib/new_relic/agent/agent.rb', line 319 def connect_in_foreground NewRelic::Agent.disable_all_tracing { connect(:keep_retrying => false) } end |
#correct_license_length ⇒ Object
A license key is an arbitrary 40 character string, usually looks something like a SHA1 hash
399 400 401 402 |
# File 'lib/new_relic/agent/agent.rb', line 399 def correct_license_length key = control.license_key log_unless((key.length == 40), :error, "Invalid license key: #{key}") end |
#disabled? ⇒ Boolean
The agent is disabled when it is not force enabled by the ‘agent_enabled’ option (e.g. in a manual start), or enabled normally through the configuration file
296 297 298 |
# File 'lib/new_relic/agent/agent.rb', line 296 def disabled? !control.agent_enabled? end |
#has_correct_license_key? ⇒ Boolean
A correct license key exists and is of the proper length
393 394 395 |
# File 'lib/new_relic/agent/agent.rb', line 393 def has_correct_license_key? has_license_key? && correct_license_length end |
#has_license_key? ⇒ Boolean
Tell the user when the license key is missing so they can fix it by adding it to the file
388 389 390 |
# File 'lib/new_relic/agent/agent.rb', line 388 def has_license_key? log_unless(control.license_key, :error, "No license key found. Please edit your newrelic.yml file and insert your license key.") end |
#install_exit_handler ⇒ Object
Installs our exit handler, which exploits the weird behavior of at_exit blocks to make sure it runs last, by doing an at_exit within an at_exit block.
340 341 342 343 344 345 |
# File 'lib/new_relic/agent/agent.rb', line 340 def install_exit_handler if control.send_data_on_exit && !weird_ruby? # Our shutdown handler needs to run after other shutdown handlers at_exit { at_exit { shutdown } } end end |
#log_app_names ⇒ Object
Logs the configured application names
310 311 312 |
# File 'lib/new_relic/agent/agent.rb', line 310 def log_app_names log.info "Application: #{control.app_names.join(", ")}" end |
#log_dispatcher ⇒ Object
Logs the dispatcher to the log file to assist with debugging. When no debugger is present, logs this fact to assist with proper dispatcher detection
303 304 305 306 307 |
# File 'lib/new_relic/agent/agent.rb', line 303 def log_dispatcher dispatcher_name = control.dispatcher.to_s return if log_if(dispatcher_name.empty?, :info, "No dispatcher detected.") log.info "Dispatcher: #{dispatcher_name}" end |
#log_if(boolean, level, message) ⇒ Object
A helper method that logs a condition if that condition is true. Mentally cleaner than having every method set a local and log if it is true
367 368 369 370 |
# File 'lib/new_relic/agent/agent.rb', line 367 def log_if(boolean, level, ) self.log.send(level, ) if boolean boolean end |
#log_unless(boolean, level, message) ⇒ Object
A helper method that logs a condition unless that condition is true. Mentally cleaner than having every method set a local and log unless it is true
375 376 377 378 |
# File 'lib/new_relic/agent/agent.rb', line 375 def log_unless(boolean, level, ) self.log.send(level, ) unless boolean boolean end |
#log_version_and_pid ⇒ Object
Classy logging of the agent version and the current pid, so we can disambiguate processes in the log file and make sure they’re running a reasonable version
360 361 362 |
# File 'lib/new_relic/agent/agent.rb', line 360 def log_version_and_pid log.info "New Relic Ruby Agent #{NewRelic::VERSION::STRING} Initialized: pid = #{$$}" end |
#monitoring? ⇒ Boolean
Warn the user if they have configured their agent not to send data, that way we can see this clearly in the log file
382 383 384 |
# File 'lib/new_relic/agent/agent.rb', line 382 def monitoring? log_unless(control.monitor_mode?, :warn, "Agent configured not to send data in this environment - edit newrelic.yml to change this") end |
#notify_log_file_location ⇒ Object
Tells us in the log file where the log file is located. This seems redundant, but can come in handy when we have some log file path set by the user which parses incorrectly, sending the log file to who-knows-where
351 352 353 354 355 |
# File 'lib/new_relic/agent/agent.rb', line 351 def notify_log_file_location log_file = NewRelic::Control.instance.log_file log_if(File.exists?(log_file.to_s), :info, "Agent Log at #{log_file}") end |
#using_forking_dispatcher? ⇒ Boolean
If we’re using a dispatcher that forks before serving requests, we need to wait until the children are forked before connecting, otherwise the parent process sends odd data
407 408 409 |
# File 'lib/new_relic/agent/agent.rb', line 407 def using_forking_dispatcher? log_if([:passenger, :unicorn].include?(control.dispatcher), :info, "Connecting workers after forking.") end |
#using_sinatra? ⇒ Boolean
If we’re using sinatra, old versions run in an at_exit block so we should probably know that
325 326 327 |
# File 'lib/new_relic/agent/agent.rb', line 325 def using_sinatra? defined?(Sinatra::Application) end |
#weird_ruby? ⇒ Boolean
we should not set an at_exit block if people are using these as they don’t do standard at_exit behavior per MRI/YARV
331 332 333 334 335 |
# File 'lib/new_relic/agent/agent.rb', line 331 def weird_ruby? NewRelic::LanguageSupport.using_engine?('rbx') || NewRelic::LanguageSupport.using_engine?('jruby') || using_sinatra? end |