This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
forked from statsd/statsd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request statsd#40 from trobrock/master
Ruby client ported from the Python client
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'socket' | ||
|
||
class Statsd | ||
@@config = {} | ||
def self.configure(host, port) | ||
@@config = { | ||
:host => host, | ||
:port => port | ||
} | ||
end | ||
|
||
def self.timing(stat, time, sample_rate=1) | ||
# Log timing information | ||
# > require 'ruby_example' | ||
# > Statsd.timing('some.time', 500) | ||
stats = {} | ||
stats[stat] = "#{time}|ms" | ||
Statsd.send(stats, sample_rate) | ||
end | ||
|
||
def self.increment(stats, sample_rate=1) | ||
# Increments one or more stats counters | ||
# > Statsd.increment('some.int') | ||
# > Statsd.increment('some.int',0.5) | ||
Statsd.update_stats(stats, 1, sample_rate) | ||
end | ||
|
||
def self.decrement(stats, sample_rate=1) | ||
# Decrements one or more stats counters | ||
# > Statsd.decrement('some.int') | ||
Statsd.update_stats(stats, -1, sample_rate) | ||
end | ||
|
||
def self.update_stats(stats, delta=1, sampleRate=1) | ||
# Updates one or more stats counters by arbitrary amounts | ||
# > Statsd.update_stats('some.int',10) | ||
stats = [stats] unless stats.kind_of?(Array) | ||
|
||
data = {} | ||
stats.each do |stat| | ||
data[stat] = "#{delta}|c" | ||
end | ||
|
||
Statsd.send(data, sampleRate) | ||
end | ||
|
||
|
||
def self.send(data, sample_rate=1) | ||
# Squirt the metrics over UDP | ||
if @@config[:host].nil? || @@config[:port].nil? | ||
raise ArgumentError.new("No configuration was sepcified") | ||
end | ||
|
||
sampled_data = {} | ||
|
||
if sample_rate < 1 | ||
if rand <= sample_rate | ||
data.each_key do |stat| | ||
value = data[stat] | ||
sampled_data[stat] = "#{value}|@#{sample_rate}" | ||
end | ||
end | ||
else | ||
sampled_data = data | ||
|
||
sock = UDPSocket.new | ||
sampled_data.each_key do |stat| | ||
value = data[stat] | ||
sock.send("#{stat}:#{value}", 0, @@config[:host], @@config[:port]) | ||
end | ||
end | ||
end | ||
end |