yabeda-rb / yabeda-puma-plugin

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

Include GC.stat metrics for each puma process #18

Open liaden opened 3 years ago

liaden commented 3 years ago

Goal

For a preloaded puma application, I'd like to capture GC.stat with an index tag applied based on which puma child process (and some default value for the parent process).

Opening an issue to at least share my experience and approach incase it helps others.

Solution (within Rails app)

I defined a puma plugin:

Puma::Plugin.create do
  # 1. configure yabeda puma plugin
  # 2. override the index tag value that is done in the Yabeda.configure block
  # 3. spin up background thread to collect ruby process metrics for forked workers
  def start(launcher)
    launcher.config.configure do |puma_config|
      # add yabeda plugin (these couple of lines aren't that relevant)
      puma_config.activate_control_app
      puma_config.plugin :yabeda

      if launcher.options[:workers] > 0
        puma_config.on_worker_boot do |worker_index|
          Yabeda.default_tag :index, worker_index

          Thread.new do
            labels = {}

            loop do
              sleep 1

              Yabeda.thread_count.set(labels, Thread.list.select(&:alive?).size)

              GC.stat.each do |key, value|
                Yabeda.send("ruby_vm_stats_#{key}").set(labels, value)
              end
            end
          end
        end

        Yabeda.default_tag :index, -1
      else
        Yabeda.default_tag :index, 0
      end
    end
  end
end

Additionally, added a default_tag to the yabeda configure block.

For the stats to be available from the parent process, we are using the direct file storage as recommended here.

Alternative

Update https://github.com/ianks/yabeda-gc/ to handle this. It may be more suitable given things such as sidekiq swarm.