Class: Instrumental::Agent

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/instrumental/agent.rb

Constant Summary collapse

@@queue =
{ :count => {}, :measure => {} }
@@queue_mutex =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#milestone(name = Time.now.utc.to_s) ⇒ Object



28
29
30
# File 'lib/instrumental/agent.rb', line 28

def milestone(name = Time.now.utc.to_s)
  post_response("#{ config.path }/milestone", { :name => name })
end

#report(type, name, value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/instrumental/agent.rb', line 12

def report(type, name, value)
  # TODO: Print this to log?
  return nil unless config && config.enabled?

  type = type.to_sym
  @@queue_mutex.synchronize do
    if type == :count
      @@queue[:count][name] ||= 0
      @@queue[:count][name] += value
    else
      @@queue[:measure][name] ||= []
      @@queue[:measure][name] << value
    end
  end
end

#setup_and_runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/instrumental/agent.rb', line 32

def setup_and_run
  @intervalometer = Intervalometer.new(config.report_interval)

  config.logger.debug("Starting agent. Reporting every #{config.report_interval} seconds")
  @thread = Thread.new do
    begin
      @intervalometer.run do
        config.logger.debug('Intervalometer run')
        new_queue_items = {}
        @@queue_mutex.synchronize do
          new_queue_items = @@queue.dup
          @@queue[:count] = {}
          @@queue[:measure] = {}
          config.logger.debug("Queue contains #{new_queue_items.inspect}")
        end

        new_queue_items[:count].each do |name, value|
          send_report(:count, name, value)
        end

        new_queue_items[:measure].each do |name, values|
          send_report(:measure, name, values.join(','))
        end
      end
    rescue => e
      config.logger.error e
      config.logger.error e.backtrace.join("\n")
    end
  end
end

#stopObject



63
64
65
66
67
# File 'lib/instrumental/agent.rb', line 63

def stop
  # If the app crashes, Passenger calls halt as it winds-down the process.
  # Only call halt if intervalometer exists.
  @intervalometer && @intervalometer.halt
end