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) ⇒ Client

Initializes a Statsd client.

Parameters:

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


85
86
87
# File 'lib/statsd/client.rb', line 85

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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



79
80
81
# File 'lib/statsd/client.rb', line 79

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



79
80
81
# File 'lib/statsd/client.rb', line 79

def port
  @port
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)


111
112
113
# File 'lib/statsd/client.rb', line 111

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)


103
104
105
# File 'lib/statsd/client.rb', line 103

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)


94
95
96
97
# File 'lib/statsd/client.rb', line 94

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)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/statsd/client.rb', line 120

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