class Minitest::Queue::Statsd

Attributes

addr[R]
default_tags[R]
namespace[R]

Public Class Methods

measure_duration() { || ... } click to toggle source
# File lib/minitest/queue/statsd.rb, line 40
def self.measure_duration
  before = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
  return_value = yield
  after = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)

  [return_value, after - before]
end
new(addr: nil, default_tags: [], namespace: nil) click to toggle source
# File lib/minitest/queue/statsd.rb, line 12
def initialize(addr: nil, default_tags: [], namespace: nil)
  @default_tags = default_tags
  @namespace = namespace
  @addr = addr

  if addr
    host, port = addr.split(':', 2)
    @socket = UDPSocket.new
    @socket.connect(host, Integer(port))
  end
rescue SocketError
  # No-op, we shouldn't fail CI because of statsd
end

Public Instance Methods

increment(metric, tags: [], value: 1) click to toggle source
# File lib/minitest/queue/statsd.rb, line 26
def increment(metric, tags: [], value: 1)
  send_metric(type: 'c', value: value, metric: metric, tags: default_tags + tags)
end
measure(metric, duration = nil, tags: [], &block) click to toggle source
# File lib/minitest/queue/statsd.rb, line 30
def measure(metric, duration = nil, tags: [], &block)
  if block_given?
    return_value, duration = Minitest::Queue::Statsd.measure_duration(&block)
  elsif duration.nil?
    raise ArgumentError, "You need to pass a block or pass a float as second argument."
  end
  send_metric(type: 'ms', value: duration, metric: metric, tags: default_tags + tags)
  return_value
end

Private Instance Methods

send_metric(type:, metric:, value:, tags:) click to toggle source
# File lib/minitest/queue/statsd.rb, line 50
def send_metric(type:, metric:, value:, tags:)
  return unless @socket 
  metric_snippet = namespace.nil? ? metric : "#{namespace}.#{metric}"
  tags_snippet = tags.empty? ? '' : "|##{tags.join(',')}"
  payload = "#{metric_snippet}:#{value}|#{type}#{tags_snippet}"
  @socket.send(payload, 0)
rescue SystemCallError
  # No-op, we shouldn't fail or spam output due to statsd issues
end