class PumaStatsLogger::Middleware

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/puma_stats_logger/middleware.rb, line 3
def initialize(app, options = {})
  @app = app
  @logger = options[:logger] || Logger.new($stdout)
end

Public Instance Methods

call(env) click to toggle source
# File lib/puma_stats_logger/middleware.rb, line 8
def call(env)
  status, headers, body = @app.call(env)
  log_puma_stats if puma_options
  [status, headers, body]
end

Private Instance Methods

log_puma_stats() click to toggle source
# File lib/puma_stats_logger/middleware.rb, line 27
def log_puma_stats
  stats = Socket.unix(puma_options[:control_url].gsub('unix://', '')) do |socket|
    socket.print("GET /stats?token=#{puma_options[:control_auth_token]} HTTP/1.0\r\n\r\n")
    socket.read
  end

  stats = JSON.parse(stats.split("\r\n").last)
  line = String.new.tap do |s|
    s << "source=#{ENV['DYNO']} " if ENV['DYNO']
    s << stats.map{|k,v| "measure#puma.#{k}=#{v}"}.join(' ')
  end

  @logger.info line
end
puma_options() click to toggle source
# File lib/puma_stats_logger/middleware.rb, line 16
def puma_options
  @puma_options ||= begin
    return nil unless File.exists?(puma_state_file)
    YAML.load_file(puma_state_file)['config'].options
  end
end
puma_state_file() click to toggle source
# File lib/puma_stats_logger/middleware.rb, line 23
def puma_state_file
  'tmp/puma.state'
end