lib/hallmonitor/monitored.rb in hallmonitor-0.2.0 vs lib/hallmonitor/monitored.rb in hallmonitor-0.3.0
- old
+ new
@@ -1,39 +1,39 @@
##
# Include this module in classes where you want monitoring capabilities
module Hallmonitor
module Monitored
module ClassMethods
- def timer_for(method_sym, options={})
- metric_name = options[:metric_name] || "#{self.underscore(self.name)}.#{method_sym.to_s}.time"
- self.send(:define_method, "#{method_sym.to_s}_with_hallmonitor_timer") do |*args|
- watch(metric_name) do
- self.send("#{method_sym.to_s}_without_hallmonitor_timer".to_sym, *args)
+ 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.to_s}_without_hallmonitor_timer".to_sym, method_sym
- alias_method method_sym, "#{method_sym.to_s}_with_hallmonitor_timer".to_sym
+ alias_method "#{method_sym}_without_hallmonitor_timer".to_sym, method_sym
+ alias_method method_sym, "#{method_sym}_with_hallmonitor_timer".to_sym
end
- def count_for(method_sym, options={})
- metric_name = options[:metric_name] || "#{self.underscore(self.name)}.#{method_sym.to_s}.count"
- self.send(:define_method, "#{method_sym.to_s}_with_hallmonitor_counter") do |*args|
+ 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)
- self.send("#{method_sym.to_s}_without_hallmonitor_counter".to_sym, *args)
+ send("#{method_sym}_without_hallmonitor_counter".to_sym, *args)
end
- alias_method "#{method_sym.to_s}_without_hallmonitor_counter".to_sym, method_sym
- alias_method method_sym, "#{method_sym.to_s}_with_hallmonitor_counter".to_sym
+ alias_method "#{method_sym}_without_hallmonitor_counter".to_sym, method_sym
+ alias_method method_sym, "#{method_sym}_with_hallmonitor_counter".to_sym
end
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.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
end
@@ -46,17 +46,15 @@
# a base event from the passed in param
# If the parameter is a #Hallmonitor::Event, it will be emitted as is.
# Otherwise, a new Hallmonitor::Event will be created with the parameter and emitted.
def emit(event = nil)
to_emit = self;
- if(!event.nil?)
+ unless event.nil?
to_emit = event.kind_of?(Hallmonitor::Event) ? event : Hallmonitor::Event.new(event)
end
# If we were given a block, then we want to execute that
- if block_given?
- yield(to_emit)
- end
+ yield(to_emit) if block_given?
Outputter.output(to_emit)
end
##