class Statsd::Client
Constants
- VERSION
Attributes
host[R]
port[R]
tcp[R]
Public Class Methods
new(host = 'localhost', port = 8125, tcp = false)
click to toggle source
Initializes a Statsd
client.
@param [String] host @param [Integer] port @param [Boolean] tcp
# File lib/statsd/client.rb, line 90 def initialize(host = 'localhost', port = 8125, tcp = false) @host, @port, @tcp = host, port, tcp end
Public Instance Methods
decrement(stats, sample_rate = 1)
click to toggle source
Decrements a counter
@param [Array, String] stats name of statistic(s) being updated @param [Integer, Float] sample_rate
# File lib/statsd/client.rb, line 116 def decrement(stats, sample_rate = 1) update_stats(stats, -1, sample_rate) end
increment(stats, sample_rate = 1)
click to toggle source
Increments a counter
@param [Array, String] stats name of statistic(s) being updated @param [Integer, Float] sample_rate
# File lib/statsd/client.rb, line 108 def increment(stats, sample_rate = 1) update_stats(stats, 1, sample_rate) end
timing(stats, time, sample_rate = 1)
click to toggle source
Sends timing statistics.
@param [Array, String] stats name of statistic(s) being updated @param [Integer] time in miliseconds @param [Integer, Float] sample_rate
# File lib/statsd/client.rb, line 99 def timing(stats, time, sample_rate = 1) data = "#{time}|ms" update_stats(stats, data, sample_rate) end
update_stats(stats, delta = 1, sample_rate = 1)
click to toggle source
Updates one or more counters by an arbitrary amount
@param [Array, String] stats name of statistic(s) being updated @param [Integer, Float] delta @param [Integer, Float] sample_rate
# File lib/statsd/client.rb, line 125 def update_stats(stats, delta = 1, sample_rate = 1) stats = [stats] unless stats.kind_of?(Array) data = {} delta = delta.to_s stats.each do |stat| # if it's got a |ms in it, we know it's a timing stat, so don't append # the |c. data[stat] = delta.include?('|ms') ? delta : "#{delta}|c" end send(data, sample_rate) end
Private Instance Methods
send(data, sample_rate = 1)
click to toggle source
# File lib/statsd/client.rb, line 142 def send(data, sample_rate = 1) #puts "sending #{data} with sample #{sample_rate}" sampled_data = {} if sample_rate < 1 if Kernel.rand <= sample_rate data.each do |k,v| sampled_data[k] = "#{v}|@#{sample_rate}" end end else sampled_data = data end if self.tcp socket = TCPSocket.new( self.host, self.port) else socket = UDPSocket.new end begin sampled_data.each do |k,v| message = [k,v].join(':') if self.tcp socket.send(message, 0) else socket.send(message, 0, self.host, self.port) end end rescue Exception => e puts "Unexpected error: #{e}" ensure socket.close end end