lograge_rails_request_queuing 0.2.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
+ SHA256:
3
+ metadata.gz: adaca376be4396d88e5bdf9f25826e587eb78c88b0d6bdd1a8ba3972841d4aa8
4
+ data.tar.gz: 02e2b2be34c7568f1e7848212fc96f7c12edc3d061ff6e23b312dbeca033a901
5
+ SHA512:
6
+ metadata.gz: a80c1b66d9328d9b25865332ee7d2f57b10c69f7c828fcc4bb16987ffaf4cc6a9fd2f5457e2c08ff4bef86c8bb905bb237e75857facc31047e92689961b65d3b
7
+ data.tar.gz: 979df62a426b344dfbd3322d009141b333e805581bc40bbf920db5021cd349dbd172126dfb3ca0161c3d1a545511a7fcd97c438d1a0571c6e68f94f198c724fb
@@ -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 laust@object.io. 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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Laust Rud Jacobsen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # LogrageRailsRequestQueuing
2
+
3
+ [![Build Status](https://travis-ci.org/rud/lograge_rails_request_queuing.svg?branch=master)](https://travis-ci.org/rud/lograge_rails_request_queuing)
4
+
5
+ [Lograge](https://github.com/roidrage/lograge) makes Rails logging output a lot more more useful.
6
+ Using the logstash formatter, the log output for a request will look something like this:
7
+
8
+ ```
9
+ status=200 duration=58.33 view=40.43 db=15.26 controller=WelcomeController action=show
10
+ ```
11
+
12
+ This gem adds another field with how long the request spent in the request queue in nginx in milliseconds, the `rq` value:
13
+
14
+ ```
15
+ status=200 duration=58.33 view=40.43 db=15.26 rq=3.14 [...]
16
+ ```
17
+
18
+ Request queueing time is the time that passes between a request is received in Nginx, and until it hits the Rails stack in a web worker.
19
+ Under normal load in production this value will be in the order of a handful milliseconds.
20
+ However, if all Rails web-processes are busy, the number will quickly climb as individual requests are queued and waiting to be served.
21
+ It's one of those numbers that are good to keep an eye on in monitoring and is very helpful to include when graphing response times over time.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'lograge_rails_request_queuing'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install lograge_rails_request_queuing
38
+
39
+ Then add it to your lograge initializer, typically in `config/initializers/lograge.rb`:
40
+
41
+ ``` ruby
42
+ Rails.application.configure do
43
+ config.lograge.enabled = true
44
+
45
+ # Keep emitting the verbose logging for easier debug
46
+ config.lograge.keep_original_rails_log = !Rails.env.production?
47
+
48
+ config.lograge.custom_options = lambda do |event|
49
+ custom_options = {}
50
+
51
+ queued_ms = RequestStore[:lograge_request_queueing].queued_ms
52
+ custom_options[:rq] = queued_ms.round(2) if queued_ms
53
+
54
+ custom_options
55
+ end
56
+ end
57
+ ```
58
+
59
+ In your nginx config, add:
60
+ ```
61
+ proxy_set_header X-Request-Start "t=${msec}";`
62
+ ```
63
+
64
+ This adds a new header to the incoming request, with current time in milliseconds as the value.
65
+
66
+ After this is deployed, you now get the `"rq=.."` value added to the output when the value is available.
67
+ If you do not see the `"rq=.."` value in logging out, double check you have added the new header in the nginx config.
68
+
69
+
70
+ ### Bonus feature: compact exception logging
71
+
72
+ Add the following to your `config/initializers/lograge.rb` file:
73
+
74
+ ``` ruby
75
+ config.lograge.custom_options = lambda do |event|
76
+ LogrageRailsRequestQueuing::ExceptionDetails.add_any_exception!(
77
+ event, custom_options
78
+ )
79
+
80
+ # ... see above ...
81
+ end
82
+
83
+ # Adding this removes the verbose exception output in the Rails log:
84
+ ActionDispatch::DebugExceptions.prepend(
85
+ LogrageRailsRequestQueuing::SilenceExceptionLogging
86
+ )
87
+ ```
88
+
89
+ Exceptions logged look like this, with a few newlines added for readability here:
90
+
91
+ ```
92
+ I, [2018-06-29T18:44:26.267292 #4] INFO -- : [29f3a9f6-1848-4858-93be-6ad76a6b9389]
93
+ method=GET path=/ format=html controller=EchoController action=index status=500
94
+ error='ArgumentError: Insufficient mittens' duration=0.27 view=0.00 rq=4.12ms
95
+ exception=["ArgumentError", "Insufficient mittens"]
96
+ backtrace=["/app/app/controllers/echo_controller.rb:4:in `index'", "/app/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'", ...SNIP...]
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ 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).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rud/lograge_rails_request_queuing. 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.
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
112
+
113
+ ## Code of Conduct
114
+
115
+ Everyone interacting in the LogrageRailsRequestQueuing project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rud/lograge_rails_request_queuing/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'LogrageRailsRequestQueuing'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'lib'
25
+ t.libs << 'test'
26
+ t.test_files = FileList['test/**/*_test.rb']
27
+ t.verbose = true
28
+ end
29
+
30
+ desc 'Run all tests'
31
+ task ci: :test
32
+
33
+ task default: :test
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lograge'
4
+ require 'lograge_rails_request_queuing/engine'
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/engine'
4
+
5
+ module LogrageRailsRequestQueuing
6
+ # Hook into Rails loading
7
+ class Engine < ::Rails::Engine
8
+ isolate_namespace LogrageRailsRequestQueuing
9
+
10
+ config.eager_load_paths += %W[#{config.root}/lib]
11
+
12
+ initializer 'lograge_rails_request_queuing.add_timing_middleware' do |app|
13
+ app.middleware.use LogrageRailsRequestQueuing::RequestStartedMiddleware
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module LogrageRailsRequestQueuing
2
+ class ExceptionDetails
3
+ # If the current request has an exception attached, add the exception details
4
+ # and backtrace to the logging custom options
5
+ def self.add_any_exception!(event, log_custom_options)
6
+ if event.payload[:exception_object]
7
+ log_custom_options[:exception] = event.payload[:exception]
8
+ log_custom_options[:backtrace] = Array(event.payload[:exception_object].backtrace)
9
+ end
10
+ nil
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LogrageRailsRequestQueuing
4
+ class RequestQueueing
5
+ # How long was the request queued for, in milliseconds
6
+ attr_reader :queued_ms
7
+ attr_reader :request_started_at, :request_queued_raw
8
+
9
+ REQUEST_START_HEADER = 'HTTP_X_REQUEST_START'
10
+ EARLIEST_REQUEST_DATE = Time.new(2000)
11
+
12
+ def initialize(env, request_started_at = Time.zone.now.to_f)
13
+ @request_queued_raw = request_start_header(env)
14
+ @request_started_at = request_started_at
15
+ @queued_ms = calculate_queued_ms
16
+ end
17
+
18
+ def request_queued_at
19
+ return if request_queued_float.blank?
20
+ @request_queued_at ||= begin
21
+ [1000, 1].each do |divisor|
22
+ adjusted = Time.zone.at(request_queued_float / divisor)
23
+ return adjusted if adjusted > EARLIEST_REQUEST_DATE
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def calculate_queued_ms
31
+ return if request_queued_at.blank?
32
+
33
+ waiting_interval_secs = request_started_at - request_queued_at.to_f
34
+ return if waiting_interval_secs < 0 # clocks out of alignment
35
+
36
+ waiting_interval_secs * 1000
37
+ end
38
+
39
+ def request_queued_float
40
+ return nil if request_queued_raw.blank?
41
+
42
+ # convert values of the form:
43
+ # "t=1529578997.145"
44
+ # "1529578997145"
45
+ # to a Float:
46
+
47
+ if request_queued_raw =~ /(t=)?([.\d+]+)/
48
+ Float(Regexp.last_match(2))
49
+ end
50
+ end
51
+
52
+ def request_start_header(env)
53
+ env[REQUEST_START_HEADER]
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LogrageRailsRequestQueuing
4
+ # Track at what time the request handling starts
5
+ class RequestStartedMiddleware
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ require 'lograge_rails_request_queuing/request_queuing'
12
+
13
+ RequestStore[:lograge_request_queueing] = RequestQueueing.new(env)
14
+ @app.call env
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module LogrageRailsRequestQueuing
2
+ # Meant to be prepended in ActionDispatch::DebugExceptions like this:
3
+ #
4
+ # ActionDispatch::DebugExceptions.prepend(
5
+ # LogrageRailsRequestQueuing::SilenceExceptionLogging
6
+ # )
7
+ module SilenceExceptionLogging
8
+
9
+ # Normally ActionDispatch::DebugExceptions will print exception details
10
+ # and a full stacktrace to the Rails log. This stops that behavior:
11
+ def log_error *_args
12
+ # pass
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LogrageRailsRequestQueuing
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # desc "Explaining what the task does"
4
+ # task :lograge_rails_request_queuing do
5
+ # # Task goes here
6
+ # end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lograge_rails_request_queuing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Laust Rud Jacobsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lograge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 5.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 5.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: request_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - laust@valuestream.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CODE_OF_CONDUCT.md
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/lograge_rails_request_queuing.rb
81
+ - lib/lograge_rails_request_queuing/engine.rb
82
+ - lib/lograge_rails_request_queuing/exception_details.rb
83
+ - lib/lograge_rails_request_queuing/request_queuing.rb
84
+ - lib/lograge_rails_request_queuing/request_started_middleware.rb
85
+ - lib/lograge_rails_request_queuing/silence_exception_logging.rb
86
+ - lib/lograge_rails_request_queuing/version.rb
87
+ - lib/tasks/lograge_rails_request_queuing_tasks.rake
88
+ homepage: https://github.com/rud/lograge_rails_request_queuing
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.7.6
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Add request queueing time to lograge output.
112
+ test_files: []