class Metrics::Integration::Rack::Middleware

Attributes

agent[RW]
app[RW]
options[RW]

Public Class Methods

new(app, options ={}) click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 20
def initialize(app, options ={})
  @app      = app
  @options  = {:show => "/stats"}.merge(options)
  @agent    = @options.delete(:agent) || Agent.new
end

Public Instance Methods

call(env) click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 26
def call(env)
  return show(env) if show?(env)
  
  env['metrics.agent'] = @agent
  
  status, headers, body = time_request { @app.call(env) }

  incr_status_code_counter(status / 100)

  [status, headers, body]
rescue Exception
  # TODO: add "last_uncaught_exception" with string of error
  incr_uncaught_exceptions
  raise
end

Private Instance Methods

incr_status_code_counter(hundred) click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 74
def incr_status_code_counter(hundred)
  @agent.counter(:"_status_#{hundred}xx").incr
end
incr_uncaught_exceptions() click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 70
def incr_uncaught_exceptions
  @agent.counter(:_uncaught_exceptions).incr
end
show(_) click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 56
def show(_)
  body = @agent.to_json
  
  [ 200,
    { 'Content-Type'    => 'application/json',
      'Content-Length'  => body.size.to_s },
    [body]
  ]
end
show?(env, test = options[:show]) click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 43
def show?(env, test = options[:show])
  case
  when String === test
    env['PATH_INFO'] == test
  when Regexp === test
    env['PATH_INFO'] =~ test
  when test.respond_to?(:call)
    test.call(env)
  else
    test
  end
end
time_request(&block) click to toggle source
# File lib/ruby-metrics/integration/rack_middleware.rb, line 66
def time_request(&block)
  @agent.timer(:_requests).time(&block)
end