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.
-
#master_pid ⇒ Object
Returns the value of attribute master_pid.
-
#paused ⇒ Object
Returns the value of attribute paused.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#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
- #active? ⇒ Boolean
- #clear_queue ⇒ Object
- #enqueue(transaction) ⇒ Object
- #forked! ⇒ Object
-
#initialize ⇒ Agent
constructor
A new instance of Agent.
- #restart_thread ⇒ Object
- #resubscribe ⇒ Object
- #send_queue ⇒ Object
- #shutdown(send_current_queue = false, reason = nil) ⇒ Object
- #start_thread ⇒ Object
- #stop_thread ⇒ Object
- #subscribe ⇒ Object
- #unsubscribe ⇒ Object
Constructor Details
#initialize ⇒ Agent
Returns a new instance of Agent.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/appsignal/agent.rb', line 8 def initialize return unless Appsignal.config.active? if Appsignal.config.env == 'development' @sleep_time = 10.0 else @sleep_time = 60.0 end @master_pid = Process.pid @pid = @master_pid @aggregator = Aggregator.new @transmitter = Transmitter.new(ACTION) subscribe start_thread @active = true 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 |
#master_pid ⇒ Object
Returns the value of attribute master_pid.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def master_pid @master_pid end |
#paused ⇒ Object
Returns the value of attribute paused.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def paused @paused end |
#pid ⇒ Object
Returns the value of attribute pid.
5 6 7 |
# File 'lib/appsignal/agent.rb', line 5 def pid @pid 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
#active? ⇒ Boolean
25 26 27 |
# File 'lib/appsignal/agent.rb', line 25 def active? !! @active end |
#clear_queue ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/appsignal/agent.rb', line 119 def clear_queue Appsignal.logger.debug('Clearing queue') # Replace aggregator while making sure no thread # is adding to it's queue Thread.exclusive do @aggregator = Aggregator.new end end |
#enqueue(transaction) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/appsignal/agent.rb', line 87 def enqueue(transaction) forked! if @pid != Process.pid if Appsignal.is_ignored_action?(transaction.action) Appsignal.logger.debug("Ignoring transaction: #{transaction.request_id} (#{transaction.action})") return end aggregator.add(transaction) end |
#forked! ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/appsignal/agent.rb', line 128 def forked! Appsignal.logger.info('Forked worker process') @active = true @pid = Process.pid Thread.exclusive do @aggregator = Aggregator.new end resubscribe restart_thread end |
#restart_thread ⇒ Object
46 47 48 49 50 |
# File 'lib/appsignal/agent.rb', line 46 def restart_thread Appsignal.logger.debug 'Restarting agent thread' stop_thread start_thread end |
#resubscribe ⇒ Object
75 76 77 78 79 |
# File 'lib/appsignal/agent.rb', line 75 def resubscribe Appsignal.logger.debug('Resubscribing to notifications') unsubscribe subscribe end |
#send_queue ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/appsignal/agent.rb', line 96 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 return unless aggregator_to_be_sent.has_transactions? handle_result( transmitter.transmit(aggregator_to_be_sent.post_processed_queue!) ) rescue OpenSSL::SSL::SSLError => ex Appsignal.logger.error "OpenSSL::SSL::SSLError: #{ex.}" 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, reason = nil) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/appsignal/agent.rb', line 139 def shutdown(send_current_queue=false, reason=nil) Appsignal.logger.info("Shutting down agent (#{reason})") @active = false unsubscribe stop_thread send_queue if send_current_queue end |
#start_thread ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/appsignal/agent.rb', line 29 def start_thread Appsignal.logger.debug('Starting agent thread') @thread = Thread.new do begin sleep(rand(sleep_time)) loop do send_queue if aggregator.has_transactions? Appsignal.logger.debug("Sleeping #{sleep_time}") sleep(sleep_time) end rescue Exception=>ex Appsignal.logger.error "#{ex.class} in agent thread: '#{ex.}'" Appsignal.logger.error ex.backtrace.join('\n') end end end |
#stop_thread ⇒ Object
52 53 54 55 56 57 |
# File 'lib/appsignal/agent.rb', line 52 def stop_thread if @thread && @thread.alive? Appsignal.logger.debug 'Stopping agent thread' Thread.kill(@thread) end end |
#subscribe ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/appsignal/agent.rb', line 59 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) elsif event.name.start_with?('perform_job') Appsignal::Transaction.current.set_perform_job_event(event) end Appsignal::Transaction.current.add_event(event) unless paused end end end |
#unsubscribe ⇒ Object
81 82 83 84 85 |
# File 'lib/appsignal/agent.rb', line 81 def unsubscribe Appsignal.logger.debug('Unsubscribing from notifications') ActiveSupport::Notifications.unsubscribe(@subscriber) @subscriber = nil end |