Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Last active June 21, 2021 13:53
Show Gist options
  • Save LeZuse/022c501143a5bc4b51a601ad2a1b8abc to your computer and use it in GitHub Desktop.
Save LeZuse/022c501143a5bc4b51a601ad2a1b8abc to your computer and use it in GitHub Desktop.
Puma plugin for stats logging on Heroku
Puma::Plugin.create do
def production?
ENV.fetch('RACK_ENV', 'development') == 'production'
end
def log(msg)
if production?
Rails.logger.info msg
else
puts msg
end
end
def flush
Rails.logger.flush if production?
end
def start
in_background do
loop do
sleep ENV.fetch('PUMA_STATS_FREQUENCY', 60).to_i
begin
stats = JSON.parse Puma.stats, symbolize_names: true
if stats[:worker_status]
stats[:worker_status].each do |worker|
stat = worker[:last_status]
log "source=worker.#{worker[:pid]} sample#puma.backlog=#{stat[:backlog]} sample#puma.running=#{stat[:running]} sample#puma.pool_capacity=#{stat[:pool_capacity]}"
end
else
log "sample#puma.backlog=#{stats[:backlog]} sample#puma.running=#{stats[:running]} sample#puma.pool_capacity=#{stats[:pool_capacity]}"
end
db_pool = ActiveRecord::Base.connection_pool.stat
log "sample#db_pool.size=#{db_pool[:size]} sample#db_pool.connections=#{db_pool[:connections]} sample#db_pool.busy=#{db_pool[:busy]} sample#db_pool.dead=#{db_pool[:dead]} sample#db_pool.idle=#{db_pool[:idle]} sample#db_pool.waiting=#{db_pool[:waiting]}"
flush
rescue => e
Rollbar.error(e) if production?
log e
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment