Class: Statsy::Client
- Inherits:
-
Object
- Object
- Statsy::Client
- Defined in:
- lib/statsy.rb
Instance Method Summary collapse
-
#batch {|self.class.new(batch = Transport::Queue.new)| ... } ⇒ Object
Batch multiple transport operations, that will group any counts together and send the fewest number of packets with the counts/timers optimized at the end of the batch block.
-
#increment(stat, count = 1, sampling = 1) ⇒ Object
Increment a count optionally at a random sample rate.
-
#initialize(transport = Transport::UDP.new("stats", 8125)) ⇒ Client
constructor
Construct a client with a given transport that implements Transport::Interface.
-
#measure(stat, time) ⇒ Object
Sample a timing.
-
#record(stat, gauge) ⇒ Object
Record an arbitrary value.
Constructor Details
#initialize(transport = Transport::UDP.new("stats", 8125)) ⇒ Client
48 49 50 |
# File 'lib/statsy.rb', line 48 def initialize(transport=Transport::UDP.new("stats", 8125)) @transport = transport end |
Instance Method Details
#batch {|self.class.new(batch = Transport::Queue.new)| ... } ⇒ Object
Batch multiple transport operations, that will group any counts together and send the fewest number of packets with the counts/timers optimized at the end of the batch block.
Note: this does not attempt to fit the packet size within the MTU.
Usage:
client.batch do |batch|
batch.increment("foo.bar", 10)
batch.measure("bat.baz", 101)
batch.measure("foo.bar", 101)
end
=> write "foo.bar:10|c:333|ms"
=> write "bat.baz:101|ms"
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/statsy.rb', line 123 def batch yield self.class.new(batch = Transport::Queue.new) batch.inject(Hash.new { |h,k| h[k]=[] }) do |stats, stat| # [ "foo.bar:10|c", "foo.bar:101|ms" ] key, value = stat.split(':', 2) stats[key] << value stats end.sort.each do |pairs| # [ "foo.bar", [ "10|c", "101|ms" ] ] @transport.write(pairs.flatten.join(":")) end self end |
#increment(stat, count = 1, sampling = 1) ⇒ Object
Increment a count optionally at a random sample rate. These keys will live under the stats and stats_counts keys.
Usage:
client.increment("coffee.single-espresso")
client.increment("coffee.single-espresso", 1)
client.increment("coffee.single-espresso", 1, 0.5) # 50% of the time
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/statsy.rb', line 60 def increment(stat, count=1, sampling=1) if sampling < 1 if Kernel.rand < sampling write(stat, count, 'c', sampling) end else write(stat, count, 'c', 1) end self end |
#measure(stat, time) ⇒ Object
Sample a timing. The units of the timing are up to you and your consumers, milliseconds is common.
Including the units in the key name will help communicate the units to consumers of these measurements.
The statistics will be aggregated over the sampling period configured in your statsd. By default this is every 10 seconds.
In graphite, these reports will end up under the stats.timings key.
Usage:
client.measure("foo.backendtime", response.headers["X-Runtime"].to_i)
Produces the statistics found per sampling interval.
stats.timings.foo.backendtime.count
stats.timings.foo.backendtime.lower
stats.timings.foo.backendtime.mean_90
stats.timings.foo.backendtime.upper
stats.timings.foo.backendtime.upper_90
92 93 94 95 |
# File 'lib/statsy.rb', line 92 def measure(stat, time) write(stat, time, 'ms', 1) self end |
#record(stat, gauge) ⇒ Object
Record an arbitrary value
Usage:
client.record("foo.arbitrary", 42)
102 103 104 105 |
# File 'lib/statsy.rb', line 102 def record(stat, gauge) write(stat, gauge, 'g', 1) self end |