Created
March 4, 2013 19:05
-
-
Save jpignata/5084567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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