Sha256: 0465624f0ab1c941ef4b38ef178dfa6a39b4f001c91c49852f7d023681c8879f

Contents?: true

Size: 844 Bytes

Versions: 2

Compression:

Stored size: 844 Bytes

Contents

module Goliath
  module Rack
    # Middleware to inject the tracer statistics into the response headers.
    #
    # @example
    #  use Goliath::Rack::Tracer
    #
    class Tracer
      def initialize(app)
        @app = app
      end

      def call(env)
        async_cb = env['async.callback']

        env['async.callback'] = Proc.new do |status, headers, body|
          async_cb.call(post_process(env, status, headers, body))
          env.logger.info env.trace_stats.collect{|s| s.join(':')}.join(', ')
        end

        status, headers, body = @app.call(env)
        post_process(env, status, headers, body)
      end

      def post_process(env, status, headers, body)
        extra = { 'X-PostRank' => env.trace_stats.collect{|s| s.join(': ')}.join(', ')}
        [status, headers.merge(extra), body]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
goliath-0.9.1 lib/goliath/rack/tracer.rb
goliath-0.9.0 lib/goliath/rack/tracer.rb