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:

`request.{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:

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

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

  • <key>.unmodified (on status 304)

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

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

  • <key>.error (on any status 5xx or when an exception is raised)

Timers (all measured from the middleware perspective):

  • <key> (request time)

  • request.queue (queue time, depends on HTTP_X_QUEUE_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['REQUEST_PATH'].squeeze('/').chomp('/')
  if path == ''
    'root'
  else
    # Skip leading '/'
    env['REQUEST_PATH'][1..-1].tr('/,|', '-')
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(app, path_name: DEFAULT_PATH_NAME) ⇒ RackStatistics

Returns a new instance of RackStatistics.



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

def initialize(app, path_name: DEFAULT_PATH_NAME)
  @app = app
  @path_name  = path_name
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  report_header_metrics(env)

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

  report_response_metrics(env, response, duration)

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