Mtrc (Metric, for short)

A small library to accumulate metrics and extract basic statistics from them. Want a latency profile of your Rack app? Boom.

gem install mtrc

Middleware all up in this bitch:

class MyMetrics
  def initialize(app)
    @app = app
    @m = Mtrc::SortedSamples.new

    Thread.new do
      sleep 100
      puts "Min:      \#{@m.min}\nMedian:   \#{@m.median}\n95th %:   \#{@m % 95}\n99th %:   \#{@m % 99}\n99.9th %: \#{@m.at .999}\nMax:      \#{@m.max}\n"
      @m = Mtrc::SortedSamples.new
    end
  end

  def call(env)
    t1 = Time.now
    r = @app.call env
    dt = Time.now - t1

    @m << dt
    r
  end        
end

Which requests take the longest?

@m << Mtrc::Sample.new dt, env[:PATH_INFO]
(@m % 95).value # => "?bacon=strips&bacon=strips&bacon=strips"

It's MIT licensed, bro. Pull requests? Roll one up homie.

Rates

r = Mtrc::Rate.new
10.times do
  r.tick 2
end
sleep 1
r.rate #=> 19.999...