class Takwimu::Instruments::PumaBacklog

Public Class Methods

new(sample_rate=nil) click to toggle source
# File lib/takwimu/instruments/puma_backlog.rb, line 6
def initialize(sample_rate=nil)
end

Public Instance Methods

instrument!(state, counters, gauges, timers) click to toggle source

For single worker process use backlog directly for multiple workers sum backlog.

github.com/puma/puma/pull/1532

# File lib/takwimu/instruments/puma_backlog.rb, line 33
def instrument!(state, counters, gauges, timers)
  stats = self.json_stats
  return if stats.empty?

  backlog = stats["backlog"]
  if backlog.nil?
    backlog = stats["worker_status"].map do |worker_status|
      worker_status["last_status"]["backlog"]
    end.reduce(0, :+)
  end

  gauges[:'backlog.requests'] = backlog
end
json_stats() click to toggle source
# File lib/takwimu/instruments/puma_backlog.rb, line 19
def json_stats
  MultiJson.load(Puma.stats || "{}")

# Puma loader has not been initialized yet
rescue NoMethodError => e
  raise e unless e.message =~ /nil/
  raise e unless e.message =~ /stats/
  return {}
end
start!(state) click to toggle source
# File lib/takwimu/instruments/puma_backlog.rb, line 15
def start!(state)
  require 'multi_json'
end
valid?() click to toggle source
# File lib/takwimu/instruments/puma_backlog.rb, line 9
def valid?
  defined?(Puma) &&
    Puma.respond_to?(:stats) &&
    ENV["DYNO"] && ENV["DYNO"].start_with?("web")
end