Module: NewRelic::Control::InstanceMethods

Included in:
NewRelic::Control
Defined in:
lib/new_relic/control/instance_methods.rb

Overview

Contains methods that relate to the runtime usage of the control object. Note that these are subject to override in the NewRelic::Control::Framework classes that are actually instantiated

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#env=(value) ⇒ Object (writeonly)

The env is the setting used to identify which section of the newrelic.yml to load. This defaults to a framework specific value, such as ENV but can be overridden as long as you set it before calling #init_plugin



14
15
16
# File 'lib/new_relic/control/instance_methods.rb', line 14

def env=(value)
  @env = value
end

#local_envObject (readonly)

The local environment contains all the information we report to the server about what kind of application this is, what gems and plugins it uses, and many other kinds of machine-dependent information useful in debugging



20
21
22
# File 'lib/new_relic/control/instance_methods.rb', line 20

def local_env
  @local_env
end

Instance Method Details

#agent_enabled?Boolean

True if dev mode or monitor mode are enabled, and we are running inside a valid dispatcher like mongrel or passenger. Can be overridden by NEWRELIC_ENABLE env variable, monitor_daemons config option when true, or agent_enabled config option when true or false.

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
# File 'lib/new_relic/control/instance_methods.rb', line 95

def agent_enabled?
  return false if !developer_mode? && !monitor_mode?
  return self['agent_enabled'].to_s =~ /true|on|yes/i if !self['agent_enabled'].nil? && self['agent_enabled'] != 'auto'
  return false if ENV['NEWRELIC_ENABLE'].to_s =~ /false|off|no/i
  return true if self['monitor_daemons'].to_s =~ /true|on|yes/i
  return true if ENV['NEWRELIC_ENABLE'].to_s =~ /true|on|yes/i
  # When in 'auto' mode the agent is enabled if there is a known
  # dispatcher running
  return true if @local_env.dispatcher != nil
end

#appObject Also known as: framework

Asks the LocalEnvironment instance which framework should be loaded



107
108
109
# File 'lib/new_relic/control/instance_methods.rb', line 107

def app
  @local_env.framework
end

#init_plugin(options = {}) ⇒ Object

Initialize the plugin/gem and start the agent. This does the necessary configuration based on the framework environment and determines whether or not to start the agent. If the agent is not going to be started then it loads the agent shim which has stubs for all the external api.

This may be invoked multiple times, as long as you don’t attempt to uninstall the agent after it has been started.

If the plugin is initialized and it determines that the agent is not enabled, it will skip starting it and install the shim. But if you later call this with :agent_enabled => true, then it will install the real agent and start it.

What determines whether the agent is launched is the result of calling agent_enabled? This will indicate whether the instrumentation should/will be installed. If we’re in a mode where tracers are not installed then we should not start the agent.

Subclasses are not allowed to override, but must implement init_config({}) which is called one or more times.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/new_relic/control/instance_methods.rb', line 46

def init_plugin(options={})
  options['app_name'] = ENV['NEWRELIC_APP_NAME'] if ENV['NEWRELIC_APP_NAME']
  options['app_name'] ||= ENV['NEW_RELIC_APP_NAME'] if ENV['NEW_RELIC_APP_NAME']

  # Merge the stringified options into the config as overrides:
  logger_override = options.delete(:log)
  environment_name = options.delete(:env) and self.env = environment_name
  dispatcher = options.delete(:dispatcher) and @local_env.dispatcher = dispatcher
  dispatcher_instance_id = options.delete(:dispatcher_instance_id) and @local_env.dispatcher_instance_id = dispatcher_instance_id


  # Clear out the settings, if they've already been loaded.  It may be that
  # between calling init_plugin the first time and the second time, the env
  # has been overridden
  @settings = nil
  settings
  merge_options(options)
  if logger_override
    @log = logger_override
    # Try to grab the log filename
    @log_file = @log.instance_eval { @logdev.filename rescue nil }
  end
  # An artifact of earlier implementation, we put both #add_method_tracer and #trace_execution
  # methods in the module methods.
  Module.send :include, NewRelic::Agent::MethodTracer::ClassMethods
  Module.send :include, NewRelic::Agent::MethodTracer::InstanceMethods
  init_config(options)
  NewRelic::Agent.agent = NewRelic::Agent::Agent.instance
  if agent_enabled? && !NewRelic::Agent.instance.started?
    setup_log unless logger_override
    start_agent
    install_instrumentation
    load_samplers unless self['disable_samplers']
    local_env.gather_environment_info
    append_environment_info
  elsif !agent_enabled?
    install_shim
  end
end

#start_agentObject

Install the real agent into the Agent module, and issue the start command.



87
88
89
# File 'lib/new_relic/control/instance_methods.rb', line 87

def start_agent
  NewRelic::Agent.agent.start
end

#to_sObject

:nodoc:



112
113
114
# File 'lib/new_relic/control/instance_methods.rb', line 112

def to_s #:nodoc:
  "Control[#{self.app}]"
end