Class: Rack::Aggregate::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/aggregate/context.rb

Constant Summary collapse

AGGREGATE_PATH =
/^\/aggregate$/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) {|_self| ... } ⇒ Context

Returns a new instance of Context.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
12
13
14
15
# File 'lib/rack/aggregate/context.rb', line 6

def initialize(app, options = {}, &blk)
  options = {
    # ...
  }.merge(options)

  @app = app
  @aggregate = ::Aggregate.new

  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/aggregate/context.rb', line 17

def call(env)
  if env['PATH_INFO'] =~ AGGREGATE_PATH
    resp = Rack::Response.new('', 200)
    resp['Content-Type'] = 'text/plain'

    resp.write "count:\t\t#{@aggregate.count}\n"
    [:mean, :min, :max, :std_dev].each do |metric|
      resp.write "#{metric}:\t\t%1.2fms\n" % (@aggregate.send(metric) || 0)
    end

    resp.write "Request histogram:\n"
    resp.write @aggregate.to_s

    return resp.finish
  end

  start = Time.now
  @status, @headers, @response = @app.call(env)
  @aggregate << (Time.now - start) * 1000

  [@status, @headers, @response]
end