rack-request-object-logger 2.0.1 → 4.0.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
- SHA1:
3
- metadata.gz: 68113b8a402b14c8a318bf7f4d741b1cdea58b5d
4
- data.tar.gz: f3b79f22b3c49915e445664b20bac57d603daebb
2
+ SHA256:
3
+ metadata.gz: f2df7642c85cddc3a1947c7d3b7191fcbee553877a19ca47ffd9b3a46a5bc0fa
4
+ data.tar.gz: 571cf74cb5555caf7d620c109e9b037cbf3a6330a44b354e44e5b1fd2c89b3b6
5
5
  SHA512:
6
- metadata.gz: a5df959bfad13fd9df2acb70480fc5e8f59d7d058c5e0e6a250838eea9df3284240a8cccd3fce58889d542a9686ef8a19b7f7e2858edfe854333a64f25f2c5bc
7
- data.tar.gz: 71f93253e15c2b0680be1ae37ceb318c4975260cc26046cccaff25df0813a17cf68ceb96bbaf8fa89df14f6966963dc97cab28ef964069118087597a2474d121
6
+ metadata.gz: b43ceb51e1f0db58d96b83988a925e790b9dd5051acd203e980c0b4b3962976669f23518129c4d155619ad25f100f6d3c71cd0ec655657d5d7031370b24a4c0e
7
+ data.tar.gz: 12254a4a8c854a1da0583c5a329e229f5169f8b538023732c693b7a8ca2abc3db7a490c6914c7950946c544e5933930179499e4d1e2d42f9a2b765b9262841b5
@@ -0,0 +1,26 @@
1
+ name: github-actions-rails
2
+
3
+ on:
4
+ pull_request:
5
+ branches: ['*']
6
+ push:
7
+ branches: [master]
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', head, truffleruby, truffleruby-head]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - name: Set up Ruby ${{ matrix.ruby }}
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: ${{ matrix.ruby }}
21
+ bundler: default
22
+ bundler-cache: true
23
+ - name: Run tests
24
+ env:
25
+ RAILS_ENV: test
26
+ run: bundle exec rake
data/README.md CHANGED
@@ -1,15 +1,12 @@
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
- [![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)
1
+ ![example workflow](https://github.com/istana/rack-request-object-logger/actions/workflows/ruby.yml/badge.svg)
3
2
  [![Gem Version](http://img.shields.io/gem/v/rack-request-object-logger.svg?style=flat-square)](https://rubygems.org/gems/rack-request-object-logger)
4
3
  [![License](http://img.shields.io/:license-apache-blue.svg?style=flat-square)](http://www.apache.org/licenses/LICENSE-2.0.html)
5
4
 
6
5
  # rack-request-object-logger
7
6
 
8
- **Human description:** I created this to log all HTTP requests from my Rails application into MySQL database automatically. Then process and search via ElasticSearch.
7
+ The project is a Rack middleware to automatically log a HTTP request to a custom object.
9
8
 
10
- **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.
11
-
12
- Don't be confused with no commits in months or years. Rack middlewares rarely change. They just work.
9
+ There might be no commits in months or years. Rack middlewares rarely change.
13
10
 
14
11
  ## Install gem
15
12
 
@@ -25,39 +22,30 @@ gem 'rack-request-object-logger'
25
22
 
26
23
  ## Rubies Support
27
24
 
28
- Ruby 2.1+, jRuby and Rubinius 3.69+
25
+ Tested with Matz Ruby 2.7, 3.0, 3.1, 3.2, 3.3, head, and ruffleruby, truffleruby-head.
26
+
27
+ Should work also with jRuby, just sqlite3 adapter supports only Rails 7 and tests require a more recent Rails.
29
28
 
30
29
  ## Roadmap
31
30
 
32
31
  While the code works flawlessly
33
32
 
34
- - performance specs don't work on jruby, but works on MRI and Rubinius 3.60
33
+ Bug: I learned at EuRuKo 2018 that my implementation of timings is wrong and not very accurate.
35
34
 
36
35
  ## Example - logging to SQL database in Rails
37
36
 
38
37
  generate a model for storage
39
38
 
40
39
  ```bash
41
- $ bin/rails g model Sql::HttpRequest uid:string data:text
42
-
43
- ```
44
-
45
- add JSON serialization
46
-
47
- ```ruby
48
- # app/models/sql/http_request.rb
49
- class Sql::HttpRequest < ApplicationRecord
50
- serialize :data, JSON
51
- end
52
-
40
+ $ bin/rails g model AnalyticsRequest uid:string data:jsonb status_code:integer application_server_request_start:datetime application_server_request_end:datetime
53
41
  ```
54
42
 
55
43
  add automatic logging via initializer
56
44
 
57
45
  ```ruby
58
- # config/initializers/rack_middlewares.rb
46
+ # config/initializers/log_requests.rb
59
47
 
60
- Rails.application.config.middleware.use(RackRequestObjectLogger, Sql::HttpRequest)
48
+ Rails.application.config.middleware.use(RackRequestObjectLogger, AnalyticsHttpRequest)
61
49
  ```
62
50
 
63
51
  # Performance
@@ -76,7 +64,7 @@ I've seen applications sending sensitive data in GET and even POST requests in a
76
64
 
77
65
  ## License & Author
78
66
 
79
- Copyright 2016 Ivan Stana
67
+ Copyright 2016-2024 Ivan Stana
80
68
 
81
69
  Licensed under the Apache License, Version 2.0 (the "License");
82
70
  you may not use this file except in compliance with the License.
@@ -100,7 +88,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
100
88
 
101
89
  ## Contributing
102
90
 
103
- 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.
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/istana/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.
104
92
 
105
93
  ## Alternatives
106
94
 
data/Rakefile CHANGED
@@ -1,6 +1,12 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- RSpec::Core::RakeTask.new(:spec)
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.pattern = 'spec'
6
+ end
5
7
 
6
- task :default => :spec
8
+ RSpec::Core::RakeTask.new(:spec_performance) do |t|
9
+ t.pattern = 'performance'
10
+ end
11
+
12
+ task :default => [:spec, :spec_performance]
@@ -0,0 +1,9 @@
1
+ require Rails.root.join('app', 'models', 'application_record').to_s
2
+ require Rails.root.join('app', 'models', 'analytics_request').to_s
3
+
4
+ Rails.configuration.before_initialize do
5
+ Rails.application.config.middleware.use(
6
+ RackRequestObjectLogger,
7
+ ::AnalyticsRequest
8
+ )
9
+ end
@@ -0,0 +1,14 @@
1
+ class CreateAnalyticsRequests < ActiveRecord::Migration[7.2]
2
+ def change
3
+ create_table :analytics_requests, id: :uuid do |t|
4
+ t.text :uid
5
+ t.json :data
6
+ t.integer :status_code
7
+ t.datetime :application_server_request_start
8
+ t.datetime :application_server_request_end
9
+ t.references :requestable, polymorphic: true, null: true, type: :uuid, index: true
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class AnalyticsRequest < ::ApplicationRecord
2
+ belongs_to :requestable, polymorphic: true, required: false
3
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class RackRequestObjectLogger
3
- VERSION = "2.0.1"
3
+ VERSION = "4.0.0"
4
4
  end
@@ -17,8 +17,7 @@ class RackRequestObjectLogger
17
17
  logger_object = @model.new
18
18
  logger_object.application_server_request_start = start_time
19
19
  logger_object.application_server_request_end = end_time
20
- request = Rack::Request.new(env)
21
- logger_object.data = request.env.select do |header, value|
20
+ logger_object.data = env.select do |header, value|
22
21
  # don't bother with values other than a string
23
22
  next if !value.is_a?(String)
24
23
  # https://tools.ietf.org/html/rfc3875 CGI 1.1 spec
@@ -38,7 +37,7 @@ class RackRequestObjectLogger
38
37
  ].include?(header) || ['HTTP', 'HTTPS'].include?(header) || header =~ /\AHTTP_\w+\z/ ||
39
38
  ['REQUEST_URI', 'ORIGINAL_FULLPATH', 'ORIGINAL_SCRIPT_NAME'].include?(header)
40
39
  end
41
- logger_object.uid = request.env['action_dispatch.request_id'] || SecureRandom.uuid
40
+ logger_object.uid = env['action_dispatch.request_id'] || SecureRandom.uuid
42
41
  logger_object.status_code = app_result.first
43
42
  logger_object.save
44
43
  app_result
@@ -11,25 +11,31 @@ Gem::Specification.new do |spec|
11
11
  spec.authors = ["Ivan Stana"]
12
12
  spec.email = ["ronon@myrtana.sk"]
13
13
 
14
- spec.summary = %q{Log HTTP requests via Rack stack to an object. You can use any object.}
15
- spec.description = %q{Log HTTP requests via Rack stack to an object. You can use any object, i.e. ActiveRecord model}
16
- spec.homepage = "https://github.com/starmammoth/rack-request-object-logger"
14
+ spec.summary = %q{The project is a Rack middleware to automatically log a HTTP request to a custom object.}
15
+ spec.description = %q{The project is a Rack middleware to automatically log a HTTP request to a custom object.}
16
+ spec.homepage = "https://github.com/istana/rack-request-object-logger"
17
17
  spec.license = "Apache-2.0"
18
18
 
19
19
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
- f.match(%r{^(test|spec|features)/})
20
+ f.match(%r{^(test|spec|features|performance)/})
21
21
  end
22
- spec.bindir = "exe"
23
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
22
  spec.require_paths = ["lib"]
25
23
 
26
- spec.add_dependency "rack", "> 0", "< 3.0"
27
- spec.add_development_dependency "bundler", "~> 1.14"
28
- spec.add_development_dependency "rake", "~> 12.0"
29
- spec.add_development_dependency "rspec", "~> 3.5"
24
+ spec.metadata = {
25
+ "bug_tracker_uri" => "https://github.com/istana/rack-request-object-logger/issues",
26
+ "homepage_uri" => "https://github.com/istana/rack-request-object-logger",
27
+ "source_code_uri" => "https://github.com/istana/rack-request-object-logger",
28
+ }
29
+
30
+ spec.add_dependency "rack", "> 0", "< 4.0"
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 13"
33
+ spec.add_development_dependency "rspec", "~> 3.13"
30
34
  spec.add_development_dependency "rspec-benchmark", "~> 0"
31
35
  spec.add_development_dependency "pry", "~> 0"
32
- spec.add_development_dependency "activerecord", '>= 4.2', "< 6.0"
33
- spec.add_development_dependency "sqlite3", "~> 1.3" unless RUBY_PLATFORM == 'java'
34
- spec.add_development_dependency "activerecord-jdbcsqlite3-adapter", "~> 1.3" if RUBY_PLATFORM == 'java'
36
+ spec.add_development_dependency "activerecord", '>= 6.0', "< 8.0"
37
+ spec.add_development_dependency "sqlite3", "<= 2.1" unless RUBY_PLATFORM == "java"
35
38
  end
39
+
40
+
41
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-request-object-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Stana
8
- autorequire:
9
- bindir: exe
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-11 00:00:00.000000000 Z
11
+ date: 2024-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '3.0'
22
+ version: '4.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,49 +29,49 @@ dependencies:
29
29
  version: '0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '3.0'
32
+ version: '4.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '1.14'
39
+ version: '2.0'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '1.14'
46
+ version: '2.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '12.0'
53
+ version: '13'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '12.0'
60
+ version: '13'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rspec
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '3.5'
67
+ version: '3.13'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '3.5'
74
+ version: '3.13'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rspec-benchmark
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -106,45 +106,45 @@ dependencies:
106
106
  requirements:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
- version: '4.2'
109
+ version: '6.0'
110
110
  - - "<"
111
111
  - !ruby/object:Gem::Version
112
- version: '6.0'
112
+ version: '8.0'
113
113
  type: :development
114
114
  prerelease: false
115
115
  version_requirements: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - - ">="
118
118
  - !ruby/object:Gem::Version
119
- version: '4.2'
119
+ version: '6.0'
120
120
  - - "<"
121
121
  - !ruby/object:Gem::Version
122
- version: '6.0'
122
+ version: '8.0'
123
123
  - !ruby/object:Gem::Dependency
124
124
  name: sqlite3
125
125
  requirement: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - "~>"
127
+ - - "<="
128
128
  - !ruby/object:Gem::Version
129
- version: '1.3'
129
+ version: '2.1'
130
130
  type: :development
131
131
  prerelease: false
132
132
  version_requirements: !ruby/object:Gem::Requirement
133
133
  requirements:
134
- - - "~>"
134
+ - - "<="
135
135
  - !ruby/object:Gem::Version
136
- version: '1.3'
137
- description: Log HTTP requests via Rack stack to an object. You can use any object,
138
- i.e. ActiveRecord model
136
+ version: '2.1'
137
+ description: The project is a Rack middleware to automatically log a HTTP request
138
+ to a custom object.
139
139
  email:
140
140
  - ronon@myrtana.sk
141
141
  executables: []
142
142
  extensions: []
143
143
  extra_rdoc_files: []
144
144
  files:
145
+ - ".github/workflows/ruby.yml"
145
146
  - ".gitignore"
146
147
  - ".rspec"
147
- - ".travis.yml"
148
148
  - CODE_OF_CONDUCT.md
149
149
  - Gemfile
150
150
  - LICENSE
@@ -152,17 +152,20 @@ files:
152
152
  - Rakefile
153
153
  - bin/console
154
154
  - bin/setup
155
+ - examples/rails7/config/initializers/log_http_request.rb
156
+ - examples/rails7/db/migrate/20240917151639_create_analytics_requests.rb
157
+ - examples/rails7/models/analytics_request.rb
155
158
  - lib/rack-request-object-logger.rb
156
159
  - lib/rack-request-object-logger/version.rb
157
- - performance/active_record_model_spec.rb
158
- - performance/in_memory_model_spec.rb
159
- - performance/performance_helper.rb
160
160
  - rack-request-object-logger.gemspec
161
- homepage: https://github.com/starmammoth/rack-request-object-logger
161
+ homepage: https://github.com/istana/rack-request-object-logger
162
162
  licenses:
163
163
  - Apache-2.0
164
- metadata: {}
165
- post_install_message:
164
+ metadata:
165
+ bug_tracker_uri: https://github.com/istana/rack-request-object-logger/issues
166
+ homepage_uri: https://github.com/istana/rack-request-object-logger
167
+ source_code_uri: https://github.com/istana/rack-request-object-logger
168
+ post_install_message:
166
169
  rdoc_options: []
167
170
  require_paths:
168
171
  - lib
@@ -177,9 +180,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
180
  - !ruby/object:Gem::Version
178
181
  version: '0'
179
182
  requirements: []
180
- rubyforge_project:
181
- rubygems_version: 2.6.8
182
- signing_key:
183
+ rubygems_version: 3.5.16
184
+ signing_key:
183
185
  specification_version: 4
184
- summary: Log HTTP requests via Rack stack to an object. You can use any object.
186
+ summary: The project is a Rack middleware to automatically log a HTTP request to a
187
+ custom object.
185
188
  test_files: []
data/.travis.yml DELETED
@@ -1,15 +0,0 @@
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
9
- cache: bundler
10
- matrix:
11
- include:
12
- - rvm: ruby-head
13
- allow_failures:
14
- - rvm: ruby-head
15
- fast_finish: true
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
- require_relative 'performance_helper'
3
- require_relative '../spec/spec_data'
4
- require 'active_record'
5
-
6
- RSpec.describe "Performance testing with an ActiveRecord class and SQLite3 database", performance: true do
7
- include RSpec::Benchmark::Matchers
8
- include_context 'spec_data'
9
-
10
- class RequestActiveRecordLog < ActiveRecord::Base
11
- serialize :headers, JSON
12
- end
13
-
14
- class CreateRequestActiveRecordLog < ActiveRecord::Migration[4.2]
15
- def change
16
- create_table :request_active_record_logs do |t|
17
- t.integer :uid
18
- t.text :data
19
- t.time :application_server_request_start
20
- t.time :application_server_request_end
21
- end
22
- end
23
- end
24
-
25
- let(:app) { proc{ [200, {}, ['Hello, world.']] } }
26
- let(:stack) { RackRequestObjectLogger.new(app, RequestActiveRecordLog) }
27
- let(:request) { Rack::MockRequest.new(stack) }
28
-
29
- after { File.unlink 'active_record_performance.sqlite3' }
30
-
31
- it 'much fast. wow' do
32
- set_headers(rails5_puma_headers)
33
-
34
- ActiveRecord::Base.establish_connection(
35
- adapter: 'sqlite3',
36
- database: 'active_record_performance.sqlite3'
37
- )
38
- CreateRequestActiveRecordLog.migrate(:up)
39
- expect { request.get('http://localhost:4000/doge') }.to perform_at_least(500).ips
40
- end
41
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
- require_relative 'performance_helper'
3
- require_relative '../spec/spec_data'
4
-
5
- RSpec.describe "Performance testing with a dummy class", performance: true do
6
- include RSpec::Benchmark::Matchers
7
- include_context 'spec_data'
8
-
9
- let(:app) { proc{ [200, {}, ['Hello, world.']] } }
10
- let(:stack) { RackRequestObjectLogger.new(app, RequestDummyLog) }
11
- let(:request) { Rack::MockRequest.new(stack) }
12
-
13
- let(:logger_object) { RequestDummyLog.new }
14
- before { allow(RequestDummyLog).to receive(:new).and_return(logger_object) }
15
-
16
- it 'much fast. wow' do
17
- set_headers(rails5_puma_headers)
18
- expect { request.get('http://localhost:4000/doge') }.to perform_at_least(5000).ips
19
- end
20
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'bundler/setup'
3
- require 'rspec-benchmark'
4
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
- require "rack-request-object-logger"