Module: Contrast::Framework::Rails::Patch::ActionControllerLiveBuffer

Defined in:
lib/contrast/framework/rails/patch/action_controller_live_buffer.rb

Overview

This class acts as our patch into the ActionController::Live::Buffer class, allowing us to track the close event on streamed responses.

Class Method Summary collapse

Class Method Details

.instrument ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/contrast/framework/rails/patch/action_controller_live_buffer.rb', line 26

def instrument
  @_instrument ||= begin
    ::ActionController::Live::Buffer.class_eval do
      # normally pre->in->post filters are applied however, in a streamed response we can run into a case
      # where it's pre -> in -> post -> more infilters in order to submit anything found during the
      # infilters after the response has been written we need to explicitly send them
      alias_method(:cs__close, :close)
      def close
        Contrast::Framework::Rails::Patch::ActionControllerLiveBuffer.send_messages
        cs__close
      end
    end
    true
  end
end

.send_messages ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/contrast/framework/rails/patch/action_controller_live_buffer.rb', line 14

def send_messages
  return unless (context = Contrast::Agent::REQUEST_TRACKER.current)

  [
    context.observed_route
  ].each do |event|
    Contrast::Agent.reporter&.send_event_immediately(event)
  end

  Contrast::Agent.reporter&.send_event_immediately(context.activity)
end