Skip to content

Instantly share code, notes, and snippets.

@mks-m
Created June 21, 2012 18:30
Show Gist options
  • Save mks-m/2967623 to your computer and use it in GitHub Desktop.
Save mks-m/2967623 to your computer and use it in GitHub Desktop.
the realtime newrelic
# Usage, assuming that production.log is your rails log
#
# $ tail -f production.log | grep Completed | ruby tailor.rb <estimate> <size>
#
# * estimate is estimated number of requests per second (make it 50% larger)
# * size is length of every stat block(second, minute, hour)
# a line to print out
#
# @accept [Integer] num number of requests
# @accept [Integer] max estimated maximum of requests to scale graph to
# @accept [Integer] size result line size
# @return [String] graph line
def spark(num, max, size)
size -= 10
fill = (size * num / max.to_f).to_i
fill = size if fill > size
none = size - fill
("% 8d " % num) << ("█" * fill) << (" " * none) << "|"
end
estimate = ARGV.shift.to_i rescue 300
spark_size = ARGV.shift.to_i rescue 40
last_minute,last_hour,requests,time = [],[],0,Time.now.to_i
while gets
requests += 1
if Time.now.to_i > time
last_minute << requests
last_minute.shift if last_minute.size > 60
last_hour << last_minute.inject(&:+)
last_hour.shift if last_hour.size > 60
puts Time.now.strftime("%y.%m.%d %H:%M") <<
spark(requests, estimate, spark_size) <<
spark(last_hour.last, estimate * 60, spark_size) <<
spark(last_hour.inject(&:+), estimate * 60 * 60, spark_size)
requests, time = 0, Time.now.to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment