Module: NewRelic::Agent::Agent::InstanceMethods

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

Overview

Holds all the methods defined on NewRelic::Agent::Agent instances

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adaptive_samplerObject (readonly)



180
181
182
# File 'lib/new_relic/agent/agent.rb', line 180

def adaptive_sampler
  @adaptive_sampler
end

#attribute_filterObject (readonly)



179
180
181
# File 'lib/new_relic/agent/agent.rb', line 179

def attribute_filter
  @attribute_filter
end

#cross_process_idObject (readonly)

cross application tracing ids and encoding



158
159
160
# File 'lib/new_relic/agent/agent.rb', line 158

def cross_process_id
  @cross_process_id
end

#custom_event_aggregatorObject (readonly)



175
176
177
# File 'lib/new_relic/agent/agent.rb', line 175

def custom_event_aggregator
  @custom_event_aggregator
end

#error_collectorObject (readonly)

error collector is a simple collection of recorded errors



152
153
154
# File 'lib/new_relic/agent/agent.rb', line 152

def error_collector
  @error_collector
end

#eventsObject (readonly)

Global events dispatcher. This will provides our primary mechanism for agent-wide events, such as finishing configuration, error notification and request before/after from Rack.



164
165
166
# File 'lib/new_relic/agent/agent.rb', line 164

def events
  @events
end

#health_checkObject (readonly)

the agent control health check file generator



145
146
147
# File 'lib/new_relic/agent/agent.rb', line 145

def health_check
  @health_check
end

#javascript_instrumentorObject (readonly)

builder for JS agent scripts to inject



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

def javascript_instrumentor
  @javascript_instrumentor
end

#log_event_aggregatorObject (readonly)



177
178
179
# File 'lib/new_relic/agent/agent.rb', line 177

def log_event_aggregator
  @log_event_aggregator
end

#monitorsObject (readonly)

listens and responds to events that need to process headers for synthetics and distributed tracing



168
169
170
# File 'lib/new_relic/agent/agent.rb', line 168

def monitors
  @monitors
end

#monotonic_gc_profilerObject (readonly)

GC::Profiler.total_time is not monotonic so we wrap it.



174
175
176
# File 'lib/new_relic/agent/agent.rb', line 174

def monotonic_gc_profiler
  @monotonic_gc_profiler
end

#record_sqlObject (readonly)

whether we should record raw, obfuscated, or no sql



154
155
156
# File 'lib/new_relic/agent/agent.rb', line 154

def record_sql
  @record_sql
end

#serverless_handlerObject (readonly)



181
182
183
# File 'lib/new_relic/agent/agent.rb', line 181

def serverless_handler
  @serverless_handler
end

#serviceObject (readonly)

service for communicating with collector



160
161
162
# File 'lib/new_relic/agent/agent.rb', line 160

def service
  @service
end

#span_event_aggregatorObject (readonly)



176
177
178
# File 'lib/new_relic/agent/agent.rb', line 176

def span_event_aggregator
  @span_event_aggregator
end

#sql_samplerObject (readonly)



150
151
152
# File 'lib/new_relic/agent/agent.rb', line 150

def sql_sampler
  @sql_sampler
end

#stats_engineObject (readonly)

the statistics engine that holds all the timeslice data



147
148
149
# File 'lib/new_relic/agent/agent.rb', line 147

def stats_engine
  @stats_engine
end

#transaction_event_recorderObject (readonly)



178
179
180
# File 'lib/new_relic/agent/agent.rb', line 178

def transaction_event_recorder
  @transaction_event_recorder
end

#transaction_rulesObject

Transaction and metric renaming rules as provided by the collector on connect. The former are applied during txns, the latter during harvest.



172
173
174
# File 'lib/new_relic/agent/agent.rb', line 172

def transaction_rules
  @transaction_rules
end

#transaction_samplerObject (readonly)

the transaction sampler that handles recording transactions



149
150
151
# File 'lib/new_relic/agent/agent.rb', line 149

def transaction_sampler
  @transaction_sampler
end

Instance Method Details

#after_fork(options = {}) ⇒ Object

This method should be called in a forked process after a fork. It assumes the parent process initialized the agent, but does not assume the agent started.

The call is idempotent, but not reentrant.

  • It clears any metrics carried over from the parent process

  • Restarts the sampler thread if necessary

  • Initiates a new agent run and worker loop unless that was done in the parent process and :force_reconnect is not true

Options:

  • :force_reconnect => true to force the spawned process to establish a new connection, such as when forking a long running process. The default is false–it will only connect to the server if the parent had not connected.

  • :keep_retrying => false if we try to initiate a new connection, this tells me to only try it once so this method returns quickly if there is some kind of latency with the server.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/new_relic/agent/agent.rb', line 214

def after_fork(options = {})
  return unless needs_after_fork_work?

  ::NewRelic::Agent.logger.debug("Starting the worker thread in #{Process.pid} (parent #{Process.ppid}) after forking.")

  channel_id = options[:report_to_channel]
  install_pipe_service(channel_id) if channel_id

  # Clear out locks and stats left over from parent process
  reset_objects_with_locks
  drop_buffered_data

  setup_and_start_agent(options)
end

#agent_id=(agent_id) ⇒ Object



191
192
193
# File 'lib/new_relic/agent/agent.rb', line 191

def agent_id=(agent_id)
  @service.agent_id = agent_id
end

#drop_buffered_dataObject

Clear out the metric data, errors, and transaction traces, etc.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/new_relic/agent/agent.rb', line 295

def drop_buffered_data
  @stats_engine.reset!
  @error_collector.drop_buffered_data
  @transaction_sampler.reset!
  @transaction_event_recorder.drop_buffered_data
  @custom_event_aggregator.reset!
  @span_event_aggregator.reset!
  @log_event_aggregator.reset!
  @sql_sampler.reset!

  if Agent.config[:clear_transaction_state_after_fork]
    Tracer.clear_state
  end
end

#flush_pipe_dataObject

used only by resque



317
318
319
320
321
# File 'lib/new_relic/agent/agent.rb', line 317

def flush_pipe_data # used only by resque
  if connected? && @service.is_a?(PipeService)
    transmit_data_types
  end
end

#install_pipe_service(channel_id) ⇒ Object



245
246
247
248
249
250
251
252
253
254
# File 'lib/new_relic/agent/agent.rb', line 245

def install_pipe_service(channel_id)
  @service = PipeService.new(channel_id)
  if connected?
    @connected_pid = Process.pid
  else
    ::NewRelic::Agent.logger.debug("Child process #{Process.pid} not reporting to non-connected parent (process #{Process.ppid}).")
    @service.shutdown
    disconnect
  end
end

#merge_data_for_endpoint(endpoint, data) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/new_relic/agent/agent.rb', line 344

def merge_data_for_endpoint(endpoint, data)
  if data && !data.empty?
    container = container_for_endpoint(endpoint)
    if container.respond_to?(:has_metadata?) && container.has_metadata?
      container_for_endpoint(endpoint).merge!(data, false)
    else
      container_for_endpoint(endpoint).merge!(data)
    end
  end
rescue => e
  NewRelic::Agent.logger.error("Error while merging #{endpoint} data from child: ", e)
end

#needs_after_fork_work?Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/new_relic/agent/agent.rb', line 229

def needs_after_fork_work?
  needs_restart = false
  @after_fork_lock.synchronize do
    needs_restart = @harvester.needs_restart?
    @harvester.mark_started
  end

  return false if !needs_restart ||
    !Agent.config[:agent_enabled] ||
    !Agent.config[:monitor_mode] ||
    disconnected? ||
    !control.security_settings_valid?

  true
end

#pop_trace_execution_flagObject

Pop the current trace execution status. Restore trace execution status to what it was before we pushed the current flag.



290
291
292
# File 'lib/new_relic/agent/agent.rb', line 290

def pop_trace_execution_flag # THREAD_LOCAL_ACCESS
  Tracer.state.pop_traced
end

#push_trace_execution_flag(should_trace = false) ⇒ Object

Push flag indicating whether we should be tracing in this thread. This uses a stack which allows us to disable tracing children of a transaction without affecting the tracing of the whole transaction



284
285
286
# File 'lib/new_relic/agent/agent.rb', line 284

def push_trace_execution_flag(should_trace = false) # THREAD_LOCAL_ACCESS
  Tracer.state.push_traced(should_trace)
end

#reset_objects_with_locksObject

Clear out state for any objects that we know lock from our parents This is necessary for cases where we’re in a forked child and Ruby might be holding locks for background thread that aren’t there anymore.



313
314
315
# File 'lib/new_relic/agent/agent.rb', line 313

def reset_objects_with_locks
  @stats_engine = StatsEngine.new
end

#revert_to_default_configurationObject



256
257
258
259
# File 'lib/new_relic/agent/agent.rb', line 256

def revert_to_default_configuration
  Agent.config.remove_config_type(:manual)
  Agent.config.remove_config_type(:server)
end

#set_record_sql(should_record) ⇒ Object

Sets a thread local variable as to whether we should or should not record sql in the current thread. Returns the previous value, if there is one



273
274
275
276
277
278
# File 'lib/new_relic/agent/agent.rb', line 273

def set_record_sql(should_record) # THREAD_LOCAL_ACCESS
  state = Tracer.state
  prev = state.record_sql
  state.record_sql = should_record
  prev.nil? || prev
end

#synthetics_event_aggregatorObject



187
188
189
# File 'lib/new_relic/agent/agent.rb', line 187

def synthetics_event_aggregator
  @transaction_event_recorder.synthetics_event_aggregator
end

#transaction_event_aggregatorObject



183
184
185
# File 'lib/new_relic/agent/agent.rb', line 183

def transaction_event_aggregator
  @transaction_event_recorder.transaction_event_aggregator
end

#trap_signals_for_litespeedObject



261
262
263
264
265
266
267
268
# File 'lib/new_relic/agent/agent.rb', line 261

def trap_signals_for_litespeed
  # if litespeed, then ignore all future SIGUSR1 - it's
  # litespeed trying to shut us down
  if Agent.config[:dispatcher] == :litespeed
    Signal.trap('SIGUSR1', 'IGNORE')
    Signal.trap('SIGTERM', 'IGNORE')
  end
end