Class: StatsD::Instrument::MetricExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/instrument/metric_expectation.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MetricExpectation

Returns a new instance of MetricExpectation.



7
8
9
10
11
12
13
14
15
16
# File 'lib/statsd/instrument/metric_expectation.rb', line 7

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

Instance Attribute Details

#ignore_tagsObject (readonly)

Returns the value of attribute ignore_tags.



5
6
7
# File 'lib/statsd/instrument/metric_expectation.rb', line 5

def ignore_tags
  @ignore_tags
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/statsd/instrument/metric_expectation.rb', line 4

def name
  @name
end

#sample_rateObject

Returns the value of attribute sample_rate.



4
5
6
# File 'lib/statsd/instrument/metric_expectation.rb', line 4

def sample_rate
  @sample_rate
end

#tagsObject

Returns the value of attribute tags.



4
5
6
# File 'lib/statsd/instrument/metric_expectation.rb', line 4

def tags
  @tags
end

#timesObject

Returns the value of attribute times.



4
5
6
# File 'lib/statsd/instrument/metric_expectation.rb', line 4

def times
  @times
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/statsd/instrument/metric_expectation.rb', line 4

def type
  @type
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/statsd/instrument/metric_expectation.rb', line 4

def value
  @value
end

Instance Method Details

#default_valueObject



41
42
43
44
45
# File 'lib/statsd/instrument/metric_expectation.rb', line 41

def default_value
  case type
    when :c; 1
  end
end

#inspectObject



64
65
66
# File 'lib/statsd/instrument/metric_expectation.rb', line 64

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

#matches(actual_metric) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/statsd/instrument/metric_expectation.rb', line 18

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

#to_sObject



56
57
58
59
60
61
62
# File 'lib/statsd/instrument/metric_expectation.rb', line 56

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