Sha256: dec49d1e22f4d2707327861dcbeb2cd89c0bf9375d76bf670cc2b51c9a98c840

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Mnemosyne
  module Middleware
    class Rack
      class Proxy
        def initialize(body, &block)
          @body = body
          @block = block
          @closed = false
        end

        def respond_to_missing?(*args)
          return false if args.first && args.first.to_s == 'to_ary'

          @body.respond_to?(*args)
        end

        def method_missing(*args)
          super if args.first && args.first.to_s == 'to_ary'

          if block_given?
            @body.__send__(*args, &Proc.new)
          else
            @body.__send__(*args)
          end
        end

        def close
          return if @closed
          @closed = true

          begin
            @body.close if @body.respond_to? :close
          ensure
            @block.call
          end
        end

        def closed?
          @closed
        end

        def each(*args)
          if block_given?
            @body.each(*args, &Proc.new)
          else
            @body.each(*args)
          end
        end
      end

      def initialize(app)
        @app = app
      end

      # rubocop:disable Metrics/MethodLength
      def call(env)
        origin      = env.fetch('HTTP_X_MNEMOSYNE_ORIGIN', false)
        transaction = env.fetch('HTTP_X_MNEMOSYNE_TRANSACTION') do
          ::SecureRandom.uuid
        end

        trace = ::Mnemosyne::Instrumenter.trace 'app.web.request.rack',
          transaction: transaction,
          origin: origin

        if trace
          trace.start!

          response = @app.call env
          response[2] = Proxy.new(response[2]) { trace.submit }
          response
        else
          @app.call env
        end

      # rubocop:disable Lint/RescueException
      rescue Exception
        trace.submit if trace
        raise
      ensure
        trace.release if trace
      end
    end

    private

    def _uuid
      ::SecureRandom.uuid
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mnemosyne-ruby-1.0.1 lib/mnemosyne/middleware/rack.rb
mnemosyne-ruby-1.0.0 lib/mnemosyne/middleware/rack.rb
mnemosyne-ruby-0.2.0 lib/mnemosyne/middleware/rack.rb
mnemosyne-ruby-0.1.0 lib/mnemosyne/middleware/rack.rb