Class: Batsd::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/batsd/handler.rb

Overview

Abstract interface for handling different types of data (e.g., counters, timers, etc.).

Generally, this should be subclassed to provide the specific functionality desired. If left unmodified, it provides an echo handler when run with ENV["VVERBOSE"], and is silent otherwise.

Direct Known Subclasses

Counter, Gauge, Timer

Defined Under Namespace

Classes: Counter, Gauge, Timer

Instance Method Summary (collapse)

Constructor Details

- (Handler) initialize(options = {})

Creates a new handler object and spawns a threadpool. If options[:threadpool_size] is specified, that will be used (default 100 threads)



16
17
18
19
# File 'lib/batsd/handler.rb', line 16

def initialize(options={})
  @threadpool = Threadpool.new(options[:threadpool_size] || 100)
  @statistics = {}
end

Instance Method Details

- (Object) handle(key, value, sample_rate)

Handle the key, value, and sample rate specified in the key. Override this in individual handlers to actually do something useful



25
26
27
28
29
# File 'lib/batsd/handler.rb', line 25

def handle(key, value, sample_rate)
  @threadpool.queue do
    puts "Received #{key} #{value} #{sample_rate}" if ENV["VVERBOSE"]
  end
end

- (Object) statistics

Provide some basic statistics about the handler. The preferred way to augment these is to modify the @statistics object from subclassed handlers



41
42
43
44
45
46
# File 'lib/batsd/handler.rb', line 41

def statistics
  {
    threadpool_size: @threadpool.pool,  
    queue_depth: @threadpool.size
  }.merge(@statistics)
end

- (Object) threadpool

Exposes the threadpool used by the handler



33
34
35
# File 'lib/batsd/handler.rb', line 33

def threadpool
  @threadpool
end