Module: NewRelic::Control::InstanceMethods

Includes:
PrivateInstanceMethods
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



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

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



27
28
29
# File 'lib/new_relic/control/instance_methods.rb', line 27

def local_env
  @local_env
end

Instance Method Details

#[](key) ⇒ Object

for backward compatibility with the old config interface



130
131
132
# File 'lib/new_relic/control/instance_methods.rb', line 130

def [](key)
  NewRelic::Agent.config[key.to_sym]
end

#appObject



121
122
123
# File 'lib/new_relic/control/instance_methods.rb', line 121

def app
  Agent.config[:framework]
end

#configure_agent(env, options) ⇒ Object



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

def configure_agent(env, options)
  manual = Agent::Configuration::ManualSource.new(options)
  Agent.config.replace_or_add_config(manual)
  yaml_source = Agent::Configuration::YamlSource.new(config_file_path, env)
  log_yaml_source_failures(yaml_source) if yaml_source.failed?
  Agent.config.replace_or_add_config(yaml_source)
  configure_high_security
end

#determine_env(options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/new_relic/control/instance_methods.rb', line 78

def determine_env(options)
  env = options[:env] || self.env
  env = env.to_s

  if @started_in_env && @started_in_env != env
    Agent.logger.error("Attempted to start agent in #{env.inspect} environment, but agent was already running " \
      "in #{@started_in_env.inspect}", "The agent will continue running in #{@started_in_env.inspect}. To " \
      'alter this, ensure the desired environment is set before the agent starts.')
  else
    Agent.logger.info("Starting the New Relic agent version #{NewRelic::VERSION::STRING} in #{env.inspect} " \
      'environment.', 'To prevent agent startup add a NEW_RELIC_AGENT_ENABLED=false environment variable or ' \
      "modify the #{env.inspect} section of your newrelic.yml.")
  end

  env
end

#dispatcherObject



138
139
140
# File 'lib/new_relic/control/instance_methods.rb', line 138

def dispatcher
  NewRelic::Agent.config[:dispatcher]
end

#frameworkObject



125
126
127
# File 'lib/new_relic/control/instance_methods.rb', line 125

def framework
  Agent.config[:framework]
end

#handle_invalid_security_settingsObject



108
109
110
111
112
113
# File 'lib/new_relic/control/instance_methods.rb', line 108

def handle_invalid_security_settings
  NewRelic::Agent.logger.error('Security Policies and High Security Mode cannot both be present in the agent ' \
    'configuration. If Security Policies have been set for your account, please ensure the ' \
    'security_policies_token is set but high_security is disabled (default).')
  install_shim
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.



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
# File 'lib/new_relic/control/instance_methods.rb', line 52

def init_plugin(options = {})
  env = determine_env(options)

  configure_agent(env, options)

  # Be sure to only create once! RUBY-1020
  create_logger(options)

  # Merge the stringified options into the config as overrides:
  environment_name = options.delete(:env) and self.env = environment_name

  NewRelic::Agent::PipeChannelManager.listener.start if options.delete(:start_channel_listener)

  # An artifact of earlier implementation, we put both #add_method_tracer and #trace_execution
  # methods in the module methods.
  # Rails applications load the next two lines before any other initializers are run
  unless defined?(Rails::VERSION) && ENV['NEW_RELIC_DEFER_RAILS_INITIALIZATION']
    Module.send(:include, NewRelic::Agent::MethodTracer::ClassMethods)
    Module.send(:include, NewRelic::Agent::MethodTracer)
  end

  init_config(options)
  NewRelic::Agent.agent = NewRelic::Agent::Agent.instance
  init_instrumentation
end

#newrelic_rootObject

Delegates to the class method newrelic_root, implemented by each subclass



144
145
146
# File 'lib/new_relic/control/instance_methods.rb', line 144

def newrelic_root
  self.class.newrelic_root
end

#security_settings_valid?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/new_relic/control/instance_methods.rb', line 104

def security_settings_valid?
  !Agent.config[:high_security] || Agent.config[:security_policies_token].empty?
end

#settingsObject



134
135
136
# File 'lib/new_relic/control/instance_methods.rb', line 134

def settings
  NewRelic::Agent.config.to_collector_hash
end

#start_agentObject

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



116
117
118
119
# File 'lib/new_relic/control/instance_methods.rb', line 116

def start_agent
  @started_in_env = self.env
  NewRelic::Agent.agent.start
end