lib/statsd/instrument/metric_expectation.rb in statsd-instrument-2.3.5 vs lib/statsd/instrument/metric_expectation.rb in statsd-instrument-2.4.0

- old
+ new

@@ -1,17 +1,33 @@ +# frozen_string_literal: true + # @private class StatsD::Instrument::MetricExpectation - attr_accessor :times, :type, :name, :value, :sample_rate, :tags attr_reader :ignore_tags def initialize(options = {}) - @type = options[:type] or raise ArgumentError, "Metric :type is required." - @name = options[:name] or raise ArgumentError, "Metric :name is required." + if options[:type] + @type = options[:type] + else + raise ArgumentError, "Metric :type is required." + end + + if options[:name] + @name = options[:name] + else + raise ArgumentError, "Metric :name is required." + end + + if options[:times] + @times = options[:times] + else + raise ArgumentError, "Metric :times is required." + end + @name = StatsD.prefix ? "#{StatsD.prefix}.#{@name}" : @name unless options[:no_prefix] @tags = StatsD::Instrument::Metric.normalize_tags(options[:tags]) - @times = options[:times] or raise ArgumentError, "Metric :times is required." @sample_rate = options[:sample_rate] @value = options[:value] @ignore_tags = StatsD::Instrument::Metric.normalize_tags(options[:ignore_tags]) end @@ -27,42 +43,40 @@ if ignore_tags ignored_tags = Set.new(ignore_tags) - expected_tags actual_tags -= ignored_tags if ignore_tags.is_a?(Array) - actual_tags.delete_if{ |key| ignore_tags.include?(key.split(":").first) } + actual_tags.delete_if { |key| ignore_tags.include?(key.split(":").first) } end end return expected_tags.subset?(actual_tags) end true end def default_value - case type - when :c; 1 - end + 1 if type == :c end TYPES = { - c: 'increment', - ms: 'measure', - g: 'gauge', - h: 'histogram', - d: 'distribution', - kv: 'key/value', - s: 'set', + c: 'increment', + ms: 'measure', + g: 'gauge', + h: 'histogram', + d: 'distribution', + kv: 'key/value', + s: 'set', } def to_s - str = "#{TYPES[type]} #{name}:#{value}" + str = +"#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 - str << " " << tags.map { |t| "##{t}"}.join(' ') if tags + str << " " << tags.map { |t| "##{t}" }.join(' ') if tags str << " times:#{times}" if times > 1 str end def inspect - "#<StatsD::Instrument::MetricExpectation #{self.to_s}>" + "#<StatsD::Instrument::MetricExpectation #{self}>" end end