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, sampling = 1) ⇒ Object
Sample a timing.
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"
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/statsy.rb', line 90 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
Usage:
client.increment("coffee.single-espresso")
client.increment("coffee.single-espresso", 1)
client.increment("coffee.single-espresso", 1, 0.5) # 50% of the time
59 60 61 62 |
# File 'lib/statsy.rb', line 59 def increment(stat, count=1, sampling=1) write(stat, count, 'c', sampling) self end |
#measure(stat, time, sampling = 1) ⇒ Object
Sample a timing
Usage:
client.measure("foo.backendtime", response.headers["X-Runtime"].to_i)
69 70 71 72 |
# File 'lib/statsy.rb', line 69 def measure(stat, time, sampling=1) write(stat, time, 'ms', sampling) self end |