Class: Statsd::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(opts={})
  @host = opts[:host] || 'localhost'
  @port = opts[:port] || 8125
  @prefix = opts[:prefix]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Instance Method Details

#decrement(stats, sample_rate = 1) ⇒ Object

stats can be a string or an array of strings



64
65
66
# File 'lib/statsd.rb', line 64

def decrement(stats, sample_rate = 1)
  update_counter stats, -1, sample_rate
end

#gauge(stats) ⇒ Object

stats is a hash



76
77
78
79
80
81
82
83
# File 'lib/statsd.rb', line 76

def gauge(stats)
  send_stats(stats.map { |s,val|
               if @prefix
                 s = "#{@prefix}.#{s}"
               end
               "#{s}:#{val}|g"
             })
end

#host_ip_addrObject



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

def host_ip_addr
  @host_ip_addr ||= Resolv.getaddress(host)
end

#increment(stats, sample_rate = 1) ⇒ Object

stats can be a string or an array of strings



59
60
61
# File 'lib/statsd.rb', line 59

def increment(stats, sample_rate = 1)
  update_counter stats, 1, sample_rate
end

#timing(stat, time = nil, sample_rate = 1) ⇒ Object

stat to log timing for time is the time to log in ms



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/statsd.rb', line 42

def timing(stat, time = nil, sample_rate = 1)
  value = nil
  if block_given?
    start_time = Time.now.to_f
    value = yield
    time = ((Time.now.to_f - start_time) * 1000).floor
  end

  if @prefix
    stat = "#{@prefix}.#{stat}"
  end

  send_stats("#{stat}:#{time}|ms", sample_rate)
  value
end

#update_counter(stats, delta = 1, sample_rate = 1) ⇒ Object

stats can be a string or array of strings



69
70
71
72
73
# File 'lib/statsd.rb', line 69

def update_counter(stats, delta = 1, sample_rate = 1)
  stats = Array(stats)
  p = @prefix ? "#{@prefix}." : '' # apply prefix to each
  send_stats(stats.map { |s| "#{p}#{s}:#{delta}|c" }, sample_rate)
end