Skip to content

Instantly share code, notes, and snippets.

@jpignata
Created March 4, 2013 19:05
Show Gist options
  • Save jpignata/5084567 to your computer and use it in GitHub Desktop.
Save jpignata/5084567 to your computer and use it in GitHub Desktop.
require "raindrops"
class UnicornConnectionMonitor
def initialize(app, options = {})
@app = app
@statsd = options.fetch(:statsd)
end
def call(env)
if linux? && unicorn?
Raindrops::Linux.tcp_listener_stats(tcp_listeners).each do |_, stats|
@statsd.measure("unicorn.connections.active", stats.active)
@statsd.measure("unicorn.connections.queued", stats.queued)
end
end
@app.call(env)
end
private
def tcp_listeners
@tcp_listeners ||= Unicorn.listener_names.grep(/\A.+:\d+\z/)
end
def linux?
@linux ||= defined?(Raindrops::Linux) &&
Raindrops::Linux.respond_to?(:tcp_listener_stats)
end
def unicorn?
@unicorn ||= defined?(Unicorn) && Unicorn.respond_to?(:listener_names)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment