├── lib ├── queue-metrics │ ├── version.rb │ ├── l2met_formatter.rb │ ├── notify.rb │ ├── railtie.rb │ ├── queue_time.rb │ ├── app_time.rb │ └── queue_depth.rb └── rack-queue-metrics.rb ├── rack-queue-metrics.gemspec ├── LICENSE └── README.md /lib/queue-metrics/version.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module QueueMetrics 3 | VERSION = "2.0.0" 4 | end 5 | end -------------------------------------------------------------------------------- /lib/rack-queue-metrics.rb: -------------------------------------------------------------------------------- 1 | require 'queue-metrics/queue_time' 2 | require 'queue-metrics/queue_depth' 3 | require 'queue-metrics/app_time' 4 | require 'queue-metrics/railtie' if defined?(Rails) 5 | -------------------------------------------------------------------------------- /lib/queue-metrics/l2met_formatter.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module QueueMetrics 3 | class L2MetFormatter 4 | def call(serverity, datetime, progname, msg) 5 | "at=metric #{msg}\n" 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/queue-metrics/notify.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module QueueMetrics 3 | module Notify 4 | def should_notify? 5 | if defined?(ActiveSupport::Notifications) 6 | ActiveSupport::Notifications.notifier.listening?(@instrument_name) 7 | end 8 | end 9 | 10 | def notify(data) 11 | ActiveSupport::Notifications.instrument(@instrument_name, data) 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/queue-metrics/railtie.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module QueueMetrics 3 | class RackQueueRailtie < Rails::Railtie 4 | initializer "rack_queue_railtie.configure_rails_initialization" do |app| 5 | app.middleware.use Rack::QueueMetrics::QueueTime 6 | app.middleware.use Raindrops::Middleware 7 | app.middleware.use Rack::QueueMetrics::QueueDepth 8 | app.middleware.use Rack::QueueMetrics::AppTime 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /rack-queue-metrics.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "queue-metrics/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "rack-queue-metrics" 7 | s.version = Rack::QueueMetrics::VERSION 8 | s.authors = ["dominic (Dominic Dagradi)"] 9 | s.email = ["dominic@heroku.com"] 10 | s.homepage = "http://github.com/heroku/rack-queue-metrics" 11 | s.summary = %q{Measure queueing metrics for Rack apps} 12 | s.description = %q{Measure queueing metrics for Rack apps} 13 | 14 | s.files = `git ls-files`.split("\n") 15 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 16 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 17 | s.require_paths = ["lib"] 18 | 19 | s.add_dependency 'raindrops' 20 | end -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2014 Heroku 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/queue-metrics/queue_time.rb: -------------------------------------------------------------------------------- 1 | require 'logger' 2 | require 'queue-metrics/l2met_formatter' 3 | require 'queue-metrics/notify' 4 | 5 | module Rack 6 | module QueueMetrics 7 | class QueueTime 8 | include Notify 9 | 10 | def initialize(app, logger = nil) 11 | @app = app 12 | @instrument_name = "rack.queue-metrics.queue-time" 13 | @logger = logger 14 | if @logger.nil? 15 | @logger = ::Logger.new($stdout) 16 | @logger.formatter = L2MetFormatter.new 17 | end 18 | end 19 | 20 | def call(env) 21 | middleware_start = (Time.now.to_f * 1000.0).round 22 | request_start = (env["HTTP_X_REQUEST_START"] || 0).to_i 23 | request_id = env["HTTP_REQUEST_ID"] 24 | request_start_delta = nil 25 | report = "measure=#{@instrument_name} middleware_start=#{middleware_start}" 26 | if request_start > 0 27 | request_start_delta = middleware_start - request_start 28 | report << " request_start=#{request_start} request_start_delta=#{request_start_delta}" 29 | end 30 | report << " request_id=#{request_id}" if request_id 31 | @logger.info report 32 | 33 | notify(:middleware_start => middleware_start, :request_start => request_start, :request_start_delta => request_start_delta, :request_id => request_id) if should_notify? 34 | 35 | env["MIDDLEWARE_START"] = middleware_start 36 | 37 | @app.call(env) 38 | end 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/queue-metrics/app_time.rb: -------------------------------------------------------------------------------- 1 | require 'logger' 2 | require 'queue-metrics/l2met_formatter' 3 | require 'queue-metrics/notify' 4 | 5 | module Rack 6 | module QueueMetrics 7 | class AppTime 8 | include Notify 9 | 10 | def initialize(app, logger = nil) 11 | @app = app 12 | @instrument_name = "rack.queue-metrics.app-time" 13 | @logger = logger 14 | if @logger.nil? 15 | @logger = ::Logger.new($stdout) 16 | @logger.formatter = L2MetFormatter.new 17 | end 18 | end 19 | 20 | def call(env) 21 | app_start = (Time.now.to_f * 1000.0).round 22 | request_id = env["HTTP_REQUEST_ID"] 23 | middleware_start = (env["MIDDLEWARE_START"] || 0).to_i 24 | middleware_delta = nil 25 | report = "measure=#{@instrument_name}.start app_start=#{app_start}" 26 | if middleware_start > 0 27 | middleware_delta = app_start - middleware_start 28 | report << " middleware_delta=#{middleware_delta}" 29 | end 30 | report << " request_id=#{request_id}" if request_id 31 | @logger.info report 32 | 33 | status, headers, response = @app.call(env) 34 | 35 | app_end = (Time.now.to_f * 1000.0).round 36 | app_delta = app_end - app_start 37 | report = "measure=#{@instrument_name}.end app_end=#{app_end}" 38 | report << " app_delta=#{app_delta}" 39 | report << " request_id=#{request_id}" if request_id 40 | @logger.info report 41 | 42 | notify(:app_end => app_end, :app_start => app_start, :app_delta => app_delta, :middleware_delta => middleware_delta, :request_id => request_id) if should_notify? 43 | 44 | [status, headers, response] 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/queue-metrics/queue_depth.rb: -------------------------------------------------------------------------------- 1 | require 'logger' 2 | require 'queue-metrics/l2met_formatter' 3 | require 'queue-metrics/notify' 4 | 5 | module Rack 6 | module QueueMetrics 7 | class QueueDepth 8 | include Notify 9 | 10 | def initialize(app, logger = nil) 11 | @app = app 12 | @addr = getaddr 13 | @instrument_name = "rack.queue-metrics.queue-depth" 14 | @logger = logger 15 | if @logger.nil? 16 | @logger = ::Logger.new($stdout) 17 | @logger.formatter = L2MetFormatter.new 18 | end 19 | 20 | Thread.new {report(1)} 21 | end 22 | 23 | def call(env) 24 | return @app.call(env) unless ENV['PORT'] 25 | status, headers, body = @app.call(env) 26 | [status, headers, body] 27 | end 28 | 29 | private 30 | 31 | def getaddr 32 | addr = IPSocket.getaddress(Socket.gethostname).to_s 33 | addr += ':' + ENV['PORT'] if ENV['PORT'] 34 | addr 35 | rescue SocketError 36 | nil 37 | end 38 | 39 | def report(interval) 40 | loop do 41 | stats = raindrops_stats 42 | stats[:addr] = @addr 43 | notify(stats) if should_notify? 44 | @logger.info(["measure=#{@instrument_name}", 45 | "addr=#{@addr}", 46 | "queue_depth=#{stats[:requests][:queued]}"].join(' ')) 47 | sleep(interval) 48 | end 49 | end 50 | 51 | def raindrops_stats 52 | if defined? Raindrops::Linux.tcp_listener_stats 53 | stats = Raindrops::Linux.tcp_listener_stats([ '0.0.0.0:'+ENV['PORT'] ])['0.0.0.0:'+ENV['PORT']] 54 | return { :requests => { :active => stats.active, :queued => stats.queued }} 55 | else 56 | return { :requests => { :active => 0, :queued => 0 }} 57 | end 58 | end 59 | 60 | end 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rack Queue Metrics 2 | 3 | Report on queued connections in any Rack-based server using [Raindrops](http://raindrops.bogomips.org/) and the [Raindrops::Linux](http://raindrops.bogomips.org/Raindrops/Linux.html) features. 4 | 5 | ## Usage 6 | 7 | First, add `rack-queue-metrics` to your Gemfile: 8 | 9 | ``` 10 | gem 'rack-queue-metrics' 11 | ``` 12 | 13 | ### Rails 14 | 15 | You're done! If you want to instrument queue metrics beyond the default output, you can subscribe to the `rack.queue-metrics` notifcation in your Rails app. For example, to print queue information to your logs, add the following to `config/initializers/notifcations.rb: 16 | 17 | ``` 18 | # config/initializers/notifications.rb 19 | ActiveSupport::Notifications.subscribe(/rack.queue-metrics/) do |*args| 20 | event = ActiveSupport::Notifications::Event.new(*args) 21 | payload = event.payload 22 | 23 | addr = payload[:addr] 24 | active = payload[:requests][:active] 25 | queued = payload[:requests][:queued] 26 | queue_time = payload[:queue_time] 27 | 28 | puts "STATS addr=#{addr} active=#{active} queued=#{queued} queue_time=#{queue_time} " 29 | end 30 | ``` 31 | 32 | For more information, see the [ActiveSupport::Notification](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) docs. 33 | 34 | ### Sinatra/Rack Apps 35 | 36 | Include the `Raindrops` and `Rack::QueueMetrics` middleware in your application's config.ru: 37 | 38 | ``` 39 | # config.ru 40 | use Raindrops::Middleware 41 | use Rack::QueueMetrics::Middleware 42 | ``` 43 | 44 | ### Output 45 | 46 | With every request, `rack-queue-metrics` will output a log line with the the following format: 47 | 48 | ``` 49 | at=metric measure=rack.queue-metrics addr=10.10.10.90:5000 queue_time=0 queue_depth=0 50 | ``` 51 | 52 | ## Notifications 53 | 54 | The following information is sent in the notification payload: 55 | 56 | * `requests[:active]`: Number of requests currently being processed by the dyno at the start of the request 57 | * `requests[:queued]`: Number of requests waiting to be processed at the start of the request 58 | * `queue_time`: Amount of time the current request spent in the queue 59 | * `addr`: Address of the dyno processing the request 60 | --------------------------------------------------------------------------------