Module: NewRelic::Agent::AgentHelpers::Startup

Included in:
NewRelic::Agent::Agent
Defined in:
lib/new_relic/agent/agent_helpers/startup.rb

Instance Method Summary collapse

Instance Method Details

#agent_should_start?Boolean

Check to see if the agent should start, returning true if it should.

Returns:

  • (Boolean)
[View source]

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 175

def agent_should_start?
  return false if already_started? || disabled?

  if defer_for_delayed_job?
    ::NewRelic::Agent.logger.debug('Deferring startup for DelayedJob')
    return false
  end

  if defer_for_resque?
    ::NewRelic::Agent.logger.debug('Deferring startup for Resque in case it daemonizes')
    return false
  end

  unless app_name_configured?
    NewRelic::Agent.agent.health_check.update_status(NewRelic::Agent::HealthCheck::MISSING_APP_NAME)
    NewRelic::Agent.logger.error('No application name configured.',
      'The agent cannot start without at least one. Please check your ',
      'newrelic.yml and ensure that it is valid and has at least one ',
      "value set for app_name in the #{NewRelic::Control.instance.env} ",
      'environment.')
    return false
  end

  return true
end

#already_started?Boolean

Check whether we have already started, which is an error condition

Returns:

  • (Boolean)
[View source]

15
16
17
18
19
20
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 15

def already_started?
  if started?
    ::NewRelic::Agent.logger.error('Agent Started Already!')
    true
  end
end

#app_name_configured?Boolean

Logs the configured application names

Returns:

  • (Boolean)
[View source]

113
114
115
116
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 113

def app_name_configured?
  names = Agent.config[:app_name]
  return names.respond_to?(:any?) && names.any?
end

#check_config_and_start_agentObject

Sanity-check the agent configuration and start the agent, setting up the worker thread and the exit handler to shut down the agent

[View source]

38
39
40
41
42
43
44
45
46
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 38

def check_config_and_start_agent
  # some health statuses, such as invalid license key, are ran before
  # the agent officially starts
  @health_check.create_and_run_health_check_loop
  return unless monitoring? && has_correct_license_key?
  return if using_forking_dispatcher?

  setup_and_start_agent
end

#connect_in_foregroundObject

Connecting in the foreground blocks further startup of the agent until we have a connection - useful in cases where you’re trying to log a very-short-running process and want to get statistics from before a server connection (typically 20 seconds) exists

[View source]

123
124
125
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 123

def connect_in_foreground
  NewRelic::Agent.disable_all_tracing { connect(:keep_retrying => false) }
end

#correct_license_lengthObject

A license key is an arbitrary 40 character string, usually looks something like a SHA1 hash

[View source]

162
163
164
165
166
167
168
169
170
171
172
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 162

def correct_license_length
  key = Agent.config[:license_key]

  if key.length == 40
    true
  else
    NewRelic::Agent.agent.health_check.update_status(NewRelic::Agent::HealthCheck::INVALID_LICENSE_KEY)
    ::NewRelic::Agent.logger.error("Invalid license key: #{key}")
    false
  end
end

#disabled?Boolean

The agent is disabled when it is not force enabled by the ‘agent_enabled’ option (e.g. in a manual start), or enabled normally through the configuration file

Returns:

  • (Boolean)
[View source]

204
205
206
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 204

def disabled?
  !Agent.config[:agent_enabled]
end

#has_correct_license_key?Boolean

Returns:

  • (Boolean)
[View source]

156
157
158
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 156

def has_correct_license_key?
  has_license_key? && correct_license_length
end

#has_license_key?Boolean

Tell the user when the license key is missing so they can fix it by adding it to the file

Returns:

  • (Boolean)
[View source]

143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 143

def has_license_key?
  if Agent.config[:license_key] && Agent.config[:license_key].length > 0
    true
  else
    NewRelic::Agent.agent.health_check.update_status(NewRelic::Agent::HealthCheck::MISSING_LICENSE_KEY)
    ::NewRelic::Agent.logger.warn('No license key found. ' +
      'This often means your newrelic.yml file was not found, or it lacks a section for the running ' \
      "environment, '#{NewRelic::Control.instance.env}'. You may also want to try linting your newrelic.yml " \
      'to ensure it is valid YML.')
    false
  end
end

#log_app_nameObject

[View source]

92
93
94
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 92

def log_app_name
  ::NewRelic::Agent.logger.info("Application: #{Agent.config[:app_name].join(', ')}")
end

#log_dispatcherObject

Logs the dispatcher to the log file to assist with debugging. When no debugger is present, logs this fact to assist with proper dispatcher detection

[View source]

82
83
84
85
86
87
88
89
90
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 82

def log_dispatcher
  dispatcher_name = Agent.config[:dispatcher].to_s

  if dispatcher_name.empty?
    ::NewRelic::Agent.logger.info('No known dispatcher detected.')
  else
    ::NewRelic::Agent.logger.info("Dispatcher: #{dispatcher_name}")
  end
end

#log_environmentObject

Log the environment the app thinks it’s running in. Useful in debugging, as this is the key for config YAML lookups.

[View source]

75
76
77
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 75

def log_environment
  ::NewRelic::Agent.logger.info("Environment: #{NewRelic::Control.instance.env}")
end

#log_ignore_url_regexesObject

[View source]

96
97
98
99
100
101
102
103
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 96

def log_ignore_url_regexes
  regexes = NewRelic::Agent.config[:'rules.ignore_url_regexes']

  unless regexes.empty?
    ::NewRelic::Agent.logger.info('Ignoring URLs that match the following regexes: ' \
      "#{regexes.map(&:inspect).join(', ')}.")
  end
end

#log_startupObject

Log startup information that we almost always want to know

[View source]

67
68
69
70
71
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 67

def log_startup
  log_environment
  log_dispatcher
  log_app_name
end

#log_version_and_pidObject

Classy logging of the agent version and the current pid, so we can disambiguate processes in the log file and make sure they’re running a reasonable version

[View source]

108
109
110
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 108

def log_version_and_pid
  ::NewRelic::Agent.logger.debug("New Relic Ruby agent #{NewRelic::VERSION::STRING} initialized: pid = #{$$}")
end

#monitoring?Boolean

Warn the user if they have configured their agent not to send data, that way we can see this clearly in the log file

Returns:

  • (Boolean)
[View source]

129
130
131
132
133
134
135
136
137
138
139
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 129

def monitoring?
  return false if Agent.config[:'serverless_mode.enabled']

  if Agent.config[:monitor_mode]
    true
  else
    NewRelic::Agent.agent.health_check.update_status(NewRelic::Agent::HealthCheck::AGENT_DISABLED)
    ::NewRelic::Agent.logger.warn('Agent configured not to send data in this environment.')
    false
  end
end

#setup_and_start_agent(options = {}) ⇒ Object

This is the shared method between the main agent startup and the after_fork call restarting the thread in deferred dispatchers.

Treatment of @started and env report is important to get right.

[View source]

52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 52

def setup_and_start_agent(options = {})
  @started = true
  @harvester.mark_started

  unless in_resque_child_process?
    install_exit_handler
    environment_for_connect
    @harvest_samplers.load_samplers unless Agent.config[:disable_samplers]
  end

  connect_in_foreground if Agent.config[:sync_startup]
  start_worker_thread(options)
end

#startObject

Logs a bunch of data and starts the agent, if needed

[View source]

23
24
25
26
27
28
29
30
31
32
33
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 23

def start
  return unless agent_should_start?

  log_startup
  check_config_and_start_agent
  log_version_and_pid

  events.subscribe(:initial_configuration_complete) do
    log_ignore_url_regexes
  end
end

#started?Boolean

True if we have initialized and completed ‘start’

Returns:

  • (Boolean)
[View source]

10
11
12
# File 'lib/new_relic/agent/agent_helpers/startup.rb', line 10

def started?
  @started
end