Skip to content

Instantly share code, notes, and snippets.

@a2f0
Forked from LeZuse/stats_logger.rb
Created March 26, 2019 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a2f0/af9cfa209da71af62c9a7e668b96948f to your computer and use it in GitHub Desktop.
Save a2f0/af9cfa209da71af62c9a7e668b96948f 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