Class: Metrics::Integration::Rack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-metrics/integration/rack_middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 23

def initialize(app, options ={})
  @app      = app
  @options  = {:show => "/stats"}.merge(options)
  @agent    = @options.delete(:agent) || Agent.new
  
  # Integration Metrics
  @requests             = @agent.timer(:_requests)
  @uncaught_exceptions  = @agent.counter(:_uncaught_exceptions)
  
  # HTTP Status Codes
  @status_codes = {
    1 => @agent.counter(:_status_1xx),
    2 => @agent.counter(:_status_2xx),
    3 => @agent.counter(:_status_3xx),
    4 => @agent.counter(:_status_4xx),
    5 => @agent.counter(:_status_5xx)
  }
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



18
19
20
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 18

def agent
  @agent
end

#appObject

Returns the value of attribute app.



18
19
20
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 18

def app
  @app
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 18

def options
  @options
end

#requestsObject

Returns the value of attribute requests.



18
19
20
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 18

def requests
  @requests
end

#status_codesObject

Returns the value of attribute status_codes.



18
19
20
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 18

def status_codes
  @status_codes
end

#uncaught_exceptionsObject

Returns the value of attribute uncaught_exceptions.



18
19
20
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 18

def uncaught_exceptions
  @uncaught_exceptions
end

Instance Method Details

#call(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 42

def call(env)
  return show(env) if show?(env)
  
  env['metrics.agent'] = @agent
  
  status, headers, body = self.requests.time{ @app.call(env) }
  
  if status_counter = self.status_codes[status / 100]
    status_counter.incr
  end
  
  [status, headers, body]
rescue Exception
  # TODO: add "last_uncaught_exception" with string of error
  self.uncaught_exceptions.incr
  raise
end

#show(_) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 69

def show(_)
  body = @agent.to_json
  
  [ 200,
    { 'Content-Type'    => 'application/json',
      'Content-Length'  => body.size.to_s },
    [body]
  ]
end

#show?(env, test = ) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/ruby-metrics/integration/rack_middleware.rb', line 60

def show?(env, test = self.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