class Rack::Graphite

Constants

GUID_REGEXP
GUID_REPLACEMENT
ID_REGEXP
ID_REPLACEMENT
PREFIX
VERSION

Public Class Methods

new(app, options={}) click to toggle source
# File lib/rack/graphite.rb, line 11
def initialize(app, options={})
  @app = app
  @prefix = options[:prefix] || PREFIX
  @filters = options[:filters] || []
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/graphite.rb, line 17
def call(env)
  @filters.each do |filter|
    if filter.call(env)
      return @app.call(env)
    end
  end

  path = env['PATH_INFO'] || '/'
  method = env['REQUEST_METHOD'] || 'GET'
  metric = path_to_graphite(method, path)

  status, headers, body = nil
  Lookout::Statsd.instance.time(metric) do
    status, headers, body = @app.call(env)
  end
  Lookout::Statsd.instance.increment("#{metric}.response.#{status}")
  return status, headers, body
end
path_to_graphite(method, path) click to toggle source
# File lib/rack/graphite.rb, line 36
def path_to_graphite(method, path)
  method = method.downcase
  if (path.nil?) || (path == '/') || (path.empty?)
    "#{@prefix}.#{method}.root"
  else
    # Replace ' ' => '_', '.' => '-'
    path = path.tr(' .', '_-')
    path.gsub!(ID_REGEXP, ID_REPLACEMENT)
    path.gsub!(GUID_REGEXP, GUID_REPLACEMENT)
    path.tr!('/', '.')

    "#{@prefix}.#{method}#{path}"
  end
end