puma-pool-usage 0.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
+ SHA256:
3
+ metadata.gz: 622845ccb16eb65e8b3822c0386090724f0b524c1b50c228c0a2364d93ab6a70
4
+ data.tar.gz: 1239bcc7a2c45cb46982d6c01cc5278fc4347a727b597a1f6c812985e0d63f87
5
+ SHA512:
6
+ metadata.gz: 6b803c243773fc8e6cb6b641c04b4a5b42a1114ec1d327ff04d811a66a267dba18c7e5701a0067943e02d8251b369a9d1be55b1b498b5387699db8b54058589f
7
+ data.tar.gz: 8a2d152f58bfbffae52fa9f9870cffeb99787df054c5324c9709348e865ade615f1b20c02ce8e5c4284239669934ddfbc1fe1a3cfd88aafb59ac1664244dd9d8
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ Gemfile.lock
12
+ gemfiles/*.lock
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ NewCops: enable
3
+
4
+ Bundler/OrderedGems:
5
+ Exclude:
6
+ - "gemfiles/**/*"
7
+
8
+ Layout/LineLength:
9
+ Max: 120
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - "spec/**/*"
14
+ - "*.gemspec"
15
+
16
+ Style/FrozenStringLiteralComment:
17
+ Exclude:
18
+ - "gemfiles/**/*"
19
+
20
+ Style/StringLiterals:
21
+ EnforcedStyle: double_quotes
data/.travis.yml ADDED
@@ -0,0 +1,29 @@
1
+ after_script:
2
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
3
+
4
+ before_install: gem install bundler -v 2.0.2
5
+
6
+ before_script:
7
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
8
+ - chmod +x ./cc-test-reporter
9
+ - ./cc-test-reporter before-build
10
+
11
+ env:
12
+ global:
13
+ - CC_TEST_REPORTER_ID=91fd1b5f4e32e12f68c365516db8abff11ac17b75d043c7b18e672b836a92f15
14
+
15
+ gemfile:
16
+ - gemfiles/rails_5.gemfile
17
+ - gemfiles/rails_6.gemfile
18
+ - gemfiles/latest.gemfile
19
+
20
+ language: ruby
21
+
22
+ rvm:
23
+ - 2.5.8
24
+ - 2.6.6
25
+ - 2.7.1
26
+
27
+ script:
28
+ - bundle exec rspec
29
+ - bundle exec rubocop
data/Appraisals ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise "rails-5" do
4
+ gem "rails", "~> 5.0"
5
+ gem "puma", "3.12.0"
6
+ end
7
+
8
+ appraise "rails-6" do
9
+ gem "rails", "~> 6.0"
10
+ gem "puma", "~> 4.0"
11
+ end
12
+
13
+ appraise "latest" do
14
+ gem "rails"
15
+ gem "puma"
16
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # Puma Pool Usage
2
+
3
+ The changelog for Puma Pool Usage is maintained in the [GitHub Releases](https://github.com/simplymadeapps/puma-pool-usage/releases).
@@ -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 contact@simplymadeapps.com. 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,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in puma-pool-usage.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Simply Made Apps
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Puma Pool Usage
2
+
3
+ [![Build Status](https://travis-ci.org/simplymadeapps/puma-pool-usage.svg?branch=master)](https://travis-ci.org/simplymadeapps/puma-pool-usage)
4
+ [![Code Climate](https://codeclimate.com/github/simplymadeapps/puma-pool-usage/badges/gpa.svg)](https://codeclimate.com/github/simplymadeapps/puma-pool-usage)
5
+ [![Test Coverage](https://codeclimate.com/github/simplymadeapps/puma-pool-usage/badges/coverage.svg)](https://codeclimate.com/github/simplymadeapps/puma-pool-usage/coverage)
6
+
7
+ A Puma plugin that interprets usage statistics for the percentage of your resources under load. Uses Rails to log this statistic. Inspired by [Tomas Ruzicka](https://github.com/LeZuse) and Heroku's pool usage statistics.
8
+
9
+ Look at this neat log:
10
+
11
+ source=PUMA pid=74840 sample#pool_usage=0.8
12
+
13
+ If your web server is fielding no requests, usage will be 0.0 (0%). If every single resource has a request, usage will be 1.0 (100%). If you have a backlog of requests that cannot be processed because no resources are available, usage can be over 100% (like 1.4).
14
+
15
+ Wish to chart this data, setup alerts, or scale your servers based on it? Great idea, but it's beyond the scope of this plugin. This plugin provides the data, what you do with the data is up to you.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem "puma-pool-usage"
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install puma-pool-usage
32
+
33
+ ## Usage
34
+
35
+ In your Puma configuration file (config/puma.rb), add the following line:
36
+
37
+ ```ruby
38
+ plugin :pool_usage
39
+ ```
40
+
41
+ Restart your server and you're all set.
42
+
43
+ ## Configuration
44
+
45
+ By default, Puma Pool Usage will log every Puma worker every 60 seconds. You can configure this via the environment variable `PUMA_STATS_FREQUENCY`. To gather this data only every 5 minutes, set your environment like so:
46
+
47
+ PUMA_STATS_FREQUENCY=300
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/simplymadeapps/puma-pool-usage. 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.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
56
+
57
+ ## Code of Conduct
58
+
59
+ Everyone interacting in the Puma::Pool::Usage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/simplymadeapps/puma-pool-usage/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails"
6
+ gem "puma"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.0"
6
+ gem "puma", "3.12.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 6.0"
6
+ gem "puma", "~> 4.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "puma/plugin"
5
+ require "rails"
6
+
7
+ # Plugin that outputs pool usage into Rails logs.
8
+ # Usage expressed via a percentage of your available resources as a float.
9
+ #
10
+ # @example when running in cluster mode (pid is logged)
11
+ # source=PUMA pid=123456 sample#pool_usage=0.8
12
+ #
13
+ # @example when not running in cluster mode (pid is logged as 0)
14
+ # source=PUMA pid=0 sample#pool_usage=0.5
15
+ Puma::Plugin.create do
16
+ # Method called by Puma on startup.
17
+ #
18
+ # @param [Puma::Launcher] _launcher Puma launcher object, ignored.
19
+ def start(_launcher)
20
+ in_background do
21
+ loop do
22
+ sleep ENV.fetch("PUMA_STATS_FREQUENCY", 60).to_i
23
+ find_and_log_pool_usage
24
+ Rails.logger.flush
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ # Find the Puma stats necessary depending on mode (single vs. cluster).
32
+ # Sends statistics for logging.
33
+ def find_and_log_pool_usage
34
+ stats = JSON.parse(Puma.stats, symbolize_names: true)
35
+
36
+ if stats[:worker_status]
37
+ stats[:worker_status].each { |worker| log_pool_usage(worker[:last_status], pid: worker[:pid]) }
38
+ else
39
+ log_pool_usage(stats, pid: 0)
40
+ end
41
+ end
42
+
43
+ # Calculate and log, assuing Puma has started and provided stats.
44
+ #
45
+ # @param [Hash] status Puma statistics.
46
+ # @param [Integer] pid Process identifier for this particular Puma worker.
47
+ def log_pool_usage(status, pid:)
48
+ return if status[:pool_capacity].nil?
49
+
50
+ pool_usage = (status[:running] - (status[:pool_capacity] - status[:backlog])) / status[:running].to_f
51
+ Rails.logger.info "source=PUMA pid=#{pid} sample#pool_usage=#{pool_usage}"
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "puma-pool-usage"
5
+ spec.version = "0.1.0"
6
+ spec.authors = ["Brandon Medenwald"]
7
+ spec.email = ["brandon@simplymadeapps.com"]
8
+
9
+ spec.summary = "Puma pool usage statistics in your logs"
10
+ spec.description = "Add pool usage statistics within your log files."
11
+ spec.homepage = "https://www.github.com/simplymadeapps/puma-pool-usage"
12
+ spec.license = "MIT"
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+ spec.metadata["changelog_uri"] = "https://www.github.com/simplymadeapps/puma-pool-usage/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
22
+ end
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_runtime_dependency "puma", ">= 3.12", "< 5.0"
26
+ spec.add_runtime_dependency "rails", ">= 5.0"
27
+
28
+ spec.add_development_dependency "appraisal"
29
+ spec.add_development_dependency "bundler", "~> 2.0"
30
+ spec.add_development_dependency "rake", ">= 12.3.3"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "rubocop", "~> 0.83.0"
33
+ spec.add_development_dependency "simplecov", "< 0.18" # https://github.com/codeclimate/test-reporter/issues/413
34
+ spec.add_development_dependency "simplecov-rcov"
35
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puma-pool-usage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Medenwald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puma
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.12'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '5.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: appraisal
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 12.3.3
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 12.3.3
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.83.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.83.0
117
+ - !ruby/object:Gem::Dependency
118
+ name: simplecov
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "<"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.18'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "<"
129
+ - !ruby/object:Gem::Version
130
+ version: '0.18'
131
+ - !ruby/object:Gem::Dependency
132
+ name: simplecov-rcov
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ description: Add pool usage statistics within your log files.
146
+ email:
147
+ - brandon@simplymadeapps.com
148
+ executables: []
149
+ extensions: []
150
+ extra_rdoc_files: []
151
+ files:
152
+ - ".gitignore"
153
+ - ".rspec"
154
+ - ".rubocop.yml"
155
+ - ".travis.yml"
156
+ - Appraisals
157
+ - CHANGELOG.md
158
+ - CODE_OF_CONDUCT.md
159
+ - Gemfile
160
+ - LICENSE.txt
161
+ - README.md
162
+ - Rakefile
163
+ - gemfiles/.bundle/config
164
+ - gemfiles/latest.gemfile
165
+ - gemfiles/rails_5.gemfile
166
+ - gemfiles/rails_6.gemfile
167
+ - lib/puma/plugin/pool_usage.rb
168
+ - puma-pool-usage.gemspec
169
+ homepage: https://www.github.com/simplymadeapps/puma-pool-usage
170
+ licenses:
171
+ - MIT
172
+ metadata:
173
+ homepage_uri: https://www.github.com/simplymadeapps/puma-pool-usage
174
+ source_code_uri: https://www.github.com/simplymadeapps/puma-pool-usage
175
+ changelog_uri: https://www.github.com/simplymadeapps/puma-pool-usage/CHANGELOG.md
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubygems_version: 3.1.3
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: Puma pool usage statistics in your logs
195
+ test_files: []