Sha256: 4884eae4b71893a24c1d0688eececdd8c44878eb07c0df3d5dea125936c45c04

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Napa
  class Middleware
    class RequestStats
      def initialize(app)
        @app = app
      end

      def normalize_path(path)
        case
          when path == '/'
            'root'
          else
            path.start_with?('/') ? path[1..-1] : path
        end
      end

      def call(env)
        # Mark the request time
        start = Time.now

        # Process the request
        status, headers, body = @app.call(env)

        # Mark the response time
        stop = Time.now

        # Calculate total response time
        response_time = (stop - start) * 1000

        request = Rack::Request.new(env)
        path = normalize_path(request.path_info)

        # Emit stats to StatsD
        Napa::Stats.emitter.increment('request_count')
        Napa::Stats.emitter.timing('response_time', response_time)
        Napa::Stats.emitter.increment("path.#{Napa::Stats.path_to_key(request.request_method, path)}.request_count")
        Napa::Stats.emitter.timing("path.#{Napa::Stats.path_to_key(request.request_method, path)}.response_time", response_time)

        # Return the results
        [status, headers, body]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
napa-0.3.0 lib/napa/middleware/request_stats.rb