Sha256: 719bc08e34e5b8ba358cbd59a88fc8ca87dfc12c0562f0af992c8bd57fdc037e

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'lookout/statsd'

module Rack
  class Graphite
    PREFIX = 'requests'
    ID_REGEXP = %r{/\d+(/|$)} # Handle /123/ or /123
    ID_REPLACEMENT = '/id\1'.freeze
    GUID_REGEXP = %r{\h{8}-\h{4}-\h{4}-\h{4}-\h{12}} # Handle *GUID*
    GUID_REPLACEMENT = 'guid'.freeze

    def initialize(app, options={})
      @app = app
      @prefix = options[:prefix] || PREFIX
    end

    def call(env)
      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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-graphite-1.5.0 lib/rack/graphite.rb