Module: Hallmonitor::Monitored::ClassMethods

Defined in:
lib/hallmonitor/monitored.rb

Instance Method Summary collapse

Instance Method Details

#count_for(method_sym, options = {}) ⇒ Object

Sets up a counter for a method by symbol. Method must have already been defined (ie. put this after the method definition)

Parameters:

  • method_sym (Symbol)

    method name as a symbol



30
31
32
33
34
35
36
37
38
39
# File 'lib/hallmonitor/monitored.rb', line 30

def count_for(method_sym, options = {})
  metric_name = options[:metric_name] || "#{underscore(name)}.#{method_sym}"
  send(:define_method, "#{method_sym}_with_hallmonitor_counter") do |*args|
    emit(metric_name)
    send("#{method_sym}_without_hallmonitor_counter".to_sym, *args)
  end

  alias_method "#{method_sym}_without_hallmonitor_counter".to_sym, method_sym
  alias_method method_sym, "#{method_sym}_with_hallmonitor_counter".to_sym
end

#timer_for(method_sym, options = {}) ⇒ Object

Sets up a timer for a method by symbol. Method must have already been defined (ie. put this after the method definition)

Parameters:

  • method_sym (Symbol)

    method name as a symbol



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hallmonitor/monitored.rb', line 12

def timer_for(method_sym, options = {})
  metric_name = options[:metric_name] || "#{underscore(name)}.#{method_sym}"
  send(:define_method, "#{method_sym}_with_hallmonitor_timer") do |*args|
    watch(metric_name) do
      send("#{method_sym}_without_hallmonitor_timer".to_sym, *args)
    end
  end

  alias_method "#{method_sym}_without_hallmonitor_timer".to_sym, method_sym
  alias_method method_sym, "#{method_sym}_with_hallmonitor_timer".to_sym
end

#underscore(value) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/hallmonitor/monitored.rb', line 41

def underscore(value)
  word = value.dup
  word.gsub!(/::/, '.')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', ' ')
  word.downcase!
  word
end