akita-har_logger 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +56 -21
- data/lib/akita/har_logger.rb +32 -0
- data/lib/akita/har_logger/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3da46a0fcf41a0f5f1f1d88261e7345713fe9487d7a7c4485d85e0ee808c8717
|
4
|
+
data.tar.gz: 8857a59375d849bf4c94b47a131c90a950f5bc06da6979c75815d3a6f1c0c4d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2a17be7f105d38c3f75b70bd42dd8982c189a129895ca887333fc3609d2e3f1c362fddffd7a77f97bd43358bc907982ea6b718f96515297cf9b65c437073a0d
|
7
|
+
data.tar.gz: 1919850a9c0be28cd22610699a17caec16bafa2b8bf9485ab4b0f7646fbf77a2b4c3294f676033ab6c14258bf52a7ffa08dd9fe3c7e0fe729c90fa228d2acfa2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# Akita HTTP Archive (HAR) logger for Rack applications
|
1
|
+
# Akita HTTP Archive (HAR) logger for Rack/Rails applications
|
2
2
|
|
3
|
-
This provides Rack middleware
|
4
|
-
file.
|
3
|
+
This provides Rack middleware and a Rails `ActionController` filter for logging
|
4
|
+
HTTP request–response pairs to a HAR file.
|
5
5
|
|
6
6
|
|
7
7
|
## Installation
|
@@ -23,24 +23,59 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
`
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
+
```
|
44
79
|
|
45
80
|
|
46
81
|
## Development
|
data/lib/akita/har_logger.rb
CHANGED
@@ -52,5 +52,37 @@ module Akita
|
|
52
52
|
[ status, headers, body ]
|
53
53
|
end
|
54
54
|
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
|
55
87
|
end
|
56
88
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: akita-har_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jed Liu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04-
|
11
|
+
date: 2021-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
requirements: []
|
85
|
-
rubygems_version: 3.2.
|
85
|
+
rubygems_version: 3.2.15
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: Rails middleware for HAR logging
|