Class: Statsd::Client

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

Constant Summary collapse

VERSION =
File.read( File.join(File.dirname(__FILE__),'..', '..', 'VERSION') ).strip

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 8125, tcp = false) ⇒ Client

Initializes a Statsd client.

Parameters:

  • host (String) (defaults to: 'localhost')
  • port (Integer) (defaults to: 8125)
  • tcp (Boolean) (defaults to: false)


90
91
92
# File 'lib/statsd/client.rb', line 90

def initialize(host = 'localhost', port = 8125, tcp = false)
  @host, @port, @tcp = host, port, tcp
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



83
84
85
# File 'lib/statsd/client.rb', line 83

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



83
84
85
# File 'lib/statsd/client.rb', line 83

def port
  @port
end

#tcpObject (readonly)

Returns the value of attribute tcp.



83
84
85
# File 'lib/statsd/client.rb', line 83

def tcp
  @tcp
end

Instance Method Details

#decrement(stats, sample_rate = 1) ⇒ Object

Decrements a counter

Parameters:

  • stats (Array, String)

    name of statistic(s) being updated

  • sample_rate (Integer, Float) (defaults to: 1)


116
117
118
# 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) ⇒ Object

Increments a counter

Parameters:

  • stats (Array, String)

    name of statistic(s) being updated

  • sample_rate (Integer, Float) (defaults to: 1)


108
109
110
# 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) ⇒ Object

Sends timing statistics.

Parameters:

  • stats (Array, String)

    name of statistic(s) being updated

  • time (Integer)

    in miliseconds

  • sample_rate (Integer, Float) (defaults to: 1)


99
100
101
102
# 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) ⇒ Object

Updates one or more counters by an arbitrary amount

Parameters:

  • stats (Array, String)

    name of statistic(s) being updated

  • delta (Integer, Float) (defaults to: 1)
  • sample_rate (Integer, Float) (defaults to: 1)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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