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
Returns the value of attribute active.
-
#aggregator ⇒ Object
Returns the value of attribute aggregator.
-
#sleep_time ⇒ Object
Returns the value of attribute sleep_time.
-
#subscriber ⇒ Object
Returns the value of attribute subscriber.
-
#thread ⇒ Object
Returns the value of attribute thread.
-
#transmitter ⇒ Object
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.
- #restart_thread ⇒ Object
- #send_queue ⇒ Object
- #shutdown(send_current_queue = false) ⇒ Object
- #start_thread ⇒ Object
- #subscribe ⇒ Object
Constructor Details
#initialize ⇒ Agent
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/appsignal/agent.rb', line 7 def initialize return unless Appsignal.active? if Appsignal.config.env == 'development' @sleep_time = 10.0 else @sleep_time = 60.0 end @aggregator = Aggregator.new @transmitter = Transmitter.new(ACTION) subscribe start_thread Appsignal.logger.info('Started Appsignal agent') end |
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def active @active end |
#aggregator ⇒ Object
Returns the value of attribute aggregator.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def aggregator @aggregator end |
#sleep_time ⇒ Object
Returns the value of attribute sleep_time.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def sleep_time @sleep_time end |
#subscriber ⇒ Object
Returns the value of attribute subscriber.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def subscriber @subscriber end |
#thread ⇒ Object
Returns the value of attribute thread.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def thread @thread end |
#transmitter ⇒ Object
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
54 55 56 57 |
# File 'lib/appsignal/agent.rb', line 54 def enqueue(transaction) Appsignal.logger.debug('Enqueueing transaction') aggregator.add(transaction) end |
#forked! ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/appsignal/agent.rb', line 79 def forked! Appsignal.logger.debug('Forked worker process') @forked = true Thread.exclusive do @aggregator = Aggregator.new end restart_thread end |
#forked? ⇒ Boolean
88 89 90 |
# File 'lib/appsignal/agent.rb', line 88 def forked? @forked ||= false end |
#restart_thread ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/appsignal/agent.rb', line 32 def restart_thread if @thread && @thread.alive? Appsignal.logger.debug 'Killing agent thread' Thread.kill(@thread) end start_thread end |
#send_queue ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/appsignal/agent.rb', line 59 def send_queue Appsignal.logger.debug('Sending queue') # Replace aggregator while making sure no thread # is adding to it's queue aggregator_to_be_sent = nil Thread.exclusive 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.message}" Appsignal.logger.error ex.backtrace.join('\n') end end |
#shutdown(send_current_queue = false) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/appsignal/agent.rb', line 92 def shutdown(send_current_queue=false) Appsignal.logger.info('Shutting down agent') ActiveSupport::Notifications.unsubscribe(subscriber) Thread.kill(thread) if thread send_queue if send_current_queue && @aggregator.has_transactions? end |
#start_thread ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/appsignal/agent.rb', line 21 def start_thread Appsignal.logger.debug('Starting agent thread') @thread = Thread.new do loop do send_queue if aggregator.has_transactions? Appsignal.logger.debug("Sleeping #{sleep_time}") sleep(sleep_time) end end end |
#subscribe ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/appsignal/agent.rb', line 40 def subscribe Appsignal.logger.debug('Subscribing to notifications') # Subscribe to notifications that don't start with a ! @subscriber = ActiveSupport::Notifications.subscribe(/^[^!]/) do |*args| if Appsignal::Transaction.current event = ActiveSupport::Notifications::Event.new(*args) if event.name.start_with?('process_action') Appsignal::Transaction.current.set_process_action_event(event) end Appsignal::Transaction.current.add_event(event) end end end |