Class: StatsD::Instrument::Expectation

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, type:, name:, value: nil, sample_rate: nil, tags: nil, no_prefix: false, times: 1) ⇒ Expectation

Returns a new instance of Expectation.



37
38
39
40
41
42
43
44
45
46
# File 'lib/statsd/instrument/expectation.rb', line 37

def initialize(client: nil, type:, name:, value: nil,
  sample_rate: nil, tags: nil, no_prefix: false, times: 1)

  @type = type
  @name = no_prefix ? name : StatsD::Instrument::Helpers.prefix_metric(name, client: client)
  @value = normalized_value_for_type(type, value) if value
  @sample_rate = sample_rate
  @tags = normalize_tags(tags)
  @times = times
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



35
36
37
# File 'lib/statsd/instrument/expectation.rb', line 35

def name
  @name
end

#sample_rateObject

Returns the value of attribute sample_rate.



35
36
37
# File 'lib/statsd/instrument/expectation.rb', line 35

def sample_rate
  @sample_rate
end

#tagsObject

Returns the value of attribute tags.



35
36
37
# File 'lib/statsd/instrument/expectation.rb', line 35

def tags
  @tags
end

#timesObject

Returns the value of attribute times.



35
36
37
# File 'lib/statsd/instrument/expectation.rb', line 35

def times
  @times
end

#typeObject

Returns the value of attribute type.



35
36
37
# File 'lib/statsd/instrument/expectation.rb', line 35

def type
  @type
end

#valueObject

Returns the value of attribute value.



35
36
37
# File 'lib/statsd/instrument/expectation.rb', line 35

def value
  @value
end

Class Method Details

.distribution(name, value = nil, **options) ⇒ Object



26
27
28
# File 'lib/statsd/instrument/expectation.rb', line 26

def distribution(name, value = nil, **options)
  new(type: :d, name: name, value: value, **options)
end

.gauge(name, value = nil, **options) ⇒ Object



18
19
20
# File 'lib/statsd/instrument/expectation.rb', line 18

def gauge(name, value = nil, **options)
  new(type: :g, name: name, value: value, **options)
end

.histogram(name, value = nil, **options) ⇒ Object



30
31
32
# File 'lib/statsd/instrument/expectation.rb', line 30

def histogram(name, value = nil, **options)
  new(type: :h, name: name, value: value, **options)
end

.increment(name, value = nil, **options) ⇒ Object



10
11
12
# File 'lib/statsd/instrument/expectation.rb', line 10

def increment(name, value = nil, **options)
  new(type: :c, name: name, value: value, **options)
end

.measure(name, value = nil, **options) ⇒ Object



14
15
16
# File 'lib/statsd/instrument/expectation.rb', line 14

def measure(name, value = nil, **options)
  new(type: :ms, name: name, value: value, **options)
end

.set(name, value = nil, **options) ⇒ Object



22
23
24
# File 'lib/statsd/instrument/expectation.rb', line 22

def set(name, value = nil, **options)
  new(type: :s, name: name, value: value, **options)
end

Instance Method Details

#inspectObject



77
78
79
# File 'lib/statsd/instrument/expectation.rb', line 77

def inspect
  "#<StatsD::Instrument::Expectation:\"#{self}\">"
end

#matches(actual_metric) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/statsd/instrument/expectation.rb', line 57

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

  if tags
    expected_tags = Set.new(tags)
    actual_tags = Set.new(actual_metric.tags)
    return expected_tags.subset?(actual_tags)
  end
  true
end

#normalized_value_for_type(type, value) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/statsd/instrument/expectation.rb', line 48

def normalized_value_for_type(type, value)
  case type
  when :c then Integer(value)
  when :g, :h, :d, :kv, :ms then Float(value)
  when :s then String(value)
  else value
  end
end

#to_sObject



69
70
71
72
73
74
75
# File 'lib/statsd/instrument/expectation.rb', line 69

def to_s
  str = +"#{name}:#{value || "<anything>"}|#{type}"
  str << "|@#{sample_rate}" if sample_rate
  str << "|#" << tags.join(",") if tags
  str << " (expected #{times} times)" if times > 1
  str
end