Module: Resque::Plugins::Statsd

Defined in:
lib/resque/plugins/statsd.rb

Constant Summary collapse

DEFAULT_TASKS =
{
  :hostname => Proc.new{ @stat_hostname ||= `hostname`.strip},
  :classname => Proc.new {self},
  :queuename => Proc.new {|args| @queue}
}

Instance Method Summary collapse

Instance Method Details

#after_dequeue_stats(*args) ⇒ Object



42
43
44
45
46
# File 'lib/resque/plugins/statsd.rb', line 42

def after_dequeue_stats(*args)
  statsd.increment("total__resque_dequeues")
  statsd.increment("total__resque_dequeues:#{self}")
  run_hooks(:dequeue, :total_dequeues, args) {|key| statsd.increment(key)}
end

#after_enqueue_stats(*args) ⇒ Object



36
37
38
39
40
# File 'lib/resque/plugins/statsd.rb', line 36

def after_enqueue_stats(*args)
  statsd.increment("total_resque_enqueues")
  statsd.increment("total_resque_enqueues:#{self}")
  run_hooks(:enqueue, :total_enqueues, args){|key| statsd.increment(key)}
end

#around_perform_stats(*args) ⇒ Object

Stuff to hook up to. Probably need to use batch.



20
21
22
23
24
25
26
27
28
# File 'lib/resque/plugins/statsd.rb', line 20

def around_perform_stats(*args)
  start = Time.now
  yield
  time_taken = Time.now - start
  statsd.timing("duration:#{self}", time_taken)
  statsd.increment("total_successful:#{self}")
  statsd.increment("total_successful")
  run_hooks(:duration, :duration, args) {|key| statsd.timing(key, time_taken)}
end

#extra_stats_keyObject



48
49
50
# File 'lib/resque/plugins/statsd.rb', line 48

def extra_stats_key
  @extra_stats_key ||= {}
end

#on_failure_stats(*args) ⇒ Object



30
31
32
33
34
# File 'lib/resque/plugins/statsd.rb', line 30

def on_failure_stats(*args)
  statsd.increment("total_resque_failures")
  statsd.increment("total_resque_failures:#{self}")
  run_hooks(:failure, :total_resque_failures, args){|key| statsd.increment(key)}
end

#run_hooks(type, key, args = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/resque/plugins/statsd.rb', line 52

def run_hooks(type, key, args = nil)
  Array(extra_stats_key[type]).each do |item|
    begin
      res = "#{key}:#{Resque::Plugins::Statsd::DEFAULT_TASKS[item].call(args)}"
      puts "**********************************************"
      puts res
      puts "**********************************************"
    rescue
      puts "#{$!}" # Don't throw up if bad stuff happened, like the proc not being there.
    end
    yield(res)  if res
  end
end

#statsdObject



10
11
12
# File 'lib/resque/plugins/statsd.rb', line 10

def statsd
  $statsd || @statsd_server
end

#statsd=(statsd) ⇒ Object



14
15
16
# File 'lib/resque/plugins/statsd.rb', line 14

def statsd=(statsd)
  @statsd_server = statsd
end