Class: OneApm::Agent::Agent

Inherits:
Object
  • Object
show all
Includes:
Connect, ContainerDataManager, ForkableDispatcherFunctions, Helpers, RuleFetcher, Start, StartWorkerThread
Defined in:
lib/one_apm/agent.rb,
lib/one_apm/agent/agent/start.rb,
lib/one_apm/agent/agent/connect.rb,
lib/one_apm/agent/agent/helpers.rb,
lib/one_apm/agent/agent/rule_fetcher.rb,
lib/one_apm/agent/agent/start_worker_thread.rb,
lib/one_apm/agent/agent/container_data_manager.rb,
lib/one_apm/agent/agent/forkable_dispatcher_functions.rb

Direct Known Subclasses

ShimAgent

Defined Under Namespace

Modules: Connect, ContainerDataManager, ForkableDispatcherFunctions, Helpers, RuleFetcher, Start, StartWorkerThread

Constant Summary

Constants included from StartWorkerThread

StartWorkerThread::OA_LOG_ONCE_KEYS_RESET_PERIOD, StartWorkerThread::OA_MIN_ALLOWED_REPORT_PERIOD, StartWorkerThread::OA_UTILIZATION_REPORT_PERIOD

Instance Attribute Summary collapse

Attributes included from ContainerDataManager

#agent_command_router, #custom_event_aggregator, #error_collector, #sql_sampler, #stats_engine, #transaction_event_aggregator, #transaction_sampler

Attributes included from Connect

#connect_attempts

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RuleFetcher

#update_rule

Methods included from ForkableDispatcherFunctions

#after_fork, #flush_pipe_data, #install_pipe_service, #reset_harvest_locks, #reset_objects_with_locks, #synchronize_with_harvest

Methods included from ContainerDataManager

#drop_buffered_data, #merge_data_for_endpoint

Methods included from Helpers

#obfuscator, #pop_trace_execution_flag, #push_trace_execution_flag, #set_record_sql, #set_record_tt

Methods included from Connect

#apdex_f, #client_ip, #connect_retry_period, #connect_settings, #connect_to_server, #connected?, #disconnect, #disconnected?, #environment_for_connect, #finish_setup, #generate_environment_report, #handle_license_error, #handle_unrecoverable_agent_error, #local_host, #log_collector_messages, #log_connection!, #log_error, #note_connect_failure, #query_server_for_configuration, #sanitize_environment_report, #should_connect?

Methods included from StartWorkerThread

#create_and_run_event_loop, #create_event_loop, #deferred_work!, #report_period_for, #start_worker_thread, #stop_event_loop

Methods included from Start

#already_started?, #app_name_configured?, #check_config_and_start_agent, #connect_in_foreground, #defer_for_background_jobs?, #defer_for_delayed_job?, #defer_for_resque?, #disabled?, #has_correct_license_key?, #has_license_key?, #in_resque_child_process?, #install_exit_handler, #log_app_name, #log_dispatcher, #log_environment, #log_ignore_url_regexes, #log_startup, #log_tier_name, #log_version_and_pid, #monitoring?, #setup_and_start_agent, #started?, #tier_name_configured?, #using_forking_dispatcher?, #using_sinatra?, #weird_ruby?

Constructor Details

#initializeAgent

Returns a new instance of Agent.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/one_apm/agent.rb', line 86

def initialize
  start_service

  @events = OneApm::Support::EventListener.new

  init_containers
  
  OneApm::Agent::SyntheticsMonitor.new @events
  OneApm::Agent::RestartMonitor.new

  @cross_app_monitor        = OneApm::Agent::CrossAppMonitor.new(@events)
  @transaction_rules        = OneApm::Support::RulesEngine.new
  @harvest_samplers         = OneApm::Collector::SamplerCollection.new(@events)
  @monotonic_gc_profiler    = OneApm::Support::VM::MonotonicGCProfiler.new
  @javascript_instrumentor  = OneApm::Agent::JavascriptInstrumentor.new(@events)
  @restart                  = OneApm::Agent::Restart.new(@events)
  @cross_app_samples_sender = OneApm::Agent::CrossAppSamplesSender.new(@events, self)

  @harvester       = OneApm::Agent::Harvester.new(@events)
  @after_fork_lock = Mutex.new

  @connect_state      = :pending
  @connect_attempts   = 0
  @environment_report = nil

  @harvest_lock = Mutex.new
end

Instance Attribute Details

#cross_app_encoding_bytesObject (readonly)

Returns the value of attribute cross_app_encoding_bytes.



62
63
64
# File 'lib/one_apm/agent.rb', line 62

def cross_app_encoding_bytes
  @cross_app_encoding_bytes
end

#cross_app_monitorObject (readonly)

Returns the value of attribute cross_app_monitor.



63
64
65
# File 'lib/one_apm/agent.rb', line 63

def cross_app_monitor
  @cross_app_monitor
end

#cross_process_idObject (readonly)

Returns the value of attribute cross_process_id.



61
62
63
# File 'lib/one_apm/agent.rb', line 61

def cross_process_id
  @cross_process_id
end

#eventsObject (readonly)

Returns the value of attribute events.



67
68
69
# File 'lib/one_apm/agent.rb', line 67

def events
  @events
end

#external_rename_rulesObject

Returns the value of attribute external_rename_rules.



70
71
72
# File 'lib/one_apm/agent.rb', line 70

def external_rename_rules
  @external_rename_rules
end

#harvest_lockObject (readonly)

Returns the value of attribute harvest_lock.



74
75
76
# File 'lib/one_apm/agent.rb', line 74

def harvest_lock
  @harvest_lock
end

#harvest_samplersObject (readonly)

Returns the value of attribute harvest_samplers.



73
74
75
# File 'lib/one_apm/agent.rb', line 73

def harvest_samplers
  @harvest_samplers
end

#harvesterObject (readonly)

Returns the value of attribute harvester.



72
73
74
# File 'lib/one_apm/agent.rb', line 72

def harvester
  @harvester
end

#javascript_instrumentorObject (readonly)

Returns the value of attribute javascript_instrumentor.



59
60
61
# File 'lib/one_apm/agent.rb', line 59

def javascript_instrumentor
  @javascript_instrumentor
end

#monotonic_gc_profilerObject (readonly)

Returns the value of attribute monotonic_gc_profiler.



76
77
78
# File 'lib/one_apm/agent.rb', line 76

def monotonic_gc_profiler
  @monotonic_gc_profiler
end

#record_sqlObject (readonly)

Returns the value of attribute record_sql.



58
59
60
# File 'lib/one_apm/agent.rb', line 58

def record_sql
  @record_sql
end

#serviceObject

Returns the value of attribute service.



65
66
67
# File 'lib/one_apm/agent.rb', line 65

def service
  @service
end

#transaction_rulesObject (readonly)

Returns the value of attribute transaction_rules.



69
70
71
# File 'lib/one_apm/agent.rb', line 69

def transaction_rules
  @transaction_rules
end

Class Method Details

.configObject



82
83
84
# File 'lib/one_apm/agent.rb', line 82

def self.config
  OneApm::Manager.config
end

.instanceObject



78
79
80
# File 'lib/one_apm/agent.rb', line 78

def self.instance
  @instance ||= self.new
end

Instance Method Details

#agent_should_start?Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
# File 'lib/one_apm/agent.rb', line 148

def agent_should_start?
  return false if already_started? || disabled? || defer_for_background_jobs?
  
  unless app_name_configured? && tier_name_configured?
    OneApm::Manager.logger.error "No application name or tier name configured in #{probe.env} environment"
    false
  else
    true
  end
end

#connect(options = {}) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/one_apm/agent.rb', line 159

def connect(options={})
  opts = {
    :keep_retrying => OneApm::Manager.config[:keep_retrying],
    :force_reconnect => OneApm::Manager.config[:force_reconnect]
  }.merge(options)

  return unless should_connect?(opts[:force_reconnect])

  OneApm::Manager.logger.debug "Connecting Process to OneApm: #$0"
  query_server_for_configuration
  @connected_pid = Process.pid
  @connect_state = :connected
rescue OneApm::ForceDisconnectException => e
  handle_force_disconnect(e)
rescue OneApm::LicenseException => e
  handle_license_error(e)
rescue OneApm::UnrecoverableAgentException => e
  handle_unrecoverable_agent_error(e)
rescue StandardError, Timeout::Error, OneApm::ServerConnectionException => e
  log_error(e)
  if opts[:keep_retrying]
    note_connect_failure
    OneApm::Manager.logger.info "Will re-attempt in #{connect_retry_period} seconds"
    sleep connect_retry_period
    retry
  else
    disconnect
  end
rescue Exception => e
  OneApm::Manager.logger.error "Exception of unexpected type during Agent#connect():", e

  raise
end

#graceful_disconnectObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/one_apm/agent.rb', line 193

def graceful_disconnect
  if connected?
    begin
      @service.request_timeout = 10

      @events.notify(:before_shutdown)
      transmit_data
      transmit_event_data
      transmit_utilization_data if OneApm::Manager.config[:collect_utilization]

      if @connected_pid == $$ && !@service.kind_of?(OneApm::Collector::CollectorService)
        OneApm::Manager.logger.debug "Sending OneApm service agent run shutdown message"
        @service.shutdown(Time.now.to_f)
      else
        OneApm::Manager.logger.debug "This agent connected from parent process #{@connected_pid}--not sending shutdown"
      end
      OneApm::Manager.logger.debug "Graceful disconnect complete"
    rescue Timeout::Error, StandardError => e
      OneApm::Manager.logger.debug "Error when disconnecting #{e.class.name}: #{e.message}"
    end
  else
    OneApm::Manager.logger.debug "Bypassing graceful disconnect - agent not connected"
  end
end

#probeObject



114
115
116
# File 'lib/one_apm/agent.rb', line 114

def probe
  OneApm::Probe.instance
end

#shutdown(options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/one_apm/agent.rb', line 136

def shutdown(options = {})
  return if not started?
  OneApm::Manager.logger.info "Starting Agent shutdown"

  stop_event_loop
  untraced_graceful_disconnect
  OneApm::Manager.revert_to_default_configuration

  @started = nil
  OneApm::Probe.reset
end

#startObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/one_apm/agent.rb', line 124

def start
  return unless agent_should_start?

  log_startup
  check_config_and_start_agent
  log_version_and_pid

  events.subscribe(:finished_configuring) do
    log_ignore_url_regexes
  end
end

#start_serviceObject



118
119
120
121
122
# File 'lib/one_apm/agent.rb', line 118

def start_service
  if OneApm::Manager.config[:monitor_mode] && !@service
    @service = OneApm::Collector::CollectorService.new
  end
end

#untraced_graceful_disconnectObject



218
219
220
221
222
223
224
# File 'lib/one_apm/agent.rb', line 218

def untraced_graceful_disconnect
  OneApm::Manager.disable_all_tracing do
    graceful_disconnect
  end
rescue => e
  OneApm::Manager.logger.error e
end