Browse files

Prefer jruby, expose de minimis statistics, bug fixes and tweaks

  • Loading branch information...
1 parent 4eb902e commit 1ba7b0b9c2666b0d448578d51eee80fd12b35de2 @noahhl committed May 10, 2012
Showing with 42 additions and 10 deletions.
  1. +1 −1 .rbenv-version
  2. +7 −1 bin/batsd
  3. +1 −1 config.yml
  4. +1 −0 lib/batsd.rb
  5. +1 −1 lib/batsd/diskstore.rb
  6. +2 −1 lib/batsd/handlers/counter.rb
  7. +7 −5 lib/batsd/handlers/timer.rb
  8. +1 −0 lib/batsd/receiver.rb
  9. +21 −0 lib/batsd/statistics.rb
View
2 .rbenv-version
@@ -1 +1 @@
-1.9.3-p194
+jruby-1.7.0-dev
View
8 bin/batsd
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'rubygems'
require 'yaml'
@@ -41,6 +41,12 @@ begin
puts "Need to specify a truncation level (#{config[:retentions].keys.join(",")} are configured)."
exit 1
end
+ when :stats
+ require 'socket'
+ TCPSocket.new('localhost', (config[:port] || 8125).to_i + 1).tap do |socket|
+ socket.puts "stats\n"
+ puts socket.gets
+ end
when :server
# Define the handlers that will handle each data type
# The key that they are assigned to should correspond to
View
2 config.yml
@@ -5,7 +5,7 @@ redis:
host: 127.0.0.1
port: 6379
retentions:
- 10: 2160 #6 hours
+ 10: 360 #1 hour
60: 10080 #1 week
600: 52594 #1 year
redis_cleanup: 30
View
1 lib/batsd.rb
@@ -8,6 +8,7 @@
require 'batsd/redis'
require 'batsd/threadpool'
require 'batsd/receiver'
+require 'batsd/statistics'
require 'batsd/truncator'
require 'batsd/handler'
require 'batsd/handlers/gauge'
View
2 lib/batsd/diskstore.rb
@@ -35,7 +35,7 @@ def append_value_to_file(filename, value)
file.close
end
rescue Exception => e
- puts "Encountered an error trying to store to #{filename}: #{e}"
+ puts "Encountered an error trying to store to #{filename}: #{e} #{e.message} #{e.backtrace if ENV["VERBOSE"]}"
end
# Reads the set of values in the range desired from file
View
3 lib/batsd/handlers/counter.rb
@@ -88,8 +88,9 @@ def flush
# Only if we're in need of a write to disk - if the next flush will be
# past the threshold
if (flush_start + @flush_interval) > @last_flushes[retention] + retention.to_i
+ puts "Starting disk writing for timers@#{retention}" if ENV["VERBOSE"]
ts = (flush_start - flush_start % retention.to_i)
- @counters.dup.keys.each do |key|
+ @counters.keys.each do |key|
@threadpool.queue ts, key, retention do |timestamp, key, retention|
key = "#{key}:#{retention}"
value = @redis.get_and_clear_key(key)
View
12 lib/batsd/handlers/timer.rb
@@ -27,9 +27,11 @@ def initialize(options)
def handle(key, value, sample_rate)
key = "timers:#{key}"
- @active_timers[key] ||= []
- @active_timers[key].push value.to_f
- @timers[key] = nil
+ if value
+ @active_timers[key] ||= []
+ @active_timers[key].push value.to_f
+ @timers[key] = nil
+ end
end
def flush
@@ -71,9 +73,9 @@ def flush
# Only if we're in need of a write to disk - if the next flush will be
# past the threshold
if (flush_start + @flush_interval) > @last_flushes[retention] + retention.to_i
-
+ puts "Starting disk writing for counters@#{retention}" if ENV["VERBOSE"]
ts = (flush_start - flush_start % retention.to_i)
- @timers.dup.keys.each do |key|
+ @timers.keys.each do |key|
@threadpool.queue ts, key, retention do |timestamp, key, retention|
values = @redis.get_and_clear_key("#{key}:#{retention}").split("<X>").reject(&:empty?).collect(&:to_f)
if values
View
1 lib/batsd/receiver.rb
@@ -72,6 +72,7 @@ def run
port = @options[:port] || 8125
puts "Starting receiver on batsd://#{bind}:#{port}"
EventMachine::open_datagram_socket(bind, port, Batsd::Receiver)
+ EventMachine::start_server(bind, port + 1, Batsd::Statistics)
@handlers.each do |type, handler|
if handler.respond_to? :flush
View
21 lib/batsd/statistics.rb
@@ -0,0 +1,21 @@
+require 'json'
+module Batsd
+ module Statistics
+
+ def post_init
+ puts "batsd statistics are available from /tmp/statsd.sock"
+ end
+
+ def receive_data(msg)
+ if msg.match /stats/
+ stats = {}
+ Batsd::Receiver.handlers.each{|type, handler| stats[type] = handler.statistics }
+ send_data "#{stats.to_json}\n"
+ elsif msg.match /quit|exit/
+ send_data "BYE\n"
+ close_connection
+ end
+ end
+
+ end
+end

0 comments on commit 1ba7b0b

Please sign in to comment.