class Rack::DogStatsd

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/dogstatsd.rb, line 6
def initialize(app, options = {})
  @app = app

  host = options[:host] || 'localhost'
  port = options[:port] || 8125
  @statsd = options[:statsd] || Statsd.new(host, port, **options)
  @metric = options[:metric] || 'rack-dogstatsd.response.time'
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/dogstatsd.rb, line 15
def call(env)
  start = Time.now

  status, header, body = @app.call env

  path = env['PATH_INFO']

  routing_args = env['rack.routing_args'] || {}
  path = path.dup.tap do |path|
    routing_args.except(:format, :namespace, :catch).each do |param, arg|
      path.sub!(arg.to_s, ":#{param}")
    end
  end

  @statsd.timing(@metric, (Time.now - start) * 1000, tags: [
    "path:#{ path }",
    "method:#{ env["REQUEST_METHOD"].downcase }",
    "status:#{ status }"
  ])

  [status, header, body]
end