Class: Appsignal::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/agent.rb

Constant Summary collapse

ACTION =
'log_entries'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

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

#activeObject (readonly)

Returns the value of attribute active.



5
6
7
# File 'lib/appsignal/agent.rb', line 5

def active
  @active
end

#aggregatorObject (readonly)

Returns the value of attribute aggregator.



5
6
7
# File 'lib/appsignal/agent.rb', line 5

def aggregator
  @aggregator
end

#aggregator_semaphoreObject (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_timeObject (readonly)

Returns the value of attribute sleep_time.



5
6
7
# File 'lib/appsignal/agent.rb', line 5

def sleep_time
  @sleep_time
end

#threadObject (readonly)

Returns the value of attribute thread.



5
6
7
# File 'lib/appsignal/agent.rb', line 5

def thread
  @thread
end

#transmitterObject (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

Returns:

  • (Boolean)


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

def forked?
  @forked ||= false
end

#send_queueObject



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.message}"
    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