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)



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

def adaptive_sampler
  @adaptive_sampler
end

#attribute_filterObject (readonly)



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

def attribute_filter
  @attribute_filter
end

#cross_process_idObject (readonly)

cross application tracing ids and encoding



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

def cross_process_id
  @cross_process_id
end

#custom_event_aggregatorObject (readonly)



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

def custom_event_aggregator
  @custom_event_aggregator
end

#error_collectorObject (readonly)

error collector is a simple collection of recorded errors



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

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.



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

def events
  @events
end

#javascript_instrumentorObject (readonly)

builder for JS agent scripts to inject



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

def javascript_instrumentor
  @javascript_instrumentor
end

#log_event_aggregatorObject (readonly)



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

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



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

def monitors
  @monitors
end

#monotonic_gc_profilerObject (readonly)

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



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

def monotonic_gc_profiler
  @monotonic_gc_profiler
end

#record_sqlObject (readonly)

whether we should record raw, obfuscated, or no sql



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

def record_sql
  @record_sql
end

#serviceObject (readonly)

service for communicating with collector



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

def service
  @service
end

#span_event_aggregatorObject (readonly)



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

def span_event_aggregator
  @span_event_aggregator
end

#sql_samplerObject (readonly)



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

def sql_sampler
  @sql_sampler
end

#stats_engineObject (readonly)

the statistics engine that holds all the timeslice data



141
142
143
# File 'lib/new_relic/agent/agent.rb', line 141

def stats_engine
  @stats_engine
end

#transaction_event_recorderObject (readonly)



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

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.



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

def transaction_rules
  @transaction_rules
end

#transaction_samplerObject (readonly)

the transaction sampler that handles recording transactions



143
144
145
# File 'lib/new_relic/agent/agent.rb', line 143

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.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/new_relic/agent/agent.rb', line 207

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



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

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

#drop_buffered_dataObject

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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/new_relic/agent/agent.rb', line 288

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



310
311
312
313
314
# File 'lib/new_relic/agent/agent.rb', line 310

def flush_pipe_data
  if connected? && @service.is_a?(PipeService)
    transmit_data_types
  end
end

#install_pipe_service(channel_id) ⇒ Object



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

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



337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/new_relic/agent/agent.rb', line 337

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)


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

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.



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

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



277
278
279
# File 'lib/new_relic/agent/agent.rb', line 277

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.



306
307
308
# File 'lib/new_relic/agent/agent.rb', line 306

def reset_objects_with_locks
  @stats_engine = StatsEngine.new
end

#revert_to_default_configurationObject



249
250
251
252
# File 'lib/new_relic/agent/agent.rb', line 249

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



266
267
268
269
270
271
# File 'lib/new_relic/agent/agent.rb', line 266

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



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

def synthetics_event_aggregator
  @transaction_event_recorder.synthetics_event_aggregator
end

#transaction_event_aggregatorObject



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

def transaction_event_aggregator
  @transaction_event_recorder.transaction_event_aggregator
end

#trap_signals_for_litespeedObject



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

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