Module: NewRelic::Agent::Autostart

Extended by:
Autostart
Included in:
Autostart
Defined in:
lib/new_relic/agent/autostart.rb

Overview

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_ENABLE environment variable or agent_enabled key in newrelic.yml

Instance Method Summary collapse

Instance Method Details

#agent_should_start?Boolean

The constants, execuatables (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’

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/new_relic/agent/autostart.rb', line 26

def agent_should_start?
    !blacklisted?('autostart.blacklisted_constants') { |name| constant_is_defined?(name) } &&
    !blacklisted?('autostart.blacklisted_executables') { |bin| File.basename($0) == bin } &&
    !in_blacklisted_rake_task?
end

#blacklisted?(key, &block) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/new_relic/agent/autostart.rb', line 55

def blacklisted?(key, &block)
  ::NewRelic::Agent.config[key].split(/\s*,\s*/).any?(&block)
end

#constant_is_defined?(const_name) ⇒ Boolean

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

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/new_relic/agent/autostart.rb', line 34

def constant_is_defined?(const_name)
  const_name.to_s.sub(/\A::/,'').split('::').inject(Object) do |namespace, name|
    begin
      result = namespace.const_get(name)

      # const_get looks up the inheritence chain, so if it's a class
      # in the constant make sure we found the one in our namespace.
      #
      # Can't help if the constant isn't a class...
      if result.is_a?(Module)
        expected_name = "#{namespace}::#{name}".gsub(/^Object::/, "")
        return false unless expected_name == result.to_s
      end

      result
    rescue NameError
      false
    end
  end
end

#in_blacklisted_rake_task?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
# File 'lib/new_relic/agent/autostart.rb', line 59

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