module NewRelic::Agent::Autostart

A singleton responsible for determining if the agent should start monitoring.

If the agent is in a monitored environment (e.g. production) it will attempt to avoid starting at “inapproriate” times, for example in an IRB session. On Heroku, logs typically go to STDOUT so agent logs can spam the console during interactive sessions.

It should be possible to override Autostart logic with an explicit configuration, for example the NEWRELIC_AGENT_ENABLED environment variable or agent_enabled key in newrelic.yml

Public Instance Methods

agent_should_start?() click to toggle source

The constants, executables (i.e. $0) and rake tasks used can be configured with the config keys 'autostart.blacklisted_constants', 'autostart.blacklisted_executables' and 'autostart.blacklisted_rake_tasks'

# File lib/new_relic/agent/autostart.rb, line 25
def agent_should_start?
    !blacklisted_constants? &&
    !blacklisted_executables? &&
    !in_blacklisted_rake_task?
end
blacklisted?(value, &block) click to toggle source
# File lib/new_relic/agent/autostart.rb, line 49
def blacklisted?(value, &block)
  value.split(/\s*,\s*/).any?(&block)
end
blacklisted_constants?() click to toggle source
# File lib/new_relic/agent/autostart.rb, line 31
def blacklisted_constants?
  blacklisted?(NewRelic::Agent.config[:'autostart.blacklisted_constants']) do |name|
    constant_is_defined?(name)
  end
end
blacklisted_executables?() click to toggle source
# File lib/new_relic/agent/autostart.rb, line 37
def blacklisted_executables?
  blacklisted?(NewRelic::Agent.config[:'autostart.blacklisted_executables']) do |bin|
    File.basename($0) == bin
  end
end
constant_is_defined?(const_name) click to toggle source

Lookup whether namespaced constants (e.g. ::Foo::Bar::Baz) are in the environment.

# File lib/new_relic/agent/autostart.rb, line 45
def constant_is_defined?(const_name)
  !!::NewRelic::LanguageSupport.constantize(const_name)
end
in_blacklisted_rake_task?() click to toggle source
# File lib/new_relic/agent/autostart.rb, line 53
def in_blacklisted_rake_task?
  tasks = begin
            ::Rake.application.top_level_tasks
          rescue => e
      ::NewRelic::Agent.logger.debug("Not in Rake environment so skipping blacklisted_rake_tasks check: #{e}")
      []
    end
  !(tasks & ::NewRelic::Agent.config[:'autostart.blacklisted_rake_tasks'].split(/\s*,\s*/)).empty?
end