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)



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

def adaptive_sampler
  @adaptive_sampler
end

#adaptive_sampler_remote_parent_not_sampledObject (readonly)



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

def adaptive_sampler_remote_parent_not_sampled
  @adaptive_sampler_remote_parent_not_sampled
end

#adaptive_sampler_remote_parent_sampledObject (readonly)



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

def adaptive_sampler_remote_parent_sampled
  @adaptive_sampler_remote_parent_sampled
end

#attribute_filterObject (readonly)



185
186
187
# File 'lib/new_relic/agent/agent.rb', line 185

def attribute_filter
  @attribute_filter
end

#custom_event_aggregatorObject (readonly)



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

def custom_event_aggregator
  @custom_event_aggregator
end

#error_collectorObject (readonly)

error collector is a simple collection of recorded errors



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

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.



170
171
172
# File 'lib/new_relic/agent/agent.rb', line 170

def events
  @events
end

#health_checkObject (readonly)

the agent control health check file generator



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

def health_check
  @health_check
end

#javascript_instrumentorObject (readonly)

builder for JS agent scripts to inject



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

def javascript_instrumentor
  @javascript_instrumentor
end

#log_event_aggregatorObject (readonly)



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

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



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

def monitors
  @monitors
end

#monotonic_gc_profilerObject (readonly)

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



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

def monotonic_gc_profiler
  @monotonic_gc_profiler
end

#record_sqlObject (readonly)

whether we should record raw, obfuscated, or no sql



162
163
164
# File 'lib/new_relic/agent/agent.rb', line 162

def record_sql
  @record_sql
end

#serverless_handlerObject (readonly)



189
190
191
# File 'lib/new_relic/agent/agent.rb', line 189

def serverless_handler
  @serverless_handler
end

#serviceObject (readonly)

service for communicating with collector



166
167
168
# File 'lib/new_relic/agent/agent.rb', line 166

def service
  @service
end

#span_event_aggregatorObject (readonly)



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

def span_event_aggregator
  @span_event_aggregator
end

#sql_samplerObject (readonly)



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

def sql_sampler
  @sql_sampler
end

#stats_engineObject (readonly)

the statistics engine that holds all the timeslice data



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

def stats_engine
  @stats_engine
end

#transaction_event_recorderObject (readonly)



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

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.



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

def transaction_rules
  @transaction_rules
end

#transaction_samplerObject (readonly)

the transaction sampler that handles recording transactions



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

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/new_relic/agent/agent.rb', line 222

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



199
200
201
# File 'lib/new_relic/agent/agent.rb', line 199

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

#drop_buffered_dataObject

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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/new_relic/agent/agent.rb', line 302

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



324
325
326
327
328
# File 'lib/new_relic/agent/agent.rb', line 324

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



252
253
254
255
256
257
258
259
260
261
# File 'lib/new_relic/agent/agent.rb', line 252

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



351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/new_relic/agent/agent.rb', line 351

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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/new_relic/agent/agent.rb', line 237

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?

  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.



297
298
299
# File 'lib/new_relic/agent/agent.rb', line 297

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



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

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.



320
321
322
# File 'lib/new_relic/agent/agent.rb', line 320

def reset_objects_with_locks
  @stats_engine = StatsEngine.new
end

#revert_to_default_configurationObject



263
264
265
266
# File 'lib/new_relic/agent/agent.rb', line 263

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



280
281
282
283
284
285
# File 'lib/new_relic/agent/agent.rb', line 280

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



195
196
197
# File 'lib/new_relic/agent/agent.rb', line 195

def synthetics_event_aggregator
  @transaction_event_recorder.synthetics_event_aggregator
end

#transaction_event_aggregatorObject



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

def transaction_event_aggregator
  @transaction_event_recorder.transaction_event_aggregator
end

#trap_signals_for_litespeedObject



268
269
270
271
272
273
274
275
# File 'lib/new_relic/agent/agent.rb', line 268

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