Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit e74703a

Browse files
authored
Merge pull request #1 from akitasoftware/jed/applicationcontroller-filter
Added a filter for instrumenting ActionController
2 parents e94fa42 + 863bd7e commit e74703a

File tree

2 files changed

+88
-21
lines changed

2 files changed

+88
-21
lines changed

README.md

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Akita HTTP Archive (HAR) logger for Rack applications
1+
# Akita HTTP Archive (HAR) logger for Rack/Rails applications
22

3-
This provides Rack middleware for logging HTTP request–response pairs to a HAR
4-
file.
3+
This provides Rack middleware and a Rails `ActionController` filter for logging
4+
HTTP request–response pairs to a HAR file.
55

66

77
## Installation
@@ -23,24 +23,59 @@ Or install it yourself as:
2323

2424
## Usage
2525

26-
To instrument your Rack application, add `Akita::HarLogger::Middleware` to the
27-
top of your middleware stack. For convenience, you can use
28-
`Akita::HarLogger.instrument`, as follows.
29-
30-
1. In your main `application.rb`, make `Akita::HarLogger` available:
31-
```ruby
32-
require 'akita/har_logger'
33-
```
34-
2. Add the following line to the bottom of your `Rails::Application`
35-
subclass. Specifying the output file is optional; if not given, it defaults
36-
to `akita_trace_{timestamp}.har`.
37-
```ruby
38-
Akita::HarLogger.instrument(config, '/path/to/output/har_file.har')
39-
```
40-
41-
Now, when you run your Rack application, all HTTP requests and responses will
42-
be logged to the HAR file that you've specified. You can then upload this HAR
43-
file to Akita for analysis.
26+
There are two options for instrumenting your Rack/Rails application. The first
27+
is to use the HAR logger as Rack middleware. The second is to use it as a Rails
28+
`ActionController` filter.
29+
30+
Depending on the framework you're using, one or both options may be available
31+
to you. If you are interested in logging RSpec tests, the filter option will
32+
capture traffic for both controller and request specs, whereas the middleware
33+
option only captures request specs.
34+
35+
Once your application is instrumented, when you run the application, HTTP
36+
requests and responses will be logged to the HAR file that you've specified.
37+
You can then upload this HAR file to Akita for analysis.
38+
39+
### Middleware
40+
41+
To instrument with middleware, add `Akita::HarLogger::Middleware` to the top of
42+
your middleware stack. For convenience, you can call
43+
`Akita::HarLogger.instrument` to do this. We recommend adding this call to the
44+
bottom of `config/environments/test.rb` to add the middleware just to your test
45+
environment.
46+
47+
Here is a sample configuration for a test environment that just adds the
48+
instrumentation.
49+
50+
```ruby
51+
Rails.application.configure.do
52+
# Other configuration for the Rails application...
53+
54+
# Put the HAR logger at the top of the middleware stack, and optionally
55+
# give an output HAR file to save your trace. If not specified, this defaults
56+
# to `akita_trace_{timestamp}.har`.
57+
Akita::HarLogger.instrument(config, "akita_trace.har")
58+
end
59+
```
60+
61+
### `ActionController` filter
62+
63+
To instrument with a filter, add an instance of `Akita::HarLogger::Filter` as
64+
an `around_action` filter to your `ActionController` implementation. Here is an
65+
example of a bare-bones `app/controllers/application_controller.rb` with this
66+
instrumentation.
67+
68+
```ruby
69+
class ApplicationController < ActionController::API
70+
include Response
71+
include ExceptionHandler
72+
73+
# Add the HAR logger as an `around_action` filter. Optionally give an output
74+
# HAR file to save your trace. If not specified, this defaults to
75+
# `akita_trace_{timestamp}.har`.
76+
around_action Akita::HarLogger::Filter.new("akita_trace.har")
77+
end
78+
```
4479

4580

4681
## Development

lib/akita/har_logger.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,37 @@ def call(env)
5252
[ status, headers, body ]
5353
end
5454
end
55+
56+
# Logging filter for `ActionController`s.
57+
# TODO: Some amount of code duplication here. Should refactor.
58+
class Filter
59+
def initialize(out_file_name = nil)
60+
if out_file_name == nil then
61+
out_file_name = "akita_trace_#{Time.now.to_i}.har"
62+
end
63+
64+
# This queue is used to ensure that event logging is thread-safe. The
65+
# main thread will enqueue HarEntry objects. The HAR writer thread
66+
# below dequeues these objects and writes them to the output file.
67+
@entry_queue = Queue.new
68+
WriterThread.new out_file_name, @entry_queue
69+
end
70+
71+
def around(controller)
72+
start_time = Time.now
73+
74+
yield
75+
76+
end_time = Time.now
77+
wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round
78+
79+
response = controller.response
80+
request = response.request
81+
82+
@entry_queue << (HarEntry.new start_time, wait_time_ms, request.env,
83+
response.status, response.headers,
84+
[response.body])
85+
end
86+
end
5587
end
5688
end

0 commit comments

Comments
 (0)