Class: Appsignal::Agent
- Inherits:
-
Object
- Object
- Appsignal::Agent
- Defined in:
- lib/appsignal/agent.rb
Constant Summary collapse
- ACTION =
'log_entries'.freeze
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#aggregator ⇒ Object
readonly
Returns the value of attribute aggregator.
-
#aggregator_semaphore ⇒ Object
readonly
Returns the value of attribute aggregator_semaphore.
-
#sleep_time ⇒ Object
readonly
Returns the value of attribute sleep_time.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#transmitter ⇒ Object
readonly
Returns the value of attribute transmitter.
Instance Method Summary collapse
- #enqueue(transaction) ⇒ Object
- #forked! ⇒ Object
- #forked? ⇒ Boolean
-
#initialize ⇒ Agent
constructor
A new instance of Agent.
- #send_queue ⇒ Object
- #shutdown(send_current_queue = false) ⇒ Object
Constructor Details
#initialize ⇒ Agent
Returns a new instance of Agent.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/appsignal/agent.rb', line 7 def initialize return unless Appsignal.active? @sleep_time = 60.0 @aggregator = Aggregator.new @aggregator_semaphore = Mutex.new @retry_request = true @thread = Thread.new do Appsignal.logger.debug('Starting agent thread') while true do send_queue if aggregator.has_transactions? Appsignal.logger.debug("Sleeping #{sleep_time}") sleep(sleep_time) end end @transmitter = Transmitter.new( Appsignal.config.fetch(:endpoint), ACTION, Appsignal.config.fetch(:api_key) ) Appsignal.logger.info('Started Appsignal agent') end |
Instance Attribute Details
#active ⇒ Object (readonly)
Returns the value of attribute active.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def active @active end |
#aggregator ⇒ Object (readonly)
Returns the value of attribute aggregator.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def aggregator @aggregator end |
#aggregator_semaphore ⇒ Object (readonly)
Returns the value of attribute aggregator_semaphore.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def aggregator_semaphore @aggregator_semaphore end |
#sleep_time ⇒ Object (readonly)
Returns the value of attribute sleep_time.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def sleep_time @sleep_time end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def thread @thread end |
#transmitter ⇒ Object (readonly)
Returns the value of attribute transmitter.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def transmitter @transmitter end |
Instance Method Details
#enqueue(transaction) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/appsignal/agent.rb', line 29 def enqueue(transaction) Appsignal.logger.debug('Enqueueing transaction') aggregator_semaphore.synchronize do aggregator.add(transaction) end end |
#forked! ⇒ Object
53 54 55 56 57 |
# File 'lib/appsignal/agent.rb', line 53 def forked! @forked = true @aggregator = Aggregator.new Appsignal.logger.info('Forked the Appsignal agent') end |
#forked? ⇒ Boolean
59 60 61 |
# File 'lib/appsignal/agent.rb', line 59 def forked? @forked ||= false end |
#send_queue ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/appsignal/agent.rb', line 36 def send_queue Appsignal.logger.debug('Sending queue') aggregator_to_be_sent = nil aggregator_semaphore.synchronize do aggregator_to_be_sent = aggregator @aggregator = Aggregator.new end begin handle_result( transmitter.transmit(aggregator_to_be_sent.post_processed_queue!) ) rescue Exception => ex Appsignal.logger.error "#{ex.class} while sending queue: #{ex.}" Appsignal.logger.error ex.backtrace.join('\n') end end |
#shutdown(send_current_queue = false) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/appsignal/agent.rb', line 63 def shutdown(send_current_queue=false) Appsignal.logger.info('Shutting down the agent') ActiveSupport::Notifications.unsubscribe(Appsignal.subscriber) Thread.kill(thread) if thread send_queue if send_current_queue && @aggregator.has_transactions? end |