class Barnes::Instruments::PumaInstrument

Public Class Methods

new(sample_rate=nil) click to toggle source
# File lib/barnes/instruments/puma_instrument.rb, line 7
def initialize(sample_rate=nil)
  @debug = ENV["BARNES_DEBUG_PUMA_STATS"]
  @puma_has_stats = (defined?(::Puma) && ::Puma.respond_to?(:stats))
end

Public Instance Methods

instrument!(state, counters, gauges) click to toggle source
# File lib/barnes/instruments/puma_instrument.rb, line 32
def instrument!(state, counters, gauges)
  gauges['using.puma'] = 1

  stats = json_stats
  return if stats.empty?

  puts "Puma debug stats from barnes: #{stats}" if @debug

  pool_capacity = StatValue.new(stats, "pool_capacity").value
  max_threads   = StatValue.new(stats, "max_threads").value
  spawned       = StatValue.new(stats, "running").value

  gauges[:'pool.capacity']    = pool_capacity if pool_capacity
  gauges[:'threads.max']      = max_threads   if max_threads
  gauges[:'threads.spawned']  = spawned       if spawned
end
json_stats() click to toggle source
# File lib/barnes/instruments/puma_instrument.rb, line 22
def json_stats
  return {} unless @puma_has_stats
  MultiJson.load(::Puma.stats || "{}")

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