class Statue::RackStatistics

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:

Timers (all measured from the middleware perspective):

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

Constants

DEFAULT_PATH_NAME

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/statue/rack_statistics.rb, line 48
def initialize(app, options = {})
  @app = app
  @path_name = options[:path_name] || DEFAULT_PATH_NAME
end

Public Instance Methods

call(env) click to toggle source
# 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

Private Instance Methods

metric_name(env) click to toggle source
# File lib/statue/rack_statistics.rb, line 91
def metric_name(env)
  "request.#{env['REQUEST_METHOD']}.#{@path_name.call(env)}"
end
report_exception(env, _exception) click to toggle source
# File lib/statue/rack_statistics.rb, line 87
def report_exception(env, _exception)
  Statue.report_increment "#{metric_name(env)}.unhandled-exception"
end
report_header_metrics(env) click to toggle source
# File lib/statue/rack_statistics.rb, line 70
def report_header_metrics(env)
  if start_header = (env['HTTP_X_REQUEST_START'] || env['HTTP_X_QUEUE_START'])
    queue_start = start_header[/t=([\d\.]+)/, 1].to_f
    Statue.report_duration 'request.queue', (Time.now.to_f - queue_start) * 1_000
  end
end
report_response_metrics(env, response, duration) click to toggle source
# File lib/statue/rack_statistics.rb, line 77
def report_response_metrics(env, response, duration)
  metric_name = metric_name(env)
  status, _headers, _body = response

  Statue.report_duration "#{metric_name}.runtime", duration

  Statue.report_increment "#{metric_name}.status-#{status}"
  Statue.report_increment "#{metric_name}.#{status_group(status)}"
end
status_group(status) click to toggle source

success: ok (2XX) failure: client error (4xx) error: server error (5xx) unmodified: (304) redirect: (3XX)

# File lib/statue/rack_statistics.rb, line 100
def status_group(status)
  case status.to_i
  when 100..299
    'success'
  when 304
    'unmodified'
  when 300..399
    'redirect'
  when 400..499
    'failure'
  when 500..599
    'error'
  else
    'invalid-status'
  end
end