Module: StatsD::Instrument

Defined in:
lib/statsd/instrument.rb

Instance Method Summary collapse

Instance Method Details

#statsd_count(method, name) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/statsd/instrument.rb', line 73

def statsd_count(method, name)
  add_to_method(method, name, :count) do |old_method, new_method, metric_name|
    define_method(new_method) do |*args|
      StatsD.increment(metric_name)
      send(old_method, *args)
    end
  end
end

#statsd_count_if(method, name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/statsd/instrument.rb', line 55

def statsd_count_if(method, name)
  add_to_method(method, name, :count_if) do |old_method, new_method, metric_name|
    define_method(new_method) do |*args|
      begin
        truthiness = result = send(old_method, *args)
      rescue
        truthiness = false
        raise
      else
        truthiness = (yield(result) rescue false) if block_given?
        result
      ensure
        StatsD.increment(metric_name) if truthiness
      end
    end
  end
end

#statsd_count_success(method, name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/statsd/instrument.rb', line 37

def statsd_count_success(method, name)
  add_to_method(method, name, :count_success) do |old_method, new_method, metric_name|
    define_method(new_method) do |*args|
      begin
        truthiness = result = send(old_method, *args)
      rescue
        truthiness = false
        raise
      else
        truthiness = (yield(result) rescue false) if block_given?
        result
      ensure
        StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'))
      end
    end
  end
end

#statsd_measure(method, name) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/statsd/instrument.rb', line 29

def statsd_measure(method, name)
  add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
    define_method(new_method) do |*args|
      StatsD.measure(metric_name) { send(old_method, *args) }
    end
  end
end