Class: Meter::Backend

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

Overview

Statsd: A DogStatsd client (www.datadoghq.com)

Examples:

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

require 'statsd'
$statsd = Statsd.new 'localhost', 8125

Send some stats

$statsd.increment 'page.views'
$statsd.timing 'page.load', 320
$statsd.gauge 'users.online', 100

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'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '127.0.0.1', port = 8125) ⇒ Backend

Returns a new instance of Backend.

Parameters:

  • host (String) (defaults to: '127.0.0.1')

    your statsd host

  • port (Integer) (defaults to: 8125)

    your statsd port



44
45
46
47
48
# File 'lib/meter/backend.rb', line 44

def initialize(host = '127.0.0.1', port = 8125)
  self.host, self.port = host, port
  @prefix = nil
  @socket = UDPSocket.new
end

Instance Attribute Details

#hostObject

StatsD host. Defaults to 127.0.0.1.



28
29
30
# File 'lib/meter/backend.rb', line 28

def host
  @host
end

#namespaceObject

A namespace to prepend to all statsd calls.



25
26
27
# File 'lib/meter/backend.rb', line 25

def namespace
  @namespace
end

#portObject

StatsD port. Defaults to 8125.



31
32
33
# File 'lib/meter/backend.rb', line 31

def port
  @port
end

Class Method Details

.loggerObject



33
34
35
# File 'lib/meter/backend.rb', line 33

def self.logger
  Meter.config.logger
end

.VERSIONObject

Return the current version of the library.



38
39
40
# File 'lib/meter/backend.rb', line 38

def self.VERSION
  "1.1.0"
end

Instance Method Details

#count(stat, count, opts = {}) ⇒ Object

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

Parameters:

  • stat (String)

    stat name

  • count (Integer)

    count

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags



92
93
94
# File 'lib/meter/backend.rb', line 92

def count(stat, count, opts={})
  send_stats stat, count, :c, opts
end

#decrement(stat, opts = {}) ⇒ Object

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

Parameters:

  • stat (String)

    stat name

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags

See Also:



81
82
83
# File 'lib/meter/backend.rb', line 81

def decrement(stat, opts={})
  count stat, -1, opts
end

#gauge(stat, value, opts = {}) ⇒ Object

Sends an arbitary gauge value for the given stat to the statsd server.

This is useful for recording things like available disk space, memory usage, and the like, which have different semantics than counters.

Examples:

Report the current user count:

$statsd.gauge('user.count', User.count)

Parameters:

  • stat (String)

    stat name.

  • gauge (Numeric)

    value.

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags



109
110
111
# File 'lib/meter/backend.rb', line 109

def gauge(stat, value, opts={})
  send_stats stat, value, :g, opts
end

#histogram(stat, value, opts = {}) ⇒ Object

Sends a value to be tracked as a histogram to the statsd server.

Examples:

Report the current user count:

$statsd.histogram('user.count', User.count)

Parameters:

  • stat (String)

    stat name.

  • histogram (Numeric)

    value.

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags



122
123
124
# File 'lib/meter/backend.rb', line 122

def histogram(stat, value, opts={})
  send_stats stat, value, :h, opts
end

#increment(stat, opts = {}) ⇒ Object

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

Parameters:

  • stat (String)

    stat name

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags

See Also:



70
71
72
# File 'lib/meter/backend.rb', line 70

def increment(stat, opts={})
  count stat, 1, opts
end

#set(stat, value, opts = {}) ⇒ Object

Sends a value to be tracked as a set to the statsd server.

Examples:

Record a unique visitory by id:

$statsd.set('visitors.uniques', User.id)

Parameters:

  • stat (String)

    stat name.

  • set (Numeric)

    value.

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags



165
166
167
# File 'lib/meter/backend.rb', line 165

def set(stat, value, opts={})
  send_stats stat, value, :s, opts
end

#time(stat, opts = {}) { ... } ⇒ Object

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 (String)

    stat name

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags

Yields:

  • The operation to be timed

See Also:



150
151
152
153
154
155
# File 'lib/meter/backend.rb', line 150

def time(stat, opts={})
  start = Time.now
  result = yield
  timing(stat, ((Time.now - start) * 1000).round, opts)
  result
end

#timing(stat, ms, opts = {}) ⇒ Object

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 (String)

    stat name

  • ms (Integer)

    timing in milliseconds

  • opts (Hash) (defaults to: {})

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags



136
137
138
# File 'lib/meter/backend.rb', line 136

def timing(stat, ms, opts={})
  send_stats stat, ms, :ms, opts
end