akita-har_logger 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c6d7e1e45f74876fef25c0840efa3b2715af8db8080411f628a7347e5d06fc9
4
- data.tar.gz: 96f8b9e4f924ec89c93847bfd293ee62b1c91700f15f76e6a5fad8f525e01ad9
3
+ metadata.gz: 3da46a0fcf41a0f5f1f1d88261e7345713fe9487d7a7c4485d85e0ee808c8717
4
+ data.tar.gz: 8857a59375d849bf4c94b47a131c90a950f5bc06da6979c75815d3a6f1c0c4d9
5
5
  SHA512:
6
- metadata.gz: f9f73e60c027ab1756f3c64f4e1687cd5d43c13a938b971972d3ab8cb49252c9506b5127e04cc3207e9b8868b43d3d18992da755ce4ff98ca8a1a74e26e48915
7
- data.tar.gz: 5b9f0a079665e2ce5b79052ddc375843a55144e1913fb19aa423ba355234d342188a9e242f50759f28b335776f1ccf26e985459ad08d6e6a900900380d52de76
6
+ metadata.gz: a2a17be7f105d38c3f75b70bd42dd8982c189a129895ca887333fc3609d2e3f1c362fddffd7a77f97bd43358bc907982ea6b718f96515297cf9b65c437073a0d
7
+ data.tar.gz: 1919850a9c0be28cd22610699a17caec16bafa2b8bf9485ab4b0f7646fbf77a2b4c3294f676033ab6c14258bf52a7ffa08dd9fe3c7e0fe729c90fa228d2acfa2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.1.0)
4
+ akita-har_logger (0.2.0)
5
5
  json (~> 2.3)
6
6
 
7
7
  GEM
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 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.
5
5
 
6
6
 
7
7
  ## Installation
@@ -23,24 +23,59 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
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
+ ```
44
79
 
45
80
 
46
81
  ## Development
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  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.1.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-02 00:00:00.000000000 Z
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.13
85
+ rubygems_version: 3.2.15
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Rails middleware for HAR logging