Module: Hallmonitor::Monitored::ClassMethods

Defined in:
lib/hallmonitor/monitored.rb

Instance Method Summary collapse

Instance Method Details

#count_for(method_sym, metric_name: nil, tags: {}) ⇒ 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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hallmonitor/monitored.rb', line 32

def count_for(method_sym, metric_name: nil, tags: {})
  metric_name ||= "#{underscore(name)}.#{method_sym}"
  undecorated_method_sym = "#{method_sym}_without_hallmonitor_counter".to_sym
  decorated_method_sym = "#{method_sym}_with_hallmonitor_counter".to_sym
  send(:define_method, decorated_method_sym) do |*args|
    emit(metric_name, tags: tags)
    send(undecorated_method_sym, *args)
  end

  alias_method undecorated_method_sym, method_sym
  alias_method method_sym, decorated_method_sym
end

#timer_for(method_sym, metric_name: nil, tags: {}) ⇒ 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
23
24
# File 'lib/hallmonitor/monitored.rb', line 12

def timer_for(method_sym, metric_name: nil, tags: {})
  metric_name ||= "#{underscore(name)}.#{method_sym}"
  undecorated_method_sym = "#{method_sym}_without_hallmonitor_timer".to_sym
  decorated_method_sym = "#{method_sym}_with_hallmonitor_timer".to_sym
  send(:define_method, decorated_method_sym) do |*args|
    watch(metric_name, tags: tags) do
      send(undecorated_method_sym, *args)
    end
  end

  alias_method undecorated_method_sym, method_sym
  alias_method method_sym, decorated_method_sym
end

#underscore(value) ⇒ Object

:reek:UtilityFunction



46
47
48
49
50
51
52
53
54
# File 'lib/hallmonitor/monitored.rb', line 46

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