yabeda-rb / yabeda-puma-plugin

Collects Puma web-server metrics from puma control panel
MIT License
70 stars 17 forks source link

Metrics not responded when I run puma with --daemon option #13

Closed pohodnya closed 4 years ago

pohodnya commented 4 years ago

When I run puma with command

$ bundle exec puma -C config/puma.rb

Starting Yabeda Prometheus metrics exporter on http://0.0.0.0:9394/metrics
Puma starting in single mode...
* Version 4.3.5 (ruby 2.6.5-p114), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
* Starting control server on unix:///var/folders/8t/rx9yjl6d6wv6fw2tzj906k080000gn/T/puma-status-1604321423093-87080
Use Ctrl-C to stop

127.0.0.1 - - [02/Nov/2020:19:50:29 +0700] "GET /metrics HTTP/1.1" 200 - 0.0042

Then it's OK

$ curl localhost:9394/metrics
# TYPE puma_backlog gauge
# HELP puma_backlog Number of established but unaccepted connections in the backlog
puma_backlog{index="0"} 0.0
...

But When I run Puma with option --daemon

$ bundle exec puma -C config/puma.rb --daemon

Starting Yabeda Prometheus metrics exporter on http://0.0.0.0:9394/metrics
Puma starting in single mode...
* Version 4.3.5 (ruby 2.6.5-p114), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Daemonizing...

then /metrics not responded

a$ curl localhost:9394/metrics
^C
Envek commented 4 years ago

Thanks for reporting!

It seems weird from the first view, but it looks like that we are starting our built-in Prometheus exporter too early: before Puma process double-forks itself in order to daemonize. So we launching it in a “launcher” process which is being immediately shut down and in forked process Puma execution continues but our bind to tcp port is already lost.

So I propose a fix which effectively postpones exporter launch on after daemonization process, please try it right from #14.

To do so place following line into your Gemfile:

gem "yabeda-puma-plugin", git: "https://github.com/yabeda-rb/yabeda-puma-plugin", branch: "fix/support-daemon-mode" # https://github.com/yabeda-rb/yabeda-puma-plugin/pull/14

Also please note that daemonization was removed from Puma 5.x as today process self-daemonization is considered to be a bad practice and usage of process monitoring tools is recommended instead (systemd, etc). Read more at https://github.com/puma/puma/blob/5-0-stable/docs/deployment.md#should-i-daemonize

Envek commented 4 years ago

Released in 0.5.0

pohodnya commented 4 years ago

Thanks! It's working!