Class: Statsd::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statsd_host, batch_size, pool_size) ⇒ Reporter

Returns a new instance of Reporter.



9
10
11
12
13
14
15
16
# File 'lib/statsd/reporter.rb', line 9

def initialize(statsd_host, batch_size, pool_size)
  @pool_size   = pool_size || 1
  @statsd_host = statsd_host
  @messages    = []
  @queue       = Queue.new
  @batch_size  = batch_size
  @pool_size.times { |i| Thread.new { Thread.current[:id] = i; spawn_thread_pool } }
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



7
8
9
# File 'lib/statsd/reporter.rb', line 7

def batch_size
  @batch_size
end

#messagesObject

Returns the value of attribute messages.



6
7
8
# File 'lib/statsd/reporter.rb', line 6

def messages
  @messages
end

#pool_sizeObject (readonly)

Returns the value of attribute pool_size.



7
8
9
# File 'lib/statsd/reporter.rb', line 7

def pool_size
  @pool_size
end

#queueObject

Returns the value of attribute queue.



6
7
8
# File 'lib/statsd/reporter.rb', line 6

def queue
  @queue
end

#statsd_hostObject (readonly)

Returns the value of attribute statsd_host.



7
8
9
# File 'lib/statsd/reporter.rb', line 7

def statsd_host
  @statsd_host
end

Instance Method Details

#enqueue(metric) ⇒ Object



32
33
34
# File 'lib/statsd/reporter.rb', line 32

def enqueue(metric)
  queue << metric
end

#spawn_thread_poolObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/statsd/reporter.rb', line 18

def spawn_thread_pool
  loop do
    if queue.size >= batch_size
      begin
        while messages << queue.pop(true)
          flush
        end
      rescue ThreadError
        flush #flush pending queue messages
      end
    end
  end
end