Class: Minitest::Queue::Statsd

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/queue/statsd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr: nil, default_tags: [], namespace: nil) ⇒ Statsd

Returns a new instance of Statsd.



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#addrObject (readonly)

Returns the value of attribute addr.



10
11
12
# File 'lib/minitest/queue/statsd.rb', line 10

def addr
  @addr
end

#default_tagsObject (readonly)

Returns the value of attribute default_tags.



10
11
12
# File 'lib/minitest/queue/statsd.rb', line 10

def default_tags
  @default_tags
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/minitest/queue/statsd.rb', line 10

def namespace
  @namespace
end

Class Method Details

.measure_durationObject



40
41
42
43
44
45
46
# 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

Instance Method Details

#increment(metric, tags: [], value: 1) ⇒ Object



26
27
28
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
# 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