Sha256: 0e610f7ecae499eee7da9a06ced236d276e0b876c802c3bb8dbedc0f45571bc1

Contents?: true

Size: 1.84 KB

Versions: 11

Compression:

Stored size: 1.84 KB

Contents

# @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."
    @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

  def matches(actual_metric)
    return false if sample_rate && sample_rate != actual_metric.sample_rate
    return false if value && value != actual_metric.value

    if tags

      expected_tags = Set.new(tags)
      actual_tags = Set.new(actual_metric.tags)

      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) }
        end
      end

      return expected_tags.subset?(actual_tags)
    end
    true
  end

  def default_value
    case type
      when :c; 1
    end
  end

  TYPES = {
      c:  'increment',
      ms: 'measure',
      g:  'gauge',
      h:  'histogram',
      d:  'distribution',
      kv: 'key/value',
      s:  'set',
  }

  def to_s
    str = "#{TYPES[type]} #{name}:#{value}"
    str << " @#{sample_rate}" if sample_rate != 1.0
    str << " " << tags.map { |t| "##{t}"}.join(' ') if tags
    str << " times:#{times}" if times > 1
    str
  end

  def inspect
    "#<StatsD::Instrument::MetricExpectation #{self.to_s}>"
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
statsd-instrument-2.3.5 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.4 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.2 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.1 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0.beta6 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0.beta5 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0.beta4 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0.beta3 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0.beta2 lib/statsd/instrument/metric_expectation.rb
statsd-instrument-2.3.0.beta lib/statsd/instrument/metric_expectation.rb