Class: Statsd

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

Overview

Statsd: A Statsd client (github.com/etsy/statsd)

Examples:

Set up a global Statsd client for a server on localhost:9125

$statsd = Statsd.new 'localhost', 8125

Send some stats

$statsd.increment 'garets'
$statsd.timing 'glork', 320

Use #time to time the execution of a block

$statsd.time('account.activate') { @account.activate! }

Create a namespaced statsd client and increment 'account.activate'

statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'}
statsd.increment 'activate'

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Statsd) initialize(host, port = 8125)

Returns a new instance of Statsd

Parameters:

  • host (String)

    your statsd host

  • port (Integer) (defaults to: 8125)

    your statsd port



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

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

Class Attribute Details

+ (Object) logger

Set to any standard logger instance (including stdlib's Logger) to enable stat logging using logger.debug



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

def logger
  @logger
end

Instance Attribute Details

- (Object) namespace

A namespace to prepend to all statsd calls.



17
18
19
# File 'lib/statsd.rb', line 17

def namespace
  @namespace
end

Instance Method Details

- (Object) count(stat, count, sample_rate = 1)

Sends an arbitrary count for the given stat to the statsd server.

Parameters:

  • stat (String)

    stat name

  • count (Integer)

    count

  • sample_rate (Integer) (defaults to: 1)

    sample rate, 1 for always



50
# File 'lib/statsd.rb', line 50

def count(stat, count, sample_rate=1); send stat, count, 'c', sample_rate end

- (Object) decrement(stat, sample_rate = 1)

Sends a decrement (count = -1) for the given stat to the statsd server.

Parameters:

  • stat (String)

    stat name

  • sample_rate (Integer) (defaults to: 1)

    sample rate, 1 for always

See Also:



43
# File 'lib/statsd.rb', line 43

def decrement(stat, sample_rate=1); count stat, -1, sample_rate end

- (Object) increment(stat, sample_rate = 1)

Sends an increment (count = 1) for the given stat to the statsd server.

Parameters:

  • stat (String)

    stat name

  • sample_rate (Integer) (defaults to: 1)

    sample rate, 1 for always

See Also:



36
# File 'lib/statsd.rb', line 36

def increment(stat, sample_rate=1); count stat, 1, sample_rate end

- (Object) time(stat, sample_rate = 1) { ... }

Reports execution time of the provided block using #timing.

Examples:

Report the time (in ms) taken to activate an account

$statsd.time('account.activate') { @account.activate! }

Parameters:

  • stat

    stat name

  • sample_rate (Integer) (defaults to: 1)

    sample rate, 1 for always

Yields:

  • The operation to be timed

See Also:



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

def time(stat, sample_rate=1)
  start = Time.now
  result = yield
  timing(stat, ((Time.now - start) * 1000).round, sample_rate)
  result
end

- (Object) timing(stat, ms, sample_rate = 1)

Sends a timing (in ms) for the given stat to the statsd server. The sample_rate determines what percentage of the time this report is sent. The statsd server then uses the sample_rate to correctly track the average timing for the stat.

Parameters:

  • stat

    stat name

  • ms (Integer)

    timing in milliseconds

  • sample_rate (Integer) (defaults to: 1)

    sample rate, 1 for always



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

def timing(stat, ms, sample_rate=1); send stat, ms, 'ms', sample_rate end