Class: StatsD::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/statsd/client.rb', line 8

def initialize(options = {})
  @hostname = options[:hostname] || "localhost"
  @port = (options[:port] || 8125).to_i
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



6
7
8
# File 'lib/statsd/client.rb', line 6

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/statsd/client.rb', line 6

def port
  @port
end

Instance Method Details

#decr(key, sample_rate = 1) ⇒ Object



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

def decr(key, sample_rate = 1)
  decrby(key, 1, sample_rate)
end

#decrby(key, decrement, sample_rate = 1) ⇒ Object



34
35
36
# File 'lib/statsd/client.rb', line 34

def decrby(key, decrement, sample_rate = 1)
  incrby(key, -decrement, sample_rate)
end

#incr(key, sample_rate = 1) ⇒ Object



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

def incr(key, sample_rate = 1)
  incrby(key, 1, sample_rate)
end

#incrby(key, increment, sample_rate = 1) ⇒ Object



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

def incrby(key, increment, sample_rate = 1)
  send_data({ key => "#{increment}|c" }, sample_rate)
end

#send_data(data, sample_rate = 1) ⇒ Object



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

def send_data(data, sample_rate = 1)
  socket = UDPSocket.new
  socket.bind("127.0.0.1", 0)
  socket.connect(@hostname, @port)
  data.each do |key, val|
    val += "|@#{sample_rate}" if sample_rate != 1
    socket.send "#{key}:#{val}", 0
  end
end

#time(key, sample_rate = 1, &block) ⇒ Object



13
14
15
16
# File 'lib/statsd/client.rb', line 13

def time(key, sample_rate = 1, &block)
  seconds = Benchmark.realtime { block.call }
  timing(key, (seconds * 1000).round, sample_rate)
end

#timing(key, ms, sample_rate = 1) ⇒ Object



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

def timing(key, ms, sample_rate = 1)
  send_data({ key => "#{ms}|ms" }, sample_rate)
end