Skip to content

Commit

Permalink
Rename Puma.stats_hash to Puma.stats
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed Dec 17, 2019
1 parent ab52666 commit fb8b934
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 51 deletions.
2 changes: 1 addition & 1 deletion History.md
Expand Up @@ -3,7 +3,7 @@
* Features
* Add pumactl `thread-backtraces` command to print thread backtraces (#2053)
* Configuration: `environment` is read from `RAILS_ENV`, if `RACK_ENV` can't be found (#2022)
* `Puma.stats` was renamed to `Puma.stats_json`, `Puma.stats_hash` has been added to provide the same data as a Hash
* `Puma.stats` now returns a Hash instead of a JSON string

* Bugfixes
* Your bugfix goes here (#Github Number)
Expand Down
10 changes: 2 additions & 8 deletions lib/puma.rb
Expand Up @@ -19,16 +19,10 @@ def self.stats_object=(val)
@get_stats = val
end

def self.stats_hash
@get_stats.stats_hash
def self.stats
@get_stats.stats
end


def self.stats_json
@get_stats.stats_json
end
singleton_class.send(:alias_method, :stats, :stats_json)

# Thread name is new in Ruby 2.3
def self.set_thread_name(name)
return unless Thread.current.respond_to?(:name=)
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/app/status.rb
Expand Up @@ -54,7 +54,7 @@ def call(env)
rack_response(200, GC.stat.to_json)

when /\/stats$/
rack_response(200, @cli.stats_json)
rack_response(200, @cli.stats.to_json)

when /\/thread-backtraces$/
backtraces = []
Expand Down
19 changes: 2 additions & 17 deletions lib/puma/cluster.rb
Expand Up @@ -96,7 +96,7 @@ def ping!(status)
@last_checkin = Time.now
captures = status.match(/{ "backlog":(?<backlog>\d*), "running":(?<running>\d*), "pool_capacity":(?<pool_capacity>\d*), "max_threads": (?<max_threads>\d*) }/)
@last_status = captures.names.inject({}) do |hash, key|
hash[key.to_sym] = captures[key]
hash[key.to_sym] = captures[key].to_i
hash
end
end
Expand Down Expand Up @@ -354,7 +354,7 @@ def reload_worker_directory

# Inside of a child process, this will return all zeroes, as @workers is only populated in
# the master process.
def stats_hash
def stats
old_worker_count = @workers.count { |w| w.phase != @phase }
worker_status = @workers.map do |w|
{
Expand All @@ -378,21 +378,6 @@ def stats_hash
}
end

def stats_json
h = stats_hash
worker_status = h[:worker_status].map do |w|
l = w[:last_status]
last_status = if l.empty?
'{}'
else
%Q!{ "backlog":#{l[:backlog]}, "running":#{l[:running]}, "pool_capacity":#{l[:pool_capacity]}, "max_threads": #{l[:max_threads]} }!
end
%Q!{ "started_at": "#{w[:started_at]}", "pid": #{w[:pid]}, "index": #{w[:index]}, "phase": #{w[:phase]}, "booted": #{w[:booted]}, "last_checkin": "#{w[:last_checkin]}", "last_status": #{last_status} }!
end
%Q!{ "started_at": "#{h[:started_at]}", "workers": #{h[:workers]}, "phase": #{h[:phase]}, "booted_workers": #{h[:booted_workers]}, "old_workers": #{h[:old_workers]}, "worker_status": [#{worker_status.join(",")}] }!
end
alias_method :stats, :stats_json

def preload?
@options[:preload_app]
end
Expand Down
9 changes: 2 additions & 7 deletions lib/puma/launcher.rb
Expand Up @@ -95,15 +95,10 @@ def initialize(conf, launcher_args={})
attr_reader :binder, :events, :config, :options, :restart_dir

# Return stats about the server
def stats_hash
@runner.stats_hash
def stats
@runner.stats
end

def stats_json
@runner.stats_json
end
alias_method :stats, :stats_json

# Write a state file that can be used by pumactl to control
# the server
def write_state
Expand Down
8 changes: 1 addition & 7 deletions lib/puma/single.rb
Expand Up @@ -13,7 +13,7 @@ module Puma
# gets created via the `start_server` method from the `Puma::Runner` class
# that this inherits from.
class Single < Runner
def stats_hash
def stats
{
started_at: @started_at.utc.iso8601,
backlog: @server.backlog || 0,
Expand All @@ -23,12 +23,6 @@ def stats_hash
}
end

def stats_json
h = stats_hash
%Q!{ "started_at": "#{h[:started_at]}", "backlog": #{h[:backlog]}, "running": #{h[:running]}, "pool_capacity": #{h[:pool_capacity]}, "max_threads": #{h[:max_threads]} }!
end
alias_method :stats, :stats_json

def restart
@server.begin_restart
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_app_status.rb
Expand Up @@ -23,8 +23,8 @@ def halt
@status = :halt
end

def stats_json
"{}"
def stats
{}
end
end

Expand Down
13 changes: 5 additions & 8 deletions test/test_cli.rb
Expand Up @@ -57,9 +57,7 @@ def test_control_for_tcp
body = s.read
s.close

assert_match(/{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }/, body.split(/\r?\n/).last)
assert_match(/{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }/, Puma.stats_json)
assert_equal([:started_at, :backlog, :running, :pool_capacity, :max_threads], Puma.stats_hash.keys)
assert_match(/{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":16,"max_threads":16}/, body.split(/\r?\n/).last)

ensure
cli.launcher.stop
Expand Down Expand Up @@ -93,10 +91,9 @@ def test_control_for_ssl
body = http.request(req).body
end

expected_stats = /{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }/
expected_stats = /{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":16,"max_threads":16}/
assert_match(expected_stats, body.split(/\r?\n/).last)
assert_match(expected_stats, Puma.stats_json)
assert_equal([:started_at, :backlog, :running, :pool_capacity, :max_threads], Puma.stats_hash.keys)
assert_equal([:started_at, :backlog, :running, :pool_capacity, :max_threads], Puma.stats.keys)

ensure
cli.launcher.stop
Expand Down Expand Up @@ -139,7 +136,7 @@ def test_control_clustered
body = s.read
s.close

assert_match(/\{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "workers": 2, "phase": 0, "booted_workers": 2, "old_workers": 0, "worker_status": \[\{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "pid": \d+, "index": 0, "phase": 0, "booted": true, "last_checkin": "[^"]+", "last_status": \{ "backlog":0, "running":2, "pool_capacity":2, "max_threads": 2 \} \},\{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "pid": \d+, "index": 1, "phase": 0, "booted": true, "last_checkin": "[^"]+", "last_status": \{ "backlog":0, "running":2, "pool_capacity":2, "max_threads": 2 \} \}\] \}/, body.split("\r\n").last)
assert_match(/\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","workers":2,"phase":0,"booted_workers":2,"old_workers":0,"worker_status":\[\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","pid":\d+,"index":0,"phase":0,"booted":true,"last_checkin":"[^"]+","last_status":\{"backlog":0,"running":2,"pool_capacity":2,"max_threads":2\}\},\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","pid":\d+,"index":1,"phase":0,"booted":true,"last_checkin":"[^"]+","last_status":\{"backlog":0,"running":2,"pool_capacity":2,"max_threads":2\}\}\]\}/, body.split("\r\n").last)
ensure
if UNIX_SKT_EXIST && HAS_FORK
cli.launcher.stop
Expand Down Expand Up @@ -174,7 +171,7 @@ def test_control
body = s.read
s.close

assert_match(/{ "started_at": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }/, body.split("\r\n").last)
assert_match(/{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":16,"max_threads":16}/, body.split("\r\n").last)
ensure
if UNIX_SKT_EXIST
cli.launcher.stop
Expand Down

0 comments on commit fb8b934

Please sign in to comment.