Module: StatsD::Instrument

Defined in:
lib/statsd/instrument.rb,
lib/statsd/instrument/version.rb

Defined Under Namespace

Modules: Assertions, Backends, Environment Classes: Backend, Metric, Railtie

Constant Summary collapse

VERSION =
"2.0.4"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.durationObject



12
13
14
15
16
# File 'lib/statsd/instrument.rb', line 12

def self.duration
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield
  Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
end

.generate_metric_name(metric_name, callee, *args) ⇒ Object



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

def self.generate_metric_name(metric_name, callee, *args)
  metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.')
end

Instance Method Details

#statsd_count(method, name, *metric_options) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/statsd/instrument.rb', line 70

def statsd_count(method, name, *metric_options)
  add_to_method(method, name, :count) do |old_method, new_method, metric_name|
    define_method(new_method) do |*args, &block|
      StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, *metric_options)
      send(old_method, *args, &block)
    end
  end
end

#statsd_count_if(method, name, *metric_options) ⇒ Object



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

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

#statsd_count_success(method, name, *metric_options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/statsd/instrument.rb', line 33

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

#statsd_measure(method, name, *metric_options) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/statsd/instrument.rb', line 25

def statsd_measure(method, name, *metric_options)
  add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
    define_method(new_method) do |*args, &block|
      StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, *metric_options) { send(old_method, *args, &block) }
    end
  end
end

#statsd_remove_count(method, name) ⇒ Object



79
80
81
# File 'lib/statsd/instrument.rb', line 79

def statsd_remove_count(method, name)
  remove_from_method(method, name, :count)
end

#statsd_remove_count_if(method, name) ⇒ Object



83
84
85
# File 'lib/statsd/instrument.rb', line 83

def statsd_remove_count_if(method, name)
  remove_from_method(method, name, :count_if)
end

#statsd_remove_count_success(method, name) ⇒ Object



87
88
89
# File 'lib/statsd/instrument.rb', line 87

def statsd_remove_count_success(method, name)
  remove_from_method(method, name, :count_success)
end

#statsd_remove_measure(method, name) ⇒ Object



91
92
93
# File 'lib/statsd/instrument.rb', line 91

def statsd_remove_measure(method, name)
  remove_from_method(method, name, :measure)
end