akita-har_logger 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +36 -0
- data/LICENSE +201 -0
- data/README.md +65 -0
- data/Rakefile +4 -0
- data/akita-har_logger.gemspec +37 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/akita/har_logger.rb +56 -0
- data/lib/akita/har_logger/har_entry.rb +39 -0
- data/lib/akita/har_logger/har_utils.rb +19 -0
- data/lib/akita/har_logger/http_request.rb +128 -0
- data/lib/akita/har_logger/http_response.rb +127 -0
- data/lib/akita/har_logger/version.rb +7 -0
- data/lib/akita/har_logger/writer_thread.rb +69 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6c6d7e1e45f74876fef25c0840efa3b2715af8db8080411f628a7347e5d06fc9
|
4
|
+
data.tar.gz: 96f8b9e4f924ec89c93847bfd293ee62b1c91700f15f76e6a5fad8f525e01ad9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9f73e60c027ab1756f3c64f4e1687cd5d43c13a938b971972d3ab8cb49252c9506b5127e04cc3207e9b8868b43d3d18992da755ce4ff98ca8a1a74e26e48915
|
7
|
+
data.tar.gz: 5b9f0a079665e2ce5b79052ddc375843a55144e1913fb19aa423ba355234d342188a9e242f50759f28b335776f1ccf26e985459ad08d6e6a900900380d52de76
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
akita-har_logger (0.1.0)
|
5
|
+
json (~> 2.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
json (2.5.1)
|
12
|
+
rake (13.0.3)
|
13
|
+
rspec (3.10.0)
|
14
|
+
rspec-core (~> 3.10.0)
|
15
|
+
rspec-expectations (~> 3.10.0)
|
16
|
+
rspec-mocks (~> 3.10.0)
|
17
|
+
rspec-core (3.10.1)
|
18
|
+
rspec-support (~> 3.10.0)
|
19
|
+
rspec-expectations (3.10.1)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.10.0)
|
22
|
+
rspec-mocks (3.10.2)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.10.0)
|
25
|
+
rspec-support (3.10.2)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
x86_64-linux
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
akita-har_logger!
|
32
|
+
rake (~> 13.0)
|
33
|
+
rspec (~> 3.10)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
2.2.15
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Akita HTTP Archive (HAR) logger for Rack applications
|
2
|
+
|
3
|
+
This provides Rack middleware for logging HTTP request–response pairs to a HAR
|
4
|
+
file.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'akita-har_logger'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install akita-har_logger
|
22
|
+
|
23
|
+
|
24
|
+
## Usage
|
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.
|
44
|
+
|
45
|
+
|
46
|
+
## Development
|
47
|
+
|
48
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can
|
49
|
+
also run `bin/console` for an interactive prompt that will allow you to
|
50
|
+
experiment.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
53
|
+
release a new version, update the version number in `version.rb`, and then run
|
54
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
55
|
+
git commits and the created tag, and push the `.gem` file to
|
56
|
+
[rubygems.org](https://rubygems.org).
|
57
|
+
|
58
|
+
|
59
|
+
## Implementation notes
|
60
|
+
|
61
|
+
References used when building this:
|
62
|
+
* https://ieftimov.com/post/writing-rails-middleware/
|
63
|
+
* https://github.com/rack/rack/blob/master/SPEC.rdoc
|
64
|
+
* https://w3c.github.io/web-performance/specs/HAR/Overview.html
|
65
|
+
* http://www.softwareishard.com/blog/har-12-spec/
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/akita/har_logger/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'akita-har_logger'
|
7
|
+
spec.version = Akita::HarLogger::VERSION
|
8
|
+
spec.authors = ['Jed Liu']
|
9
|
+
spec.email = ['jed@akitasoftware.com']
|
10
|
+
|
11
|
+
spec.summary = 'Rails middleware for HAR logging'
|
12
|
+
spec.description = 'Middleware instrumentation for logging HTTP ' \
|
13
|
+
'request-response pairs to a HAR file.'
|
14
|
+
spec.homepage = 'https://akitasoftware.com/'
|
15
|
+
spec.license = 'Apache-2.0'
|
16
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
17
|
+
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
19
|
+
spec.metadata['source_code_uri'] =
|
20
|
+
'https://github.com/akitasoftware/akita-rails-har-logger'
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
24
|
+
# into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f|
|
27
|
+
f.match(%r{\A(?:test|spec|features)/})
|
28
|
+
}
|
29
|
+
end
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_dependency 'json', '~> 2.3'
|
35
|
+
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
37
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "akita/har_logger"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'har_logger/har_entry'
|
4
|
+
require_relative 'har_logger/version'
|
5
|
+
require_relative 'har_logger/writer_thread'
|
6
|
+
|
7
|
+
module Akita
|
8
|
+
module HarLogger
|
9
|
+
# Adds HAR-logging instrumentation to a Rails application by adding to the
|
10
|
+
# top of the middleware stack.
|
11
|
+
#
|
12
|
+
# Params:
|
13
|
+
# +config+:: the +Rails::Application::Configuration+ associated with the
|
14
|
+
# Rails application being instrumented.
|
15
|
+
# +har_file_name:: the name of the HAR file to be produced. If the file
|
16
|
+
# exists, it will be overwritten.
|
17
|
+
def self.instrument(config, har_file_name = nil)
|
18
|
+
config.middleware.unshift(Middleware, har_file_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Logs HTTP request-response pairs to a HAR file.
|
22
|
+
#
|
23
|
+
# Params:
|
24
|
+
# +app+:: the application to log.
|
25
|
+
# +out_file_name+:: the name of the HAR file to be produced. If the file
|
26
|
+
# exists, it will be overwritten.
|
27
|
+
class Middleware
|
28
|
+
def initialize(app, out_file_name = nil)
|
29
|
+
@app = app
|
30
|
+
|
31
|
+
if out_file_name == nil then
|
32
|
+
out_file_name = "akita_trace_#{Time.now.to_i}.har"
|
33
|
+
end
|
34
|
+
|
35
|
+
# This queue is used to ensure that event logging is thread-safe. The
|
36
|
+
# main thread will enqueue HarEntry objects. The HAR writer thread
|
37
|
+
# below dequeues these objects and writes them to the output file.
|
38
|
+
@entry_queue = Queue.new
|
39
|
+
WriterThread.new out_file_name, @entry_queue
|
40
|
+
end
|
41
|
+
|
42
|
+
def call(env)
|
43
|
+
start_time = Time.now
|
44
|
+
status, headers, body = @app.call(env)
|
45
|
+
end_time = Time.now
|
46
|
+
|
47
|
+
wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round
|
48
|
+
|
49
|
+
@entry_queue << (HarEntry.new start_time, wait_time_ms, env, status,
|
50
|
+
headers, body)
|
51
|
+
|
52
|
+
[ status, headers, body ]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require_relative 'http_request'
|
5
|
+
require_relative 'http_response'
|
6
|
+
|
7
|
+
module Akita
|
8
|
+
module HarLogger
|
9
|
+
# Encapsulates an HTTP request-response pair.
|
10
|
+
class HarEntry
|
11
|
+
attr_reader :request, :response
|
12
|
+
|
13
|
+
# Params:
|
14
|
+
# +start_time+:: a Time object representing the request's start time.
|
15
|
+
# +env+:: the request's HTTP environment.
|
16
|
+
# +status+:: the response's HTTP status code.
|
17
|
+
# +headers+:: the response's HTTP headers.
|
18
|
+
# +body+:: the response's HTTP body.
|
19
|
+
def initialize(start_time, wait_time_ms, env, status, headers, body)
|
20
|
+
@self = {
|
21
|
+
startedDateTime: start_time.strftime('%FT%T.%L%:z'),
|
22
|
+
time: wait_time_ms,
|
23
|
+
request: (HttpRequest.new env),
|
24
|
+
response: (HttpResponse.new env, status, headers, body),
|
25
|
+
cache: {}, # Not applicable to server-side logging.
|
26
|
+
timings: {
|
27
|
+
send: 0, # Mandatory, but not applicable to server-side logging.
|
28
|
+
wait: wait_time_ms,
|
29
|
+
receive: 0, # Mandatory, but not applicable to server-side logging.
|
30
|
+
},
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json(*args)
|
35
|
+
@self.to_json(*args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Akita
|
4
|
+
module HarLogger
|
5
|
+
class HarUtils
|
6
|
+
# Converts a Hash into a list of Hash objects. Each entry in the given
|
7
|
+
# Hash will be represented in the output by a Hash object that maps
|
8
|
+
# 'name' to the entry's key and 'value' to the entry's value.
|
9
|
+
def self.hashToList(hash)
|
10
|
+
hash.reduce([]) { |accum, (k, v)|
|
11
|
+
accum.append({
|
12
|
+
name: k,
|
13
|
+
value: v,
|
14
|
+
})
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require_relative 'har_utils'
|
5
|
+
|
6
|
+
module Akita
|
7
|
+
module HarLogger
|
8
|
+
class HttpRequest
|
9
|
+
# Produces an HttpRequest from a request's HTTP environment.
|
10
|
+
def initialize(env)
|
11
|
+
req = Rack::Request.new env
|
12
|
+
|
13
|
+
@self = {
|
14
|
+
method: getMethod(env),
|
15
|
+
url: req.url,
|
16
|
+
httpVersion: getHttpVersion(env),
|
17
|
+
cookies: getCookies(env),
|
18
|
+
headers: getHeaders(env),
|
19
|
+
queryString: getQueryString(env),
|
20
|
+
headersSize: getHeadersSize(env),
|
21
|
+
bodySize: getBodySize(env),
|
22
|
+
}
|
23
|
+
|
24
|
+
# Augment with post data if we have any.
|
25
|
+
postData = getPostData(env)
|
26
|
+
if postData != nil then
|
27
|
+
@self[:postData] = postData
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json(*args)
|
32
|
+
@self.to_json(*args)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Obtains the client's request method from an HTTP environment.
|
36
|
+
def getMethod(env)
|
37
|
+
(Rack::Request.new env).request_method
|
38
|
+
end
|
39
|
+
|
40
|
+
# Obtains the client-requested HTTP version from an HTTP environment.
|
41
|
+
def getHttpVersion(env)
|
42
|
+
# The environment doesn't have HTTP_VERSION when running with `rspec`;
|
43
|
+
# assume HTTP/1.1 when this happens. We don't return nil, so we can
|
44
|
+
# calculate the size of the headers.
|
45
|
+
env.key?('HTTP_VERSION') ? env['HTTP_VERSION'] : 'HTTP/1.1'
|
46
|
+
end
|
47
|
+
|
48
|
+
# Builds a list of cookie objects from an HTTP environment.
|
49
|
+
def getCookies(env)
|
50
|
+
req = Rack::Request::new env
|
51
|
+
HarUtils.hashToList req.cookies
|
52
|
+
end
|
53
|
+
|
54
|
+
# Builds a list of headers from an HTTP environment.
|
55
|
+
def getHeaders(env)
|
56
|
+
# HTTP headers in the environment can be identified with the "HTTP_"
|
57
|
+
# prefix. Filter for these. In the resulting map, rewrite keys of the
|
58
|
+
# form "HTTP_FOO_BAR_BAZ" into "Foo-Bar-Baz", and convert into an
|
59
|
+
# array.
|
60
|
+
HarUtils.hashToList (
|
61
|
+
env.select { |k,v| k.start_with? 'HTTP_' }.
|
62
|
+
transform_keys { |k|
|
63
|
+
k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
|
64
|
+
}
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Builds a list of query parameters from an HTTP environment.
|
69
|
+
def getQueryString(env)
|
70
|
+
req = Rack::Request::new env
|
71
|
+
paramMap = Rack::Utils.parse_nested_query req.query_string
|
72
|
+
HarUtils.hashToList paramMap
|
73
|
+
end
|
74
|
+
|
75
|
+
# Obtains the posted data from an HTTP environment.
|
76
|
+
def getPostData(env)
|
77
|
+
if env.key?('CONTENT_TYPE') && env['CONTENT_TYPE'] then
|
78
|
+
result = { mimeType: env['CONTENT_TYPE'] }
|
79
|
+
|
80
|
+
# Populate 'params' if we have URL-encoded parameters. Otherwise,
|
81
|
+
# populate 'text.
|
82
|
+
req = Rack::Request.new env
|
83
|
+
if env['CONTENT_TYPE'] == 'application/x-www-form-urlencoded' then
|
84
|
+
# Decoded parameters can be found as a map in req.params. Convert
|
85
|
+
# this map into an array.
|
86
|
+
#
|
87
|
+
# XXX Spec has space for files, but are file uploads ever
|
88
|
+
# URL-encoded?
|
89
|
+
result[:params] = HarUtils.hashToList req.params
|
90
|
+
else
|
91
|
+
result[:text] = req.body.string
|
92
|
+
end
|
93
|
+
|
94
|
+
result
|
95
|
+
else
|
96
|
+
nil
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def getHeadersSize(env)
|
101
|
+
# XXX This seems to under-count, compared to a HAR produced by Firefox.
|
102
|
+
|
103
|
+
# Count the number of bytes needed to produce the first line of the
|
104
|
+
# request (HTTP method, full path, HTTP version, CRLF). For example,
|
105
|
+
#
|
106
|
+
# GET /index.html?foo=bar&baz=qux HTTP/1.1<CR><LF>
|
107
|
+
req = Rack::Request::new env
|
108
|
+
line_length =
|
109
|
+
getMethod(env).length + 1
|
110
|
+
+ req.fullpath.length + 1
|
111
|
+
+ getHttpVersion(env).length + 2
|
112
|
+
|
113
|
+
# Add the size of the headers. Add 2 to the starting value to account
|
114
|
+
# for the CRLF on the blank line.
|
115
|
+
getHeaders(env).reduce(line_length + 2) { |accum, entry|
|
116
|
+
# Header-Name: header value<CR><LF>
|
117
|
+
accum + entry[:name].length + 2 + entry[:value].length + 2
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
# Obtains the size of the request body from an HTTP environment.
|
122
|
+
def getBodySize(env)
|
123
|
+
# Assume no content if Content-Length header was not provided.
|
124
|
+
env.key?('CONTENT_LENGTH') ? env['CONTENT_LENGTH'].to_i : 0
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require_relative 'har_utils'
|
5
|
+
|
6
|
+
module Akita
|
7
|
+
module HarLogger
|
8
|
+
class HttpResponse
|
9
|
+
def initialize(env, status, headers, body)
|
10
|
+
@self = {
|
11
|
+
status: status,
|
12
|
+
statusText: getStatusText(status),
|
13
|
+
httpVersion: getHttpVersion(env),
|
14
|
+
cookies: getCookies(headers),
|
15
|
+
headers: (HarUtils.hashToList headers),
|
16
|
+
content: getContent(headers, body),
|
17
|
+
redirectURL: getRedirectUrl(headers),
|
18
|
+
headersSize: getHeadersSize(env, status, headers),
|
19
|
+
bodySize: getBodySize(body),
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_json(*args)
|
24
|
+
@self.to_json(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Obtains the status text corresponding to a status code.
|
28
|
+
def getStatusText(status)
|
29
|
+
Rack::Utils::HTTP_STATUS_CODES[status]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Obtains the HTTP version in the response.
|
33
|
+
def getHttpVersion(env)
|
34
|
+
# XXX Assume the server replies with the same HTTP version as the
|
35
|
+
# XXX request. This seems to hold true empirically.
|
36
|
+
|
37
|
+
# The environment doesn't have HTTP_VERSION when running with `rspec`;
|
38
|
+
# assume HTTP/1.1 when this happens. We don't return nil, so we can
|
39
|
+
# calculate the size of the headers.
|
40
|
+
env.key?('HTTP_VERSION') ? env['HTTP_VERSION'] : 'HTTP/1.1'
|
41
|
+
end
|
42
|
+
|
43
|
+
def getCookies(headers)
|
44
|
+
result = []
|
45
|
+
headers.each { |k, v|
|
46
|
+
if "Set-Cookie".casecmp(k) != 0 then next end
|
47
|
+
|
48
|
+
# Couldn't find a great library for parsing Set-Cookie headers, so
|
49
|
+
# let's roll our own...
|
50
|
+
#
|
51
|
+
# According to RFC 6265, the value of Set-Cookie has the format
|
52
|
+
# "cookieName=cookieValue", optionally followed by a semicolon and
|
53
|
+
# attribute-value pairs. The cookieValue can be optionally enclosed
|
54
|
+
# in double quotes. Neither cookieName nor cookieValue can contain
|
55
|
+
# double quotes, semicolons, or equal signs.
|
56
|
+
match = /^([^=]*)=([^;]*)(|;.*)$/.match(v)
|
57
|
+
if !match then next end
|
58
|
+
|
59
|
+
cookie_name = match[1]
|
60
|
+
cookie_value = match[2]
|
61
|
+
|
62
|
+
# Strip quotation marks from the value if they are present.
|
63
|
+
match = /^"(.*)"$/.match(cookie_value)
|
64
|
+
if match then cookie_value = match[1] end
|
65
|
+
|
66
|
+
result << {
|
67
|
+
name: cookie_name,
|
68
|
+
value: cookie_value,
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
result
|
73
|
+
end
|
74
|
+
|
75
|
+
def getContent(headers, body)
|
76
|
+
# XXX Handle compression & encoding.
|
77
|
+
|
78
|
+
text = +""
|
79
|
+
body.each { |part|
|
80
|
+
# XXX Figure out how to join together multi-part bodies.
|
81
|
+
text << part;
|
82
|
+
}
|
83
|
+
|
84
|
+
{
|
85
|
+
size: getBodySize(body),
|
86
|
+
|
87
|
+
# XXX What to use when no Content-Type is given?
|
88
|
+
mimeType: headers['Content-Type'],
|
89
|
+
|
90
|
+
text: text,
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
def getRedirectUrl(headers)
|
95
|
+
# Use the "Location" header if it exists. Otherwise, based on some HAR
|
96
|
+
# examples found online, it looks like an empty string is used.
|
97
|
+
headers.key?('Location') ? headers['Location'] : ''
|
98
|
+
end
|
99
|
+
|
100
|
+
def getHeadersSize(env, status, headers)
|
101
|
+
# XXX This seems to under-count, compared to a HAR produced by Firefox.
|
102
|
+
|
103
|
+
# Count the number of bytes needed to produce the first line of the
|
104
|
+
# response (HTTP version, status code, status text, CRLF). For example,
|
105
|
+
#
|
106
|
+
# HTTP/1.1 404 Not Found<CR><LF>
|
107
|
+
status_length =
|
108
|
+
getHttpVersion(env).length + 1
|
109
|
+
+ status.to_s.length + 1
|
110
|
+
+ getStatusText(status).length + 2
|
111
|
+
|
112
|
+
# Add the size of the headers. Add 2 to the starting value to account
|
113
|
+
# for the CRLF on the blank line.
|
114
|
+
headers.reduce(status_length + 2) { |accum, (k, v)|
|
115
|
+
# Header-Name: header value<CR><LF>
|
116
|
+
accum + k.length + 2 + v.length + 2
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
120
|
+
def getBodySize(body)
|
121
|
+
length = 0
|
122
|
+
body.each { |part| length += part.bytesize }
|
123
|
+
length
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Akita
|
6
|
+
module HarLogger
|
7
|
+
# A thread that consumes HarEntry objects from a queue and writes them to a
|
8
|
+
# file.
|
9
|
+
#
|
10
|
+
# Params:
|
11
|
+
# +out_file_name+:: the name of the HAR file to be produced.
|
12
|
+
# +entry_queue+:: the queue from which to consume HAR entries.
|
13
|
+
class WriterThread
|
14
|
+
def initialize(out_file_name, entry_queue)
|
15
|
+
# This mutex is used to ensure the entire output is written before the
|
16
|
+
# application shuts down.
|
17
|
+
shutdown_mutex = Mutex.new
|
18
|
+
|
19
|
+
Thread.new do
|
20
|
+
shutdown_mutex.synchronize {
|
21
|
+
File.open(out_file_name, 'w') { |f|
|
22
|
+
# Produce a preamble.
|
23
|
+
f.write <<~EOF.chomp
|
24
|
+
{
|
25
|
+
"log": {
|
26
|
+
"version": "1.2",
|
27
|
+
"creator": {
|
28
|
+
"name": "Akita HAR logger for Ruby",
|
29
|
+
"version": "1.0.0"
|
30
|
+
},
|
31
|
+
"entries": [
|
32
|
+
EOF
|
33
|
+
|
34
|
+
first_entry = true
|
35
|
+
|
36
|
+
loop do
|
37
|
+
entry = entry_queue.pop
|
38
|
+
if entry == nil then break end
|
39
|
+
|
40
|
+
# Emit comma separator if needed.
|
41
|
+
f.puts (first_entry ? '' : ',')
|
42
|
+
first_entry = false
|
43
|
+
|
44
|
+
# Emit the dequeued entry.
|
45
|
+
f.write JSON.generate(entry)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Produce the epilogue.
|
49
|
+
f.write <<~EOF
|
50
|
+
|
51
|
+
]
|
52
|
+
}
|
53
|
+
}
|
54
|
+
EOF
|
55
|
+
}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
# Finish outputting the HAR file when the application shuts down.
|
60
|
+
at_exit do
|
61
|
+
# Signal to the consumer that this is the end of the entry stream and
|
62
|
+
# wait for the consumer to terminate.
|
63
|
+
entry_queue << nil
|
64
|
+
shutdown_mutex.synchronize {}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: akita-har_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jed Liu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.10'
|
41
|
+
description: Middleware instrumentation for logging HTTP request-response pairs to
|
42
|
+
a HAR file.
|
43
|
+
email:
|
44
|
+
- jed@akitasoftware.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- akita-har_logger.gemspec
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/akita/har_logger.rb
|
58
|
+
- lib/akita/har_logger/har_entry.rb
|
59
|
+
- lib/akita/har_logger/har_utils.rb
|
60
|
+
- lib/akita/har_logger/http_request.rb
|
61
|
+
- lib/akita/har_logger/http_response.rb
|
62
|
+
- lib/akita/har_logger/version.rb
|
63
|
+
- lib/akita/har_logger/writer_thread.rb
|
64
|
+
homepage: https://akitasoftware.com/
|
65
|
+
licenses:
|
66
|
+
- Apache-2.0
|
67
|
+
metadata:
|
68
|
+
homepage_uri: https://akitasoftware.com/
|
69
|
+
source_code_uri: https://github.com/akitasoftware/akita-rails-har-logger
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.4.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.2.13
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Rails middleware for HAR logging
|
89
|
+
test_files: []
|