Class: Statsd
- Inherits:
-
Object
- Object
- Statsd
- Defined in:
- lib/statsd.rb
Overview
Statsd: A Statsd client (github.com/etsy/statsd)
Statsd instances are thread safe for general usage, by using a thread local UDPSocket and carrying no state. The attributes are stateful, and are not mutexed, it is expected that users will not change these at runtime in threaded environments. If users require such use cases, it is recommend that users either mutex around their Statsd object, or create separate objects for each namespace / host+port combination.
Direct Known Subclasses
Defined Under Namespace
Classes: Batch
Class Attribute Summary collapse
-
.logger ⇒ Object
Set to a standard logger instance to enable debug logging.
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
The default batch size for new batches (default: 10).
-
#host ⇒ Object
StatsD host.
-
#namespace ⇒ Object
A namespace to prepend to all statsd calls.
-
#port ⇒ Object
StatsD port.
-
#postfix ⇒ Object
a postfix to append to all metrics.
-
#prefix ⇒ Object
readonly
StatsD namespace prefix, generated from #namespace.
Instance Method Summary collapse
-
#batch {|Batch| ... } ⇒ Object
Creates and yields a Batch that can be used to batch instrument reports into larger packets.
-
#count(stat, count, sample_rate = 1) ⇒ Object
Sends an arbitrary count for the given stat to the statsd server.
-
#decrement(stat, sample_rate = 1) ⇒ Object
Sends a decrement (count = -1) for the given stat to the statsd server.
-
#gauge(stat, value, sample_rate = 1) ⇒ Object
Sends an arbitary gauge value for the given stat to the statsd server.
-
#increment(stat, sample_rate = 1) ⇒ Object
Sends an increment (count = 1) for the given stat to the statsd server.
-
#initialize(host = '127.0.0.1', port = 8125) ⇒ Statsd
constructor
A new instance of Statsd.
-
#set(stat, value, sample_rate = 1) ⇒ Object
Sends an arbitary set value for the given stat to the statsd server.
-
#time(stat, sample_rate = 1) { ... } ⇒ Object
Reports execution time of the provided block using #timing.
-
#timing(stat, ms, sample_rate = 1) ⇒ Object
Sends a timing (in ms) for the given stat to the statsd server.
Constructor Details
#initialize(host = '127.0.0.1', port = 8125) ⇒ Statsd
Returns a new instance of Statsd.
116 117 118 119 120 121 |
# File 'lib/statsd.rb', line 116 def initialize(host = '127.0.0.1', port = 8125) self.host, self.port = host, port @prefix = nil @batch_size = 10 @postfix = nil end |
Class Attribute Details
.logger ⇒ Object
Set to a standard logger instance to enable debug logging.
111 112 113 |
# File 'lib/statsd.rb', line 111 def logger @logger end |
Instance Attribute Details
#batch_size ⇒ Object
The default batch size for new batches (default: 10)
104 105 106 |
# File 'lib/statsd.rb', line 104 def batch_size @batch_size end |
#host ⇒ Object
StatsD host. Defaults to 127.0.0.1.
95 96 97 |
# File 'lib/statsd.rb', line 95 def host @host end |
#namespace ⇒ Object
A namespace to prepend to all statsd calls.
92 93 94 |
# File 'lib/statsd.rb', line 92 def namespace @namespace end |
#port ⇒ Object
StatsD port. Defaults to 8125.
98 99 100 |
# File 'lib/statsd.rb', line 98 def port @port end |
#postfix ⇒ Object
a postfix to append to all metrics
107 108 109 |
# File 'lib/statsd.rb', line 107 def postfix @postfix end |
#prefix ⇒ Object (readonly)
StatsD namespace prefix, generated from #namespace
101 102 103 |
# File 'lib/statsd.rb', line 101 def prefix @prefix end |
Instance Method Details
#batch {|Batch| ... } ⇒ Object
Creates and yields a Batch that can be used to batch instrument reports into larger packets. Batches are sent either when the packet is “full” (defined by batch_size), or when the block completes, whichever is the sooner.
247 248 249 |
# File 'lib/statsd.rb', line 247 def batch(&block) Batch.new(self).easy &block end |
#count(stat, count, sample_rate = 1) ⇒ Object
Sends an arbitrary count for the given stat to the statsd server.
175 176 177 |
# File 'lib/statsd.rb', line 175 def count(stat, count, sample_rate=1) send_stats stat, count, :c, sample_rate end |
#decrement(stat, sample_rate = 1) ⇒ Object
Sends a decrement (count = -1) for the given stat to the statsd server.
166 167 168 |
# File 'lib/statsd.rb', line 166 def decrement(stat, sample_rate=1) count stat, -1, sample_rate end |
#gauge(stat, value, sample_rate = 1) ⇒ 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.
190 191 192 |
# File 'lib/statsd.rb', line 190 def gauge(stat, value, sample_rate=1) send_stats stat, value, :g, sample_rate end |
#increment(stat, sample_rate = 1) ⇒ Object
Sends an increment (count = 1) for the given stat to the statsd server.
157 158 159 |
# File 'lib/statsd.rb', line 157 def increment(stat, sample_rate=1) count stat, 1, sample_rate end |
#set(stat, value, sample_rate = 1) ⇒ Object
Sends an arbitary set value for the given stat to the statsd server.
This is for recording counts of unique events, which are useful to see on graphs to correlate to other values. For example, a deployment might get recorded as a set, and be drawn as annotations on a CPU history graph.
206 207 208 |
# File 'lib/statsd.rb', line 206 def set(stat, value, sample_rate=1) send_stats stat, value, :s, sample_rate end |
#time(stat, sample_rate = 1) { ... } ⇒ Object
Reports execution time of the provided block using #timing.
230 231 232 233 234 235 |
# File 'lib/statsd.rb', line 230 def time(stat, sample_rate=1) start = Time.now result = yield timing(stat, ((Time.now - start) * 1000).round, sample_rate) result end |
#timing(stat, ms, sample_rate = 1) ⇒ 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.
218 219 220 |
# File 'lib/statsd.rb', line 218 def timing(stat, ms, sample_rate=1) send_stats stat, ms, :ms, sample_rate end |