Class: Mongrel::StatisticsFilter

Inherits:
HttpHandler show all
Defined in:
lib/mongrel/handlers.rb

Overview

Implements a few basic statistics for a particular URI. Register it anywhere you want in the request chain and it’ll quickly gather some numbers for you to analyze. It is pretty fast, but don’t put it out in production.

You should pass the filter to StatusHandler as StatusHandler.new(:stats_filter => stats). This lets you then hit the status URI you want and get these stats from a browser.

StatisticsFilter takes an option of :sample_rate. This is a number that’s passed to rand and if that number gets hit then a sample is taken. This helps reduce the load and keeps the statistics valid (since sampling is a part of how they work).

The exception to :sample_rate is that inter-request time is sampled on every request. If this wasn’t done then it wouldn’t be accurate as a measure of time between requests.

Instance Attribute Summary collapse

Attributes inherited from HttpHandler

#listener, #request_notify

Instance Method Summary collapse

Methods inherited from HttpHandler

#request_begins, #request_progress

Constructor Details

#initialize(ops = {}) ⇒ StatisticsFilter

Returns a new instance of StatisticsFilter.



341
342
343
344
345
346
347
348
349
# File 'lib/mongrel/handlers.rb', line 341

def initialize(ops={})
  @sample_rate = ops[:sample_rate] || 300

  @processors = Mongrel::Stats.new("processors")
  @reqsize = Mongrel::Stats.new("request Kb")
  @headcount = Mongrel::Stats.new("req param count")
  @respsize = Mongrel::Stats.new("response Kb")
  @interreq = Mongrel::Stats.new("inter-request time")
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



339
340
341
# File 'lib/mongrel/handlers.rb', line 339

def stats
  @stats
end

Instance Method Details

#dumpObject



362
363
364
# File 'lib/mongrel/handlers.rb', line 362

def dump
  "#{@processors.to_s}\n#{@reqsize.to_s}\n#{@headcount.to_s}\n#{@respsize.to_s}\n#{@interreq.to_s}"
end

#process(request, response) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'lib/mongrel/handlers.rb', line 352

def process(request, response)
  if rand(@sample_rate)+1 == @sample_rate
    @processors.sample(listener.workers.list.length)
    @headcount.sample(request.params.length)
    @reqsize.sample(request.body.length / 1024.0)
    @respsize.sample((response.body.length + response.header.out.length) / 1024.0)
  end
  @interreq.tick
end