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 “inappropriate” 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 NEW_RELIC_AGENT_ENABLED environment variable or agent_enabled key in newrelic.yml

Constant Summary collapse

COMMA =
','.freeze

Instance Method Summary collapse

Instance Method Details

#agent_should_start?Boolean

The constants, executables (i.e. $0) and rake tasks used can be configured with the config keys ‘autostart.denylisted_constants’, ‘autostart.denylisted_executables’ and ‘autostart.denylisted_rake_tasks’

Returns:

  • (Boolean)


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

def agent_should_start?
  !denylisted_constants? &&
    !denylisted_executables? &&
    !in_denylisted_rake_task?
end

#denylisted?(value, &block) ⇒ Boolean

Returns:

  • (Boolean)


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

def denylisted?(value, &block)
  value.split(/\s*,\s*/).any?(&block)
end

#denylisted_constants?Boolean

Returns:

  • (Boolean)


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

def denylisted_constants?
  denylisted?(NewRelic::Agent.config[:'autostart.denylisted_constants']) do |name|
    if RUBY_PLATFORM.eql?('java')
      # As of JRuby 9.3.4.0, Object.const_defined? will still cross
      # namespaces to find constants, which is not what we want. This
      # behavior is similar to CRuby's Object.const_get prior to v2.6.
      #
      # Example:
      #  irb> class MyClass; end; module MyModule; end
      #  irb> Object.const_defined?('MyModule::MyClass') # => true
      !!::NewRelic::LanguageSupport.constantize(name)
    else
      Object.const_defined?(name)
    end
  end
end

#denylisted_executables?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/new_relic/agent/autostart.rb', line 50

def denylisted_executables?
  denylisted?(NewRelic::Agent.config[:'autostart.denylisted_executables']) do |bin|
    File.basename($0) == bin
  end
end

#in_denylisted_rake_task?Boolean

Returns:

  • (Boolean)


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

def in_denylisted_rake_task?
  tasks = begin
    ::Rake.application.top_level_tasks
  rescue => e
    ::NewRelic::Agent.logger.debug("Not in Rake environment so skipping denylisted_rake_tasks check: #{e}")
    NewRelic::EMPTY_ARRAY
  end
  !(tasks & ::NewRelic::Agent.config[:'autostart.denylisted_rake_tasks'].split(/\s*,\s*/)).empty?
end