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 its 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.
-
#defer_for_resque? ⇒ Boolean
Return true if we’re using resque and it hasn’t had a chance to (potentially) daemonize itself.
-
#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_environment ⇒ Object
Log the environment the app thinks it’s running in.
-
#log_if(boolean, level, message) ⇒ Object
A helper method that logs a condition if that condition is true.
-
#log_startup ⇒ Object
Log startup information that we almost always want to know.
-
#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.
-
#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
308 309 310 311 312 313 |
# File 'lib/new_relic/agent/agent.rb', line 308 def already_started? if started? ::NewRelic::Agent.logger.error("Agent Started Already!") 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
467 468 469 470 471 472 473 |
# File 'lib/new_relic/agent/agent.rb', line 467 def check_config_and_start_agent return unless monitoring? && has_correct_license_key? return if using_forking_dispatcher? connect_in_foreground if Agent.config[: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
359 360 361 |
# File 'lib/new_relic/agent/agent.rb', line 359 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
443 444 445 446 |
# File 'lib/new_relic/agent/agent.rb', line 443 def correct_license_length key = Agent.config[:license_key] log_unless((key.length == 40), :error, "Invalid license key: #{key}") end |
#defer_for_resque? ⇒ Boolean
Return true if we’re using resque and it hasn’t had a chance to (potentially) daemonize itself. This avoids hanging when there’s a Thread started before Resque calls Process.daemon (Jira RUBY-857)
459 460 461 462 |
# File 'lib/new_relic/agent/agent.rb', line 459 def defer_for_resque? NewRelic::Agent.config[:dispatcher] == :resque && !NewRelic::Agent::PipeChannelManager.listener.started? 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
318 319 320 |
# File 'lib/new_relic/agent/agent.rb', line 318 def disabled? !Agent.config[:agent_enabled] end |
#has_correct_license_key? ⇒ Boolean
A correct license key exists and is of the proper length
437 438 439 |
# File 'lib/new_relic/agent/agent.rb', line 437 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
430 431 432 433 434 |
# File 'lib/new_relic/agent/agent.rb', line 430 def has_license_key? log_unless(Agent.config[:license_key], :warn, "No license key found in newrelic.yml config. " + "This often means your newrelic.yml is missing a section for the running environment '#{NewRelic::Control.instance.env}'") 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.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/new_relic/agent/agent.rb', line 380 def install_exit_handler if Agent.config[:send_data_on_exit] && !weird_ruby? at_exit do # Workaround for MRI 1.9 bug that loses exit codes in at_exit blocks. # This is necessary to get correct exit codes for the agent's # test suites. # http://bugs.ruby-lang.org/issues/5218 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 end |
#log_app_names ⇒ Object
Logs the configured application names
345 346 347 348 349 350 351 352 |
# File 'lib/new_relic/agent/agent.rb', line 345 def log_app_names names = Agent.config.app_names if names.respond_to?(:any?) && names.any? ::NewRelic::Agent.logger.info "Application: #{names.join(", ")}" else ::NewRelic::Agent.logger.error 'Unable to determine application name. Please set the application name in your newrelic.yml or in a NEW_RELIC_APP_NAME environment variable.' end 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
338 339 340 341 342 |
# File 'lib/new_relic/agent/agent.rb', line 338 def log_dispatcher dispatcher_name = Agent.config[:dispatcher].to_s return if log_if(dispatcher_name.empty?, :warn, "No dispatcher detected.") ::NewRelic::Agent.logger.info "Dispatcher: #{dispatcher_name}" end |
#log_environment ⇒ Object
Log the environment the app thinks it’s running in. Useful in debugging, as this is the key for config YAML lookups.
331 332 333 |
# File 'lib/new_relic/agent/agent.rb', line 331 def log_environment ::NewRelic::Agent.logger.info "Environment: #{NewRelic::Control.instance.env}" 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
408 409 410 411 |
# File 'lib/new_relic/agent/agent.rb', line 408 def log_if(boolean, level, ) ::NewRelic::Agent.logger.send(level, ) if boolean boolean end |
#log_startup ⇒ Object
Log startup information that we almost always want to know
323 324 325 326 327 |
# File 'lib/new_relic/agent/agent.rb', line 323 def log_startup log_environment log_dispatcher log_app_names 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
416 417 418 419 |
# File 'lib/new_relic/agent/agent.rb', line 416 def log_unless(boolean, level, ) ::NewRelic::Agent.logger.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
401 402 403 |
# File 'lib/new_relic/agent/agent.rb', line 401 def log_version_and_pid ::NewRelic::Agent.logger.debug "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
423 424 425 426 |
# File 'lib/new_relic/agent/agent.rb', line 423 def monitoring? log_unless(Agent.config[:monitor_mode], :warn, "Agent configured not to send data in this environment.") 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
451 452 453 454 |
# File 'lib/new_relic/agent/agent.rb', line 451 def using_forking_dispatcher? log_if([:passenger, :rainbows, :unicorn].include?(Agent.config[: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
365 366 367 |
# File 'lib/new_relic/agent/agent.rb', line 365 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
371 372 373 374 375 |
# File 'lib/new_relic/agent/agent.rb', line 371 def weird_ruby? NewRelic::LanguageSupport.using_engine?('rbx') || NewRelic::LanguageSupport.using_engine?('jruby') || using_sinatra? end |