Class: StatsD::Client
- Inherits:
-
Object
- Object
- StatsD::Client
- Defined in:
- lib/statsd/client.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #decr(key, sample_rate = 1) ⇒ Object
- #decrby(key, decrement, sample_rate = 1) ⇒ Object
- #incr(key, sample_rate = 1) ⇒ Object
- #incrby(key, increment, sample_rate = 1) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #send_data(data, sample_rate = 1) ⇒ Object
- #time(key, sample_rate = 1, &block) ⇒ Object
- #timing(key, ms, sample_rate = 1) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
8 9 10 11 |
# File 'lib/statsd/client.rb', line 8 def initialize( = {}) @hostname = [:hostname] || "localhost" @port = ([:port] || 8125).to_i end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
6 7 8 |
# File 'lib/statsd/client.rb', line 6 def hostname @hostname end |
#port ⇒ Object (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 |