class MetricExpectation

@private

Attributes

name[RW]
sample_rate[RW]
tags[RW]
times[RW]
type[RW]
value[RW]

Public Class Methods

distribution(name, value = nil, **options) click to toggle source
# 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) click to toggle source
# File lib/statsd/instrument/expectation.rb, line 14
def gauge(name, value = nil, **options)
  new(type: :g, name: name, value: value, **options)
end
histogram(name, value = nil, **options) click to toggle source
# 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) click to toggle source
# File lib/statsd/instrument/expectation.rb, line 6
def increment(name, value = nil, **options)
  new(type: :c, name: name, value: value, **options)
end
key_value(name, value = nil, **options) click to toggle source
# File lib/statsd/instrument/expectation.rb, line 22
def key_value(name, value = nil, **options)
  new(type: :kv, name: name, value: value, **options)
end
measure(name, value = nil, **options) click to toggle source
# File lib/statsd/instrument/expectation.rb, line 10
def measure(name, value = nil, **options)
  new(type: :ms, name: name, value: value, **options)
end
new(client: StatsD.singleton_client, type:, name:, value: nil, sample_rate: nil, tags: nil, no_prefix: false, times: 1) click to toggle source
# File lib/statsd/instrument/expectation.rb, line 37
def initialize(client: StatsD.singleton_client, type:, name:, value: nil,
  sample_rate: nil, tags: nil, no_prefix: false, times: 1)

  @type = type
  @name = client.prefix ? "#{client.prefix}.#{name}" : name unless no_prefix
  @value = normalized_value_for_type(type, value) if value
  @sample_rate = sample_rate
  @tags = normalize_tags(tags)
  @times = times
end
set(name, value = nil, **options) click to toggle source
# File lib/statsd/instrument/expectation.rb, line 18
def set(name, value = nil, **options)
  new(type: :s, name: name, value: value, **options)
end

Public Instance Methods

inspect() click to toggle source
# File lib/statsd/instrument/expectation.rb, line 77
def inspect
  "#<StatsD::Instrument::Expectation:\"#{self}\">"
end
matches(actual_metric) click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source
# 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

Private Instance Methods

normalize_tags(tags) click to toggle source

@private

Utility function to convert tags to the canonical form.

  • Tags specified as key value pairs will be converted into an array

  • Tags are normalized to remove unsupported characters

@param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. @return [Array<String>, nil] the list of tags in canonical form.

@todo We should delegate this to thje datagram builder of the current client,

to ensure that this logic matches the logic of the active datagram builder.
# File lib/statsd/instrument/expectation.rb, line 108
def normalize_tags(tags)
  return [] unless tags
  tags = tags.map { |k, v| "#{k}:#{v}" } if tags.is_a?(Hash)

  # Fast path when no string replacement is needed
  return tags unless tags.any? { |tag| /[|,]/.match?(tag) }
  tags.map { |tag| tag.tr('|,', '') }
end