Class: Appsignal::Agent

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

Constant Summary collapse

ACTION =
'log_entries'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/appsignal/agent.rb', line 6

def initialize
  return unless Appsignal.active?
  @sleep_time = 60.0
  @slowest_transactions = {}
  @queue = []
  @retry_request = true
  @thread = Thread.new do
    while true do
      send_queue if @queue.any?
      sleep @sleep_time
    end
  end
  @transmitter = Transmitter.new(
    Appsignal.config[:endpoint],
    ACTION,
    Appsignal.config[:api_key]
  )
  Appsignal.logger.info 'Started the Appsignal agent'
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



3
4
5
# File 'lib/appsignal/agent.rb', line 3

def active
  @active
end

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/appsignal/agent.rb', line 3

def queue
  @queue
end

#sleep_timeObject (readonly)

Returns the value of attribute sleep_time.



3
4
5
# File 'lib/appsignal/agent.rb', line 3

def sleep_time
  @sleep_time
end

#slowest_transactionsObject (readonly)

Returns the value of attribute slowest_transactions.



3
4
5
# File 'lib/appsignal/agent.rb', line 3

def slowest_transactions
  @slowest_transactions
end

#transmitterObject (readonly)

Returns the value of attribute transmitter.



3
4
5
# File 'lib/appsignal/agent.rb', line 3

def transmitter
  @transmitter
end

Instance Method Details

#add_to_queue(transaction) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/appsignal/agent.rb', line 26

def add_to_queue(transaction)
  if !transaction.exception? && transaction.action
    current_slowest_transaction = @slowest_transactions[transaction.action]
    if current_slowest_transaction
      if current_slowest_transaction.process_action_event.duration <
         transaction.process_action_event.duration
        current_slowest_transaction.clear_payload_and_events!
        @slowest_transactions[transaction.action] = transaction
      else
        transaction.clear_payload_and_events!
      end
    else
      @slowest_transactions[transaction.action] = transaction
    end
  end
  @queue << transaction
end

#handle_result(code) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/appsignal/agent.rb', line 54

def handle_result(code)
  Appsignal.logger.debug "Queue sent, response code: #{code}"
  case code.to_i
  when 200
    good_response
  when 420 # Enhance Your Calm
    good_response
    @sleep_time = @sleep_time * 1.5
  when 413 # Request Entity Too Large
    good_response
    @sleep_time = @sleep_time / 1.5
  when 429
    Appsignal.logger.error "Too many requests sent, disengaging the agent"
    stop_logging
  when 406
    Appsignal.logger.error "Your appsignal gem cannot communicate with the API anymore, please upgrade. Disengaging the agent"
    stop_logging
  when 402
    Appsignal.logger.error "Payment required, disengaging the agent"
    stop_logging
  when 401
    Appsignal.logger.error "API token cannot be authorized, disengaging the agent"
    stop_logging
  else
    retry_once
  end
end

#send_queueObject



44
45
46
47
48
49
50
51
52
# File 'lib/appsignal/agent.rb', line 44

def send_queue
  Appsignal.logger.debug "Sending queue"
  begin
    handle_result transmitter.transmit(queue.map(&:to_hash))
  rescue Exception => ex
    Appsignal.logger.error "Exception while communicating with AppSignal: #{ex}"
    handle_result nil
  end
end