Class: Rack::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/stats.rb,
lib/rack/stats/stat.rb,
lib/rack/stats/timer.rb,
lib/rack/stats/runtime.rb,
lib/rack/stats/version.rb

Defined Under Namespace

Modules: Stat Classes: Duration, Runtime, Timer

Constant Summary collapse

DEFAULT_STATS =
[
  {
    name: -> (*_args) { 'request_duration_global' },
    value: -> (_req, d, _resp) { d.ms },
    type: :timing
  },
  {
    name: lambda do |req, _d, _resp|
      [
        req.path.eql?('/') ? 'index' : \
          req.path[1..-1].gsub(/\/\d+\//, '/id/')
                         .gsub(/\/\d+$/, '/id')
                         .gsub(/\//, '_'),
        req.request_method.downcase,
        'request_duration'
      ]
    end,
    value: -> (_req, d, _resp) { d.ms },
    type: :timing
  },
  {
    name: -> (*_args) { 'request_number' },
    type: :increment
  },
  {
    name: lambda do |_req, _d, resp|
      ['request_number_status', "#{resp.status / 100}XX"]
    end,
    type: :increment
  }
]
VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ Stats

Returns a new instance of Stats.



38
39
40
41
42
43
# File 'lib/rack/stats.rb', line 38

def initialize(app, args = {})
  @app = app
  @namespace = args.fetch(:namespace, ENV['RACK_STATS_NAMESPACE'])
  @statsd = Statsd.new(*args.fetch(:statsd, '127.0.0.1:8125').split(':'))
  @stats = args.fetch(:stats, DEFAULT_STATS)
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
# File 'lib/rack/stats.rb', line 45

def call(env)
  Runtime.new(@statsd, @app, env, @stats, @namespace).execute
end