lib/spectator/meter_id.rb in netflix-spectator-rb-0.2.2 vs lib/spectator/meter_id.rb in netflix-spectator-rb-0.3.0
- old
+ new
@@ -1,9 +1,10 @@
module Spectator
# Identifier for a meter or Measure
class MeterId
attr_reader :name, :tags
+
def initialize(name, maybe_tags = nil)
tags = maybe_tags.nil? ? {} : maybe_tags
@name = name.to_sym
@tags = {}
tags.each { |k, v| @tags[k.to_sym] = v.to_sym }
@@ -16,12 +17,32 @@
new_tags = @tags.dup
new_tags[key] = value
MeterId.new(@name, new_tags)
end
+ # Create a new MeterId adding the given tags
+ def with_tags(additional_tags)
+ new_tags = @tags.dup
+ additional_tags.each do |k, v|
+ new_tags[k] = v
+ end
+ MeterId.new(@name, new_tags)
+ end
+
# Create a new MeterId with key=statistic and the given value
def with_stat(stat_value)
with_tag(:statistic, stat_value)
+ end
+
+ # Get a MeterId with a statistic tag. If the current MeterId
+ # already includes statistic then just return it, otherwise create
+ # a new one
+ def with_default_stat(stat_value)
+ if tags.key?(:statistic)
+ self
+ else
+ with_tag(:statistic, stat_value)
+ end
end
# lazyily compute a key to be used in hashes for efficiency
def key
@key ||= begin