Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby 1.8 patch #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/statsd/graphite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
module Statsd
class Graphite < EM::Connection
attr_accessor :counters, :timers, :flush_interval

def initialize(*args)
puts args
super
# stuff here...
end

def post_init
# puts counters.size
# send_data 'Hello'
Expand All @@ -18,21 +18,21 @@ def post_init
end

def receive_data(data)
p data
p data
end

# def unbind
# p ' connection totally closed'
# EventMachine::stop_event_loop
# end

def flush_stats
print "#{Time.now} Flushing #{counters.count} counters and #{timers.count} timers to Graphite."
stat_string = ''
time = ::Benchmark.realtime do
ts = Time.now.to_i
num_stats = 0
num_stats = 0

# store counters
counters.each_pair do |key,value|
message = "stats.#{key} #{value} #{ts}\n"
Expand All @@ -41,10 +41,10 @@ def flush_stats

num_stats += 1
end

# store timers
timers.each_pair do |key, values|
if (values.length > 0)
if (values.length > 0)
pct_threshold = 90
values.sort!
count = values.count
Expand Down Expand Up @@ -74,16 +74,16 @@ def flush_stats
stat_string += message

timers[key] = []

num_stats += 1
end
end
stat_string += "statsd.numStats #{num_stats} #{ts}\n"
end

end
# send to graphite
send_data stat_string
puts "complete. (#{time.round(3)}s)"
puts "complete. (#{(time*100).round()/100.0}s)"
close_connection_after_writing
end
end
Expand Down
19 changes: 10 additions & 9 deletions stats.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'rubygems'
require 'eventmachine'
require 'statsd'
require 'statsd/server'
Expand All @@ -14,7 +15,7 @@
APP_CONFIG['retentions'].each do |retention|
collection_name = retention['name']
unless db.collection_names.include?(collection_name)
db.create_collection(collection_name, :capped => retention['capped'], :size => retention['cap_bytes'])
db.create_collection(collection_name, :capped => retention['capped'], :size => retention['cap_bytes'])
end
db.collection(collection_name).ensure_index([['ts', Mongo::ASCENDING]])
end
Expand All @@ -25,27 +26,27 @@
Statsd::Mongo.retentions = APP_CONFIG['retentions']
Statsd::Mongo.flush_interval = APP_CONFIG['flush_interval']
EventMachine::run do
EventMachine::open_datagram_socket('127.0.0.1', 8125, Statsd::Server)
EventMachine::open_datagram_socket('127.0.0.1', 8125, Statsd::Server)
EventMachine::add_periodic_timer(APP_CONFIG['flush_interval']) do
counters,timers = Statsd::Server.get_and_clear_stats!

#
# Flush Adapters
#
# Mongo
# EM.defer do
# EM.defer do
# Statsd::Mongo.flush_stats(counters,timers)
# end
#

# Graphite
EventMachine.connect APP_CONFIG['graphite_host'], APP_CONFIG['graphite_port'], Statsd::Graphite do |conn|
conn.counters = counters
conn.timers = timers
conn.flush_interval = 10
conn.flush_stats
end
end
end
end


end