Class: Rack::Graphite

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/graphite.rb,
lib/rack/graphite/version.rb

Constant Summary collapse

PREFIX =
'requests'
ID_REGEXP =

Handle /123/ or /123

%r{/\d+(/|$)}
ID_REPLACEMENT =
'/id\1'.freeze
GUID_REGEXP =

Handle GUID

%r{\h{8}-\h{4}-\h{4}-\h{4}-\h{12}}
GUID_REPLACEMENT =
'guid'.freeze
VERSION =
'1.6.0'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Graphite

Returns a new instance of Graphite.



11
12
13
14
15
# File 'lib/rack/graphite.rb', line 11

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

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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