Sha256: 3b500300044195e4a95900a00d8dd668d69e043c70ef01e201f8875e2974247d

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 Bytes

Contents

module Dalia
  module MiniGraphite
    class RoutesReporter
      def initialize(app, &routes_block)
        @app = app
        @routes = {}
        instance_eval(&routes_block) if block_given?
      end

      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
        end

        [status, headers, body]
      end

      private
        def set_graphite_key(graphite_key, route_regexp)
          @routes[graphite_key] = route_regexp
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mini_graphite-0.2.1 lib/mini_graphite/routes_reporter.rb