Class: Statue::RackStatistics

Inherits:
Object
  • Object
show all
Defined in:
lib/statue/rack_statistics.rb

Overview

Middleware to send metrics about rack requests

this middleware reports metrics with the following pattern:

`{env['REQUEST_METHOD']}.{path_name}

where ‘path_name` can be configured when inserting the middleware like this:

`use RackStatistics, path_name: ->(env) { ... build the path name ... }`

You can build the path using the environment information in the lambda or you can delegate that logic to your app stack and later fetching it from the env, Eg:

`use RackStatistics, path_name: ->(env) { env['route.path_name'] }`

This middleware will report the following metrics

Counters:

  • request.<key>.status-XXX (where XXX is the status code)

  • request.<key>.success (on any status 2XX)

  • request.<key>.unmodified (on status 304)

  • request.<key>.redirect (on any status 3XX)

  • request.<key>.failure (on any status 4xx)

  • request.<key>.error (on any status 5xx)

  • request.<key>.unhandled-exception (when an exception is raised that your application didn’t handle)

Timers (all measured from the middleware perspective):

  • request.<key>.runtime (request time)

  • request.queue (queue time, depends on HTTP_X_REQUEST_START header)

To get accurate timers, the middleware should be as higher as possible in your rack stack

Constant Summary collapse

DEFAULT_PATH_NAME =
lambda do |env|
  # Remove duplicate and trailing '/'
  path = env['PATH_INFO'].squeeze('/').chomp('/')
  if path == ''
    'root'
  else
    # Skip leading '/' and replace statsd special characters by '-'
    env['REQUEST_PATH'][1..-1].tr('/,|', '-')
  end
end

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RackStatistics.



48
49
50
51
# File 'lib/statue/rack_statistics.rb', line 48

def initialize(app, options = {})
  @app = app
  @path_name = options[:path_name] || DEFAULT_PATH_NAME
end

Instance Method Details

#call(env) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/statue/rack_statistics.rb', line 53

def call(env)
  report_header_metrics(env)

  response = nil
  duration = Statue::Clock.duration_in_ms do
    response = @app.call(env)
  end

  report_response_metrics(env, response, duration)

  response
rescue => e
  report_exception(env, e) and raise
end