class Dalia::MiniGraphite::RoutesReporter

Public Class Methods

new(app, &routes_block) click to toggle source
# File lib/mini_graphite/routes_reporter.rb, line 4
def initialize(app, &routes_block)
  @app = app
  @routes = {}
  instance_eval(&routes_block) if block_given?
end

Public Instance Methods

call(env) click to toggle source
# File lib/mini_graphite/routes_reporter.rb, line 10
def call(env)
  start_time = Time.now
  status, headers, body  = @app.call(env)
  time_taken = (1000 * (Time.now - start_time))

  current_route = env["sinatra.route"]
  route = @routes.select { |graphite_key, route_regexp| current_route =~ route_regexp}

  if route
    graphite_key = route.keys.first

    Dalia::MiniGraphite.counter("#{graphite_key}.count") if graphite_key
    Dalia::MiniGraphite.counter("#{graphite_key}.duration", time_taken) if graphite_key
    Dalia::MiniGraphite.time("#{graphite_key}.duration_stats", time_taken) if graphite_key
  end

  [status, headers, body]
end

Private Instance Methods

set_graphite_key(graphite_key, route_regexp) click to toggle source
# File lib/mini_graphite/routes_reporter.rb, line 30
def set_graphite_key(graphite_key, route_regexp)
  @routes[graphite_key] = route_regexp
end