Sha256: 688d8cd8fd2a957b162d9ec6751bd55c657494bcabe272958965e6ba1716a0a4

Contents?: true

Size: 1.89 KB

Versions: 108

Compression:

Stored size: 1.89 KB

Contents

# Appsignal module that tracks exceptions in Streaming rack responses.
module Appsignal
  module Rack
    class StreamingListener
      def initialize(app, options = {})
        Appsignal.logger.debug 'Initializing Appsignal::Rack::StreamingListener'
        @app, @options = app, options
      end

      def call(env)
        if Appsignal.active?
          call_with_appsignal_monitoring(env)
        else
          @app.call(env)
        end
      end

      def call_with_appsignal_monitoring(env)
        request = ::Rack::Request.new(env)
        transaction = Appsignal::Transaction.create(
          SecureRandom.uuid,
          Appsignal::Transaction::HTTP_REQUEST,
          request
        )

        # Instrument a `process_action`, to set params/action name
        status, headers, body =
          ActiveSupport::Notifications.instrument('process_action.rack') do
            begin
              @app.call(env)
            rescue Exception => e
              transaction.set_error(e)
              raise e
            ensure
              transaction.set_action(env['appsignal.action'])
              transaction.set_metadata('path', request.path)
              transaction.set_metadata('method', request.request_method)
              transaction.set_http_or_background_queue_start
            end
          end

        # Wrap the result body with our StreamWrapper
        [status, headers, StreamWrapper.new(body, transaction)]
      end
    end
  end

  class StreamWrapper
    def initialize(stream, transaction)
       @stream      = stream
       @transaction = transaction
    end

    def each
      @stream.each { |c| yield(c) }
    rescue Exception => e
      @transaction.set_error(e); raise e
    end

    def close
      @stream.close if @stream.respond_to?(:close)
    rescue Exception => e
      @transaction.set_error(e); raise e
    ensure
      Appsignal::Transaction.complete_current!
    end
  end
end

Version data entries

108 entries across 108 versions & 1 rubygems

Version Path
appsignal-1.3.5 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.5.beta.1 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.4 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.3 lib/appsignal/rack/streaming_listener.rb
appsignal-1.4.0.alpha.2 lib/appsignal/rack/streaming_listener.rb
appsignal-1.4.0.alpha.1 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.2 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.1 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.0 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.0.beta.3 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.0.beta.2 lib/appsignal/rack/streaming_listener.rb
appsignal-1.3.0.beta.1 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.5 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.4 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.4.beta.4 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.4.beta.3 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.4.beta.2 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.4.beta.1 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.3 lib/appsignal/rack/streaming_listener.rb
appsignal-1.2.3.beta.3 lib/appsignal/rack/streaming_listener.rb