lib/fiveruns/dash/metric.rb in fiveruns-dash-ruby-0.7.6 vs lib/fiveruns/dash/metric.rb in fiveruns-dash-ruby-0.8.0

- old
+ new

@@ -8,12 +8,12 @@ attr_reader :name, :description, :help_text, :options attr_accessor :recipe def initialize(name, *args, &block) @@warned = false @name = name.to_s - @options = args.extract_options! - @description = args.shift || @name.titleize + @options = args.last.is_a?(Hash) ? args.pop : {} + @description = args.shift || Util.titleize(@name) @help_text = args.shift @operation = block @virtual = !!options[:sources] @abstract = options[:abstract] validate! @@ -100,21 +100,24 @@ ####### private ####### def validate! - raise ArgumentError, "#{name} - Virtual metrics should have source metrics" if virtual? && options[:sources].blank? + raise ArgumentError, "'#{name}' should be between 3 and 32 characters" unless (3..32).include?(name.size) + raise ArgumentError, "'#{name}' should only contain letters, numbers and underscore" if name !~ /\A\w+\Z/ + + raise ArgumentError, "#{name} - Virtual metrics should have source metrics" if virtual? && Util.blank?(options[:sources]) raise ArgumentError, "#{name} - metrics should not have source metrics" if !virtual? && options[:sources] end def optional_info - returning({}) do |optional| - copy = optional.merge(@options[:unit] ? {:unit => @options[:unit].to_s} : {}) - copy = copy.merge(@options[:scope] ? {:scope => @options[:scope].to_s} : {}) - copy = copy.merge(abstract? ? {:abstract => true} : {}) - optional.merge!(copy) - end + optional = {} + copy = optional.merge(@options[:unit] ? {:unit => @options[:unit].to_s} : {}) + copy = copy.merge(@options[:scope] ? {:scope => @options[:scope].to_s} : {}) + copy = copy.merge(abstract? ? {:abstract => true} : {}) + optional.merge!(copy) + optional end def combine(source_values) # Get the intersection of contexts for all the source metrics. # We combine the values for all shared contexts. @@ -255,13 +258,13 @@ def blank_data Hash.new {{ :invocations => 0, :value => 0 }} end def value_hash - returning(:values => current_value) do - reset - end + values = {:values => current_value} + reset + values end def install_hook @operation ||= lambda { nil } methods_to_instrument.each do |meth| @@ -274,26 +277,30 @@ end end end def instrument_options - returning({}) do |options| - options[:reentrant_token] = self.object_id.abs if @options[:reentrant] - options[:only_within] = @options[:only_within] if @options[:only_within] - options[:mark_as] = @name if @options[:mark] - end + options = {} + options[:reentrant_token] = self.object_id.abs if @options[:reentrant] + options[:only_within] = @options[:only_within] if @options[:only_within] + options[:mark_as] = @name if @options[:mark] + options end def methods_to_instrument - @methods_to_instrument ||= Array(@options[:method]) + Array(@options[:methods]) + @methods_to_instrument ||= begin + Array(@options[:method]) + Array(@options[:methods]) + end end def validate! super - raise ArgumentError, "Can not set :unit for `#{@name}' time metric" if @options[:unit] - if methods_to_instrument.blank? - raise ArgumentError, "Must set :method or :methods option for `#{@name}` time metric" + raise ArgumentError, + "Can not set :unit for `#{@name}' time metric" if @options[:unit] + if Util.blank?(methods_to_instrument) + raise ArgumentError, + "Must set :method or :methods option for `#{@name}` time metric" end end # Get the current value # * Note: We sync here (and wherever @data is being written) @@ -317,20 +324,21 @@ end end def value_hash if incrementing_methods.any? - returning(:values => current_value) do - reset - end + values = {:values => current_value} + reset + values else super end end def install_hook - if incrementing_methods.blank? - raise RuntimeError, "Bad configuration for `#{@name}` counter metric" + if Util.blank?(incrementing_methods) + raise RuntimeError, + "Bad configuration for `#{@name}` counter metric" end @operation ||= lambda { nil } incrementing_methods.each do |meth| Instrument.add meth do |obj, time, *args| find_containers(obj, *args) do |container|