1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Expose top level Puma.stats API

Right now to get stats for a puma process the only way is to go through the control server. The puma-stats-logger does this by reading in a state file then querying the control server manually.

7ad7798e9d/lib/puma_stats_logger/middleware.rb (L28)

Instead, I’m proposing adding a top level `Puma.stats` method that will allow anyone inside of the same process to get access to the stats.

This could be instrumented by a gem to theoretically export these stats to say a Heroku dashboard where we could list out backlog or thread count.

The format of stats is a hash, and will change depending on if the server is in “single” or “clustered” mode.

Clustered:

```
{ "workers": 2, "phase": 0, "booted_workers": 2, "old_workers": 0, "worker_status": [{ "pid": 19832, "index": 0, "phase": 0, "booted": true, "last_checkin": "2018-03-12T16:03:12Z", "last_status": { "backlog":0, "running":5 } },{ "pid": 19833, "index": 1, "phase": 0, "booted": true, "last_checkin": "2018-03-12T16:03:12Z", "last_status": { "backlog":0, "running":5 } }] }
```

Single:

```
{ "backlog": 0, "running": 2 }
```

Alternatively if we could somehow enable another process to get these stats from Puma via pumactl by default without requiring any additional in app config, that would also work.
This commit is contained in:
schneems 2018-03-12 11:41:39 -05:00
parent e5b5cb87ff
commit 441a42e185
4 changed files with 11 additions and 0 deletions

View file

@ -12,4 +12,12 @@ module Puma
autoload :Const, 'puma/const'
autoload :Server, 'puma/server'
autoload :Launcher, 'puma/launcher'
def self.stats_object=(val)
@get_stats = val
end
def self.stats
@get_stats.stats
end
end

View file

@ -1,6 +1,7 @@
require 'optparse'
require 'uri'
require 'puma'
require 'puma/configuration'
require 'puma/launcher'
require 'puma/const'

View file

@ -86,6 +86,7 @@ module Puma
else
@runner = Single.new(self, @events)
end
Puma.stats_object = @runner
@status = :run
end

View file

@ -57,6 +57,7 @@ class TestCLI < Minitest::Test
s << "GET /stats HTTP/1.0\r\n\r\n"
body = s.read
assert_equal '{ "backlog": 0, "running": 0 }', body.split(/\r?\n/).last
assert_equal '{ "backlog": 0, "running": 0 }', Puma.stats
cli.launcher.stop
t.join