rack-request-object-logger 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d73de14f9c2ab393b7e8847378c163a24542616c
4
+ data.tar.gz: d6c4ab97e336f8550f8986fd57f416ca37434c32
5
+ SHA512:
6
+ metadata.gz: f390810d5da7e2b24a8a13f0db342501b6ecf8072e08621b7b55e398f519d7755ec485c97df81f9f4a9341ea667e02c566e815cad4cf3a70122d254272a23d3c
7
+ data.tar.gz: 0ae0aaed1d36641878d5185a459d310379789d311de965b5a3bc3a0f2419585798ea699daeee9560f4af78a0b3dec31f27b6abaf9ee161c1797172abd0b5c34b
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.6
5
+ - 2.3.3
6
+ - 2.4.0
7
+ - jruby-9.1.6.0
8
+ before_install: gem install bundler -v 1.13.7
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ronon@myrtana.sk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-request-object-logger.gemspec
4
+ gemspec
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,112 @@
1
+ [![Build Status](http://img.shields.io/travis/starmammoth/rack-request-object-logger.svg?style=flat-square)](https://travis-ci.org/starmammoth/rack-request-object-logger)
2
+ [![Dependency Status](http://img.shields.io/gemnasium/starmammoth/rack-request-object-logger.svg?style=flat-square)](https://gemnasium.com/starmammoth/rack-request-object-logger)
3
+ [![Code Climate](http://img.shields.io/codeclimate/github/starmammoth/rack-request-object-logger.svg?style=flat-square)](https://codeclimate.com/github/starmammoth/rack-request-object-logger)
4
+ [![Gem Version](http://img.shields.io/gem/v/rack-request-object-logger.svg?style=flat-square)](https://rubygems.org/gems/rack-request-object-logger)
5
+ [![License](http://img.shields.io/:license-apache-blue.svg?style=flat-square)](http://www.apache.org/licenses/LICENSE-2.0.html)
6
+
7
+ # rack-request-object-logger
8
+
9
+ **Human description:** I created this to log all HTTP requests from my Rails application into MySQL database automatically. Then process and search via ElasticSearch.
10
+
11
+ **General concept:** Log HTTP requests via Rack stack to an object. Use any object, because logger uses dependency injection in the constructor. Be independent from Rails.
12
+
13
+ Don't be confused with no commits in months or years. Rack middlewares rarely change. They just work.
14
+
15
+ ## Install gem
16
+
17
+ ```bash
18
+ gem install rack-request-object-logger
19
+ ```
20
+
21
+ ## Gemfile
22
+
23
+ ```ruby
24
+ gem 'rack-request-object-logger'
25
+ ```
26
+
27
+ ## Rubies Support
28
+
29
+ Ruby 2.1+, jruby and rubinius...most likely, because the code is in pure Ruby
30
+
31
+ ## TODO
32
+
33
+ - the code work flawlessly
34
+ - performance specs don't work on jruby, but works on MRI
35
+ - I couldn't install rubinius yet, so it's untested
36
+
37
+ ## Example - logging to SQL database in Rails
38
+
39
+ generate a model for storage
40
+
41
+ ```bash
42
+ $ bin/rails g model Sql::HttpRequest uuid:string data:text
43
+
44
+ ```
45
+
46
+ add JSON serialization
47
+
48
+ ```ruby
49
+ # app/models/sql/http_request.rb
50
+ class Sql::HttpRequest < ApplicationRecord
51
+ serialize :data, JSON
52
+ end
53
+
54
+ ```
55
+
56
+ add automatic logging via initializer
57
+
58
+ ```ruby
59
+ # config/initializers/rack_middlewares.rb
60
+
61
+ Rails.application.config.middleware.use(RackRequestObjectLogger, Sql::HttpRequest)
62
+ ```
63
+
64
+ # Performance
65
+
66
+ To run performance tests on your computer run `rspec performance/`. On my i5 laptop with ActiveRecord it processes and stores 500 logs per second, with dummy class 5000.
67
+
68
+ ## Rails Integration/Awareness
69
+
70
+ The logger sets the UUID of request to match the request ID set by Rails.
71
+
72
+ ## Security considerations
73
+
74
+ The middleware stores all HTTP headers, but strips all *active_dispatch*, *warden* and other stuff. That means HTTP basic auth credentials are stored and also data in query string.
75
+
76
+ I've seen applications sending sensitive data in GET and even POST requests in a query string. Don't do that. Use POST body. Or modify the middleware to filter out them.
77
+
78
+ ## License & Author
79
+
80
+ Copyright 2016 Ivan Stana
81
+
82
+ Licensed under the Apache License, Version 2.0 (the "License");
83
+ you may not use this file except in compliance with the License.
84
+ You may obtain a copy of the License at
85
+
86
+ http://www.apache.org/licenses/LICENSE-2.0 (or see the file `LICENSE`)
87
+
88
+ Unless required by applicable law or agreed to in writing, software
89
+ distributed under the License is distributed on an "AS IS" BASIS,
90
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
91
+ See the License for the specific language governing permissions and
92
+ limitations under the License.
93
+
94
+ ...Enjoy
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/starmammoth/rack-request-object-logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
105
+
106
+ ## Alternatives
107
+
108
+ There may be better alternatives for you:
109
+
110
+ - http://bogomips.org/clogger/
111
+ - http://www.rubydoc.info/github/rack/rack/Rack/CommonLogger
112
+ - https://github.com/mattt/rack-http-logger
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack-request-object-logger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,44 @@
1
+ require 'rack'
2
+ require 'securerandom'
3
+
4
+ class RackRequestObjectLogger
5
+ def initialize(app, request_model)
6
+ @app = app
7
+ @model = request_model
8
+ end
9
+
10
+ def call(env)
11
+ start_time = Time.now.utc
12
+
13
+ app_result = @app.call(env)
14
+
15
+ end_time = Time.now.utc
16
+ logger_object = @model.new
17
+ logger_object.application_server_request_start = start_time
18
+ logger_object.application_server_request_end = end_time
19
+ request = Rack::Request.new(env)
20
+ logger_object.data = request.env.select do |header, value|
21
+ # don't bother with values other than a string
22
+ next if !value.is_a?(String)
23
+ # https://tools.ietf.org/html/rfc3875 CGI 1.1 spec
24
+ # reject cookies
25
+ next if ['HTTP_COOKIE'].include?(header)
26
+
27
+ # 1. predefined
28
+ # 2. $scheme - rack is for HTTP, won't handle other protocols here
29
+ # 3. http headers
30
+ # 4. REQUEST_URI seems to be defined like everywhere (PHP, Python, Rack), but is not a part of CGI spec
31
+ # ORIGINAL_FULLPATH and ORIGINAL_SCRIPT_NAME is Rack specific
32
+ [
33
+ 'AUTH_TYPE', 'CONTENT_LENGTH', 'CONTENT_TYPE', 'GATEWAY_INTERFACE', 'PATH_INFO',
34
+ 'PATH_TRANSLATED', 'QUERY_STRING', 'REMOTE_ADDR', 'REMOTE_HOST', 'REMOTE_IDENT',
35
+ 'REMOTE_USER', 'REQUEST_METHOD', 'SCRIPT_NAME', 'SERVER_NAME', 'SERVER_PORT',
36
+ 'SERVER_PROTOCOL', 'SERVER_SOFTWARE'
37
+ ].include?(header) || ['HTTP', 'HTTPS'].include?(header) || header =~ /\AHTTP_\w+\z/ ||
38
+ ['REQUEST_URI', 'ORIGINAL_FULLPATH', 'ORIGINAL_SCRIPT_NAME'].include?(header)
39
+ end
40
+ logger_object.uid = request.env['action_dispatch.request_id'] || SecureRandom.uuid
41
+ logger_object.save
42
+ app_result
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ class RackRequestObjectLogger
2
+ VERSION = "1.1.0"
3
+ end
@@ -0,0 +1,105 @@
1
+ require_relative 'performance_helper'
2
+ require 'active_record'
3
+
4
+ RSpec.describe "Performance testing with an ActiveRecord class", performance: true do
5
+ include RSpec::Benchmark::Matchers
6
+
7
+ let(:rails5_puma_headers) do
8
+ {
9
+ "rack.version" => [1, 3],
10
+ "rack.errors" => StringIO.new('#<IO:<STDERR>>'),
11
+ "rack.multithread" => true,
12
+ "rack.multiprocess" => false,
13
+ "rack.run_once" => false,
14
+ "rack.hijack?" => true,
15
+ "rack.hijack" => StringIO.new('#<Puma::Client:0x3fdb250638cc @ready=true>'),
16
+ "rack.input" => StringIO.new('#<Puma::NullIO:0x007fb646f1db88>'),
17
+ "rack.url_scheme" => "http",
18
+ "rack.after_reply" => [],
19
+ "rack.session" => StringIO.new('#<ActionDispatch::Request::Session:0x7fb64a073ae8 not yet loaded>'),
20
+ "rack.request.query_string" => "",
21
+ "rack.request.query_hash" => {},
22
+ "rack.request.cookie_hash" => {},
23
+ "SCRIPT_NAME" => "",
24
+ "QUERY_STRING" => "",
25
+ "SERVER_PROTOCOL" => "HTTP/1.1",
26
+ "SERVER_SOFTWARE" => "puma 3.6.2 Sleepy Sunday Serenity",
27
+ "GATEWAY_INTERFACE" => "CGI/1.2",
28
+ "REQUEST_METHOD" => "GET",
29
+ "REQUEST_PATH" => "/lol",
30
+ "REQUEST_URI" => "/lol",
31
+ "SERVER_NAME" => "localhost",
32
+ "SERVER_PORT" => "4000",
33
+ "PATH_INFO" => "/lol",
34
+ "REMOTE_ADDR" => "::1",
35
+ "ROUTES_70210414172620_SCRIPT_NAME" => "",
36
+ "ORIGINAL_FULLPATH" => "/lol",
37
+ "ORIGINAL_SCRIPT_NAME" => "",
38
+ "HTTP_VERSION" => "HTTP/1.1",
39
+ "HTTP_HOST" => "localhost:4000",
40
+ "HTTP_UPGRADE_INSECURE_REQUESTS" => "1",
41
+ "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
42
+ "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0.2 Safari/602.3.12",
43
+ "HTTP_ACCEPT_LANGUAGE" => "en-us",
44
+ "HTTP_ACCEPT_ENCODING" => "gzip, deflate",
45
+ "HTTP_CONNECTION" => "keep-alive",
46
+ "puma.socket" => StringIO.new('#<TCPSocket:fd 36>'),
47
+ "action_dispatch.parameter_filter" => [:password],
48
+ "action_dispatch.redirect_filter" => [],
49
+ "action_dispatch.secret_token" => nil,
50
+ "action_dispatch.secret_key_base" =>
51
+ "1f6009534595b3497d036d11adaea7f0597f26bc153216c8ac961c2831e9585a21982feb8ecd6dd1a25eabfc4850d5e516482107c3d4e0fd46bad26f31ec5055",
52
+ "action_dispatch.show_exceptions" => true,
53
+ "action_dispatch.show_detailed_exceptions" => true,
54
+ "action_dispatch.logger" => StringIO.new('some logger'),
55
+ "action_dispatch.backtrace_cleaner" => StringIO.new('some backtrace silencer'),
56
+ "action_dispatch.key_generator" => StringIO.new('some key generator'),
57
+ "action_dispatch.http_auth_salt" => "http authentication",
58
+ "action_dispatch.signed_cookie_salt" => "signed cookie",
59
+ "action_dispatch.encrypted_cookie_salt" => "encrypted cookie",
60
+ "action_dispatch.encrypted_signed_cookie_salt" => "signed encrypted cookie",
61
+ "action_dispatch.cookies_serializer" => :json,
62
+ "action_dispatch.cookies_digest" => nil,
63
+ "action_dispatch.request_id" => "18f28286-d4d1-4378-b959-189db4a22754",
64
+ "action_dispatch.request.path_parameters" => {:controller=>"greetings", :action=>"index"},
65
+ "action_dispatch.request.content_type" => nil,
66
+ "action_dispatch.request.request_parameters" => {},
67
+ "action_dispatch.request.query_parameters" => {},
68
+ "action_dispatch.request.parameters" => {"controller"=>"greetings", "action"=>"index"},
69
+ "action_dispatch.request.formats" => StringIO.new('some formats'),
70
+ "action_dispatch.request.unsigned_session_cookie" => {}
71
+ }
72
+ end
73
+
74
+ class RequestActiveRecordLog < ActiveRecord::Base
75
+ serialize :headers, JSON
76
+ end
77
+
78
+ class CreateRequestActiveRecordLog < ActiveRecord::Migration[4.2]
79
+ def change
80
+ create_table :request_active_record_logs do |t|
81
+ t.integer :uid
82
+ t.text :data
83
+ t.time :application_server_request_start
84
+ t.time :application_server_request_end
85
+ end
86
+ end
87
+ end
88
+
89
+ let(:app) { proc{ [200, {}, ['Hello, world.']] } }
90
+ let(:stack) { RackRequestObjectLogger.new(app, RequestActiveRecordLog) }
91
+ let(:request) { Rack::MockRequest.new(stack) }
92
+
93
+ after { File.unlink 'active_record_performance.sqlite3' }
94
+
95
+ it 'much fast. wow' do
96
+ stub_const("Rack::MockRequest::DEFAULT_ENV", rails5_puma_headers)
97
+
98
+ ActiveRecord::Base.establish_connection(
99
+ adapter: 'sqlite3',
100
+ database: 'active_record_performance.sqlite3'
101
+ )
102
+ CreateRequestActiveRecordLog.migrate(:up)
103
+ expect { request.get('http://localhost:4000/doge') }.to perform_at_least(500).ips
104
+ end
105
+ end
@@ -0,0 +1,92 @@
1
+ require_relative 'performance_helper'
2
+
3
+ class RequestDummyLog
4
+ attr_accessor :application_server_request_start, :application_server_request_end, :data, :uid
5
+
6
+ def save
7
+ true
8
+ end
9
+ end
10
+
11
+ RSpec.describe "Performance testing with a dummy class", performance: true do
12
+ include RSpec::Benchmark::Matchers
13
+
14
+ let(:rails5_puma_headers) do
15
+ {
16
+ "rack.version" => [1, 3],
17
+ "rack.errors" => StringIO.new('#<IO:<STDERR>>'),
18
+ "rack.multithread" => true,
19
+ "rack.multiprocess" => false,
20
+ "rack.run_once" => false,
21
+ "rack.hijack?" => true,
22
+ "rack.hijack" => StringIO.new('#<Puma::Client:0x3fdb250638cc @ready=true>'),
23
+ "rack.input" => StringIO.new('#<Puma::NullIO:0x007fb646f1db88>'),
24
+ "rack.url_scheme" => "http",
25
+ "rack.after_reply" => [],
26
+ "rack.session" => StringIO.new('#<ActionDispatch::Request::Session:0x7fb64a073ae8 not yet loaded>'),
27
+ "rack.request.query_string" => "",
28
+ "rack.request.query_hash" => {},
29
+ "rack.request.cookie_hash" => {},
30
+ "SCRIPT_NAME" => "",
31
+ "QUERY_STRING" => "",
32
+ "SERVER_PROTOCOL" => "HTTP/1.1",
33
+ "SERVER_SOFTWARE" => "puma 3.6.2 Sleepy Sunday Serenity",
34
+ "GATEWAY_INTERFACE" => "CGI/1.2",
35
+ "REQUEST_METHOD" => "GET",
36
+ "REQUEST_PATH" => "/lol",
37
+ "REQUEST_URI" => "/lol",
38
+ "SERVER_NAME" => "localhost",
39
+ "SERVER_PORT" => "4000",
40
+ "PATH_INFO" => "/lol",
41
+ "REMOTE_ADDR" => "::1",
42
+ "ROUTES_70210414172620_SCRIPT_NAME" => "",
43
+ "ORIGINAL_FULLPATH" => "/lol",
44
+ "ORIGINAL_SCRIPT_NAME" => "",
45
+ "HTTP_VERSION" => "HTTP/1.1",
46
+ "HTTP_HOST" => "localhost:4000",
47
+ "HTTP_UPGRADE_INSECURE_REQUESTS" => "1",
48
+ "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
49
+ "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0.2 Safari/602.3.12",
50
+ "HTTP_ACCEPT_LANGUAGE" => "en-us",
51
+ "HTTP_ACCEPT_ENCODING" => "gzip, deflate",
52
+ "HTTP_CONNECTION" => "keep-alive",
53
+ "puma.socket" => StringIO.new('#<TCPSocket:fd 36>'),
54
+ "action_dispatch.parameter_filter" => [:password],
55
+ "action_dispatch.redirect_filter" => [],
56
+ "action_dispatch.secret_token" => nil,
57
+ "action_dispatch.secret_key_base" =>
58
+ "1f6009534595b3497d036d11adaea7f0597f26bc153216c8ac961c2831e9585a21982feb8ecd6dd1a25eabfc4850d5e516482107c3d4e0fd46bad26f31ec5055",
59
+ "action_dispatch.show_exceptions" => true,
60
+ "action_dispatch.show_detailed_exceptions" => true,
61
+ "action_dispatch.logger" => StringIO.new('some logger'),
62
+ "action_dispatch.backtrace_cleaner" => StringIO.new('some backtrace silencer'),
63
+ "action_dispatch.key_generator" => StringIO.new('some key generator'),
64
+ "action_dispatch.http_auth_salt" => "http authentication",
65
+ "action_dispatch.signed_cookie_salt" => "signed cookie",
66
+ "action_dispatch.encrypted_cookie_salt" => "encrypted cookie",
67
+ "action_dispatch.encrypted_signed_cookie_salt" => "signed encrypted cookie",
68
+ "action_dispatch.cookies_serializer" => :json,
69
+ "action_dispatch.cookies_digest" => nil,
70
+ "action_dispatch.request_id" => "18f28286-d4d1-4378-b959-189db4a22754",
71
+ "action_dispatch.request.path_parameters" => {:controller=>"greetings", :action=>"index"},
72
+ "action_dispatch.request.content_type" => nil,
73
+ "action_dispatch.request.request_parameters" => {},
74
+ "action_dispatch.request.query_parameters" => {},
75
+ "action_dispatch.request.parameters" => {"controller"=>"greetings", "action"=>"index"},
76
+ "action_dispatch.request.formats" => StringIO.new('some formats'),
77
+ "action_dispatch.request.unsigned_session_cookie" => {}
78
+ }
79
+ end
80
+
81
+ let(:app) { proc{ [200, {}, ['Hello, world.']] } }
82
+ let(:stack) { RackRequestObjectLogger.new(app, RequestDummyLog) }
83
+ let(:request) { Rack::MockRequest.new(stack) }
84
+
85
+ let(:logger_object) { RequestDummyLog.new }
86
+ before { allow(RequestDummyLog).to receive(:new).and_return(logger_object) }
87
+
88
+ it 'much fast. wow' do
89
+ stub_const("Rack::MockRequest::DEFAULT_ENV", rails5_puma_headers)
90
+ expect { request.get('http://localhost:4000/doge') }.to perform_at_least(5000).ips
91
+ end
92
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ require 'rspec-benchmark'
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+ require "rack-request-object-logger"
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack-request-object-logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-request-object-logger"
8
+ spec.version = RackRequestObjectLogger::VERSION
9
+ spec.authors = ["Ivan Stana"]
10
+ spec.email = ["ronon@myrtana.sk"]
11
+
12
+ spec.summary = %q{Log HTTP requests via Rack stack to an object. You can use any object.}
13
+ spec.description = %q{Log HTTP requests via Rack stack to an object. You can use any object, i.e. ActiveRecord model}
14
+ spec.homepage = "https://github.com/starmammoth/rack-request-object-logger"
15
+ spec.license = "Apache 2.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rack"
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.5"
28
+ spec.add_development_dependency "rspec-benchmark"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "activerecord", '>= 4.2'
31
+ spec.add_development_dependency "sqlite3" unless RUBY_PLATFORM == 'java'
32
+ spec.add_development_dependency "activerecord-jdbcsqlite3-adapter" if RUBY_PLATFORM == 'java'
33
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-request-object-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Stana
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ name: rack
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.13'
33
+ name: bundler
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '10.0'
47
+ name: rake
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.5'
61
+ name: rspec
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ name: rspec-benchmark
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: pry
90
+ prerelease: false
91
+ type: :development
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '4.2'
103
+ name: activerecord
104
+ prerelease: false
105
+ type: :development
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ - !ruby/object:Gem::Dependency
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ name: activerecord-jdbcsqlite3-adapter
118
+ prerelease: false
119
+ type: :development
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Log HTTP requests via Rack stack to an object. You can use any object, i.e. ActiveRecord model
126
+ email:
127
+ - ronon@myrtana.sk
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - LICENSE
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - lib/rack-request-object-logger.rb
143
+ - lib/rack-request-object-logger/version.rb
144
+ - performance/active_record_model_spec.rb
145
+ - performance/in_memory_model_spec.rb
146
+ - performance/performance_helper.rb
147
+ - rack-request-object-logger.gemspec
148
+ homepage: https://github.com/starmammoth/rack-request-object-logger
149
+ licenses:
150
+ - Apache 2.0
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.6.8
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Log HTTP requests via Rack stack to an object. You can use any object.
172
+ test_files: []