Class: Akita::HarLogger::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/akita/har_logger.rb

Overview

Logging filter for ‘ActionController`s. TODO: Some amount of code duplication here. Should refactor.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_file_name = nil) ⇒ Filter



83
84
85
86
87
88
89
90
91
92
# File 'lib/akita/har_logger.rb', line 83

def initialize(out_file_name = nil)
  if out_file_name == nil then
    out_file_name = HarLogger.default_file_name
  end

  @entry_queue = HarLogger.get_queue(out_file_name)
rescue => e
  HarLogger.logException("initializing filter", e)
  raise
end

Class Method Details

.install(out_file_name = nil, hook_name = :action_controller) ⇒ Object

Registers an ‘on_load` initializer to add a logging filter to any ActionController that is created.



96
97
98
99
100
# File 'lib/akita/har_logger.rb', line 96

def self.install(out_file_name = nil, hook_name = :action_controller)
  ActiveSupport.on_load(hook_name) do
    around_action Filter.new(out_file_name)
  end
end

Instance Method Details

#around(controller) ⇒ Object

Implements the actual ‘around` filter.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/akita/har_logger.rb', line 103

def around(controller)
  start_time = Time.now

  # Read the request body here, in case there is non-Rack-compliant
  # middleware in the stack that closes the request-body stream on us.
  request_body = controller.response.request.env['rack.input'].read
  controller.response.request.env['rack.input'].rewind  # Be kind.

  yield

  end_time = Time.now
  wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round

  response = controller.response
  request = response.request

  # Patch env with our saved request body.
  saved_input = request.env['rack.input']
  request.env['rack.input'] = StringIO.new request_body

  @entry_queue << (HarEntry.new start_time, wait_time_ms, request.env,
                                response.status, response.headers,
                                [response.body])

  # Be kind and restore the original request-body stream.
  request.env['rack.input'] = saved_input
rescue => e
  HarLogger.logException("handling request", e)
  raise
end