module NewRelic::Agent::Agent::InstanceMethods

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

Attributes

adaptive_sampler[R]
attribute_filter[R]
cross_process_id[R]

cross application tracing ids and encoding

custom_event_aggregator[R]
error_collector[R]

error collector is a simple collection of recorded errors

events[R]

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.

javascript_instrumentor[R]

builder for JS agent scripts to inject

log_event_aggregator[R]
monitors[R]

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

monotonic_gc_profiler[R]

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

record_sql[R]

whether we should record raw, obfuscated, or no sql

serverless_handler[R]
service[R]

service for communicating with collector

span_event_aggregator[R]
sql_sampler[R]
stats_engine[R]

the statistics engine that holds all the timeslice data

transaction_event_recorder[R]
transaction_rules[RW]

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

transaction_sampler[R]

the transaction sampler that handles recording transactions

Public Instance Methods

after_fork(options = {}) click to toggle source

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.

# File lib/new_relic/agent/agent.rb, line 210
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) click to toggle source
# File lib/new_relic/agent/agent.rb, line 187
def agent_id=(agent_id)
  @service.agent_id = agent_id
end
drop_buffered_data() click to toggle source

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

# File lib/new_relic/agent/agent.rb, line 291
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_data() click to toggle source
# File lib/new_relic/agent/agent.rb, line 313
def flush_pipe_data # used only by resque
  if connected? && @service.is_a?(PipeService)
    transmit_data_types
  end
end
install_pipe_service(channel_id) click to toggle source
# File lib/new_relic/agent/agent.rb, line 241
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) click to toggle source
# File lib/new_relic/agent/agent.rb, line 340
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?() click to toggle source
# File lib/new_relic/agent/agent.rb, line 225
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_flag() click to toggle source

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

# File lib/new_relic/agent/agent.rb, line 286
def pop_trace_execution_flag # THREAD_LOCAL_ACCESS
  Tracer.state.pop_traced
end
push_trace_execution_flag(should_trace = false) click to toggle source

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

# File lib/new_relic/agent/agent.rb, line 280
def push_trace_execution_flag(should_trace = false) # THREAD_LOCAL_ACCESS
  Tracer.state.push_traced(should_trace)
end
reset_objects_with_locks() click to toggle source

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.

# File lib/new_relic/agent/agent.rb, line 309
def reset_objects_with_locks
  @stats_engine = StatsEngine.new
end
revert_to_default_configuration() click to toggle source
# File lib/new_relic/agent/agent.rb, line 252
def revert_to_default_configuration
  Agent.config.remove_config_type(:manual)
  Agent.config.remove_config_type(:server)
end
set_record_sql(should_record) click to toggle source

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

# File lib/new_relic/agent/agent.rb, line 269
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_aggregator() click to toggle source
# File lib/new_relic/agent/agent.rb, line 183
def synthetics_event_aggregator
  @transaction_event_recorder.synthetics_event_aggregator
end
transaction_event_aggregator() click to toggle source
# File lib/new_relic/agent/agent.rb, line 179
def transaction_event_aggregator
  @transaction_event_recorder.transaction_event_aggregator
end
trap_signals_for_litespeed() click to toggle source
# File lib/new_relic/agent/agent.rb, line 257
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

Private Instance Methods

container_for_endpoint(endpoint) click to toggle source
# File lib/new_relic/agent/agent.rb, line 326
def container_for_endpoint(endpoint)
  case endpoint
  when :metric_data then @stats_engine
  when :transaction_sample_data then @transaction_sampler
  when :error_data then @error_collector.error_trace_aggregator
  when :error_event_data then @error_collector.error_event_aggregator
  when :analytic_event_data then transaction_event_aggregator
  when :custom_event_data then @custom_event_aggregator
  when :span_event_data then span_event_aggregator
  when :sql_trace_data then @sql_sampler
  when :log_event_data then @log_event_aggregator
  end
end
control() click to toggle source

A shorthand for NewRelic::Control.instance

# File lib/new_relic/agent/agent.rb, line 322
def control
  NewRelic::Control.instance
end