Class: Appsignal::Rack::StreamingListener

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/rack/streaming_listener.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ StreamingListener

Returns a new instance of StreamingListener.


5
6
7
8
# File 'lib/appsignal/rack/streaming_listener.rb', line 5

def initialize(app, options = {})
  Appsignal.logger.debug 'Initializing Appsignal::Rack::StreamingListener'
  @app, @options = app, options
end

Instance Method Details

#call(env) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/appsignal/rack/streaming_listener.rb', line 10

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

#call_with_appsignal_monitoring(env) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/appsignal/rack/streaming_listener.rb', line 18

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.('path', request.path)
        transaction.('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