puma-stats 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f396129e8be9e85b76570d975528664a442288b
4
+ data.tar.gz: 9d9bd1a156559b8e13d59936c22b51928f42eb67
5
+ SHA512:
6
+ metadata.gz: 7a234756a43d6b54f8edc3f547f837cef073c3ebd33590ef50d2cf8bb2c5ba9373a84de5872aac7256438ee915c1c0d67d6676627aba6bf50f47216870e075c5
7
+ data.tar.gz: 482236a96b8589e23404aa0367f4dcd58f89ad76e76a065f98d4bb4d3ca1bad18ceb3995b3fec57ecb1d0befa65d480cf379145ca4ba38af97c87497eadd825c
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ Initial release of the `puma-stats` gem.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 harmjanblok
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # puma-stats
2
+
3
+ A puma plugin to expose Puma's internal statistics.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'puma-stats'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install puma-stats
20
+
21
+ ## Usage
22
+
23
+ Add following lines to your puma `config.rb` (see
24
+ [Configuration File](https://github.com/puma/puma#configuration-file)):
25
+
26
+ ```ruby
27
+ # config/puma.rb
28
+
29
+ plugin 'stats'
30
+
31
+ # Bind the stats server to "url". "tcp://" is the only accepted protocol.
32
+ #
33
+ # stats_url 'tcp://0.0.0.0:12345'
34
+ # stats_token 'knockknock'
35
+ ```
36
+
37
+ ## Credits
38
+
39
+ The gem is inspired by the following projects:
40
+ * https://github.com/puma/puma-metrics
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/test_*.rb']
10
+ end
11
+
12
+ task :server do
13
+ require 'puma'
14
+ require 'puma/configuration'
15
+ require 'puma/events'
16
+ require 'puma/plugin/stats.rb'
17
+
18
+ configuration = Puma::Configuration.new do |config|
19
+ config.bind 'tcp://127.0.0.1:0'
20
+ config.stats_url 'tcp://127.0.0.1:12345'
21
+ config.plugin 'stats'
22
+ config.workers 1
23
+ config.app do |_env|
24
+ [200, {}, ['Hello World']]
25
+ end
26
+ end
27
+
28
+ events = Puma::Events.new STDOUT, STDERR
29
+ launcher = Puma::Launcher.new(configuration, events: events)
30
+ launcher.run
31
+ end
32
+
33
+ task default: :test
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'puma/stats/dsl'
4
+
5
+ Puma::Plugin.create do
6
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
7
+ def start(launcher)
8
+ str = launcher.options[:stats_url] || 'tcp://0.0.0.0:51209'
9
+
10
+ require 'puma/stats/app'
11
+
12
+ app = Puma::Stats::App.new launcher
13
+ uri = URI.parse str
14
+
15
+ stats = Puma::Server.new app, launcher.events
16
+ stats.min_threads = 0
17
+ stats.max_threads = 1
18
+
19
+ case uri.scheme
20
+ when 'tcp'
21
+ optional_token = launcher.options[:stats_token] ? "with auth token: #{launcher.options[:stats_token]}" : ''
22
+ launcher.events.log "* Starting stats server on URI: #{str} #{optional_token}"
23
+ stats.add_tcp_listener uri.host, uri.port
24
+ else
25
+ launcher.events.error "Invalid stats server URI: #{str}"
26
+ end
27
+
28
+ launcher.events.register(:state) do |state|
29
+ if %i[halt restart stop].include?(state)
30
+ stats.stop(true) unless stats.shutting_down?
31
+ end
32
+ end
33
+
34
+ stats.run
35
+ end
36
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
37
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Puma
6
+ module Stats
7
+ class App
8
+ def initialize(launcher)
9
+ @launcher = launcher
10
+ @auth_token = launcher.options[:stats_token]
11
+ end
12
+
13
+ def call(env)
14
+ unless authenticate(env)
15
+ return rack_response(403, "Invalid stats auth token", 'text/plain')
16
+ end
17
+
18
+ case env['PATH_INFO']
19
+ when /\/puma-stats-gc$/
20
+ rack_response(200, GC.stat.to_json)
21
+
22
+ when /\/puma-stats$/
23
+ rack_response(200, @launcher.stats)
24
+
25
+ else
26
+ rack_response 404, "Unsupported request", 'text/plain'
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def authenticate(env)
33
+ return true unless @auth_token
34
+ env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
35
+ end
36
+
37
+ def rack_response(status, body, content_type='application/json')
38
+ headers = {
39
+ 'Content-Type' => content_type,
40
+ 'Content-Length' => body.bytesize.to_s
41
+ }
42
+ [status, headers, [body]]
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Puma
4
+ class DSL
5
+ def stats_url(url = nil)
6
+ @options[:stats_url] = url
7
+ end
8
+
9
+ def stats_token(token = nil)
10
+ @options[:stats_token] = token
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Puma
4
+ module Stats
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puma-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Kishel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-27 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.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rake
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
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A puma plugin to expose Puma's internal statistics
84
+ email: tom.kishel@puppet.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - CHANGELOG.md
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/puma/plugin/stats.rb
94
+ - lib/puma/stats/app.rb
95
+ - lib/puma/stats/dsl.rb
96
+ - lib/puma/stats/version.rb
97
+ homepage: https://github.com/tkishel/puma-stats
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.14.1
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Expose Puma's internal statistics
121
+ test_files: []