This commit is contained in:
rick 2011-12-02 16:29:33 -07:00
Коммит 18f143ddd0
6 изменённых файлов: 517 добавлений и 0 удалений

1
.gitignore поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
pkg

22
LICENSE Normal file
Просмотреть файл

@ -0,0 +1,22 @@
The MIT License
Copyright (c) GitHub, Inc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

13
README.md Normal file
Просмотреть файл

@ -0,0 +1,13 @@
# RackMonitor
Some tiny Rack apps for monitoring Rack apps in production.
* RackMonitor::RequestStatus - Adds a status URL for health checks.
* RackMonitor::RequestHostname - Shows which what code is running on
which node for a given request.
* RackMonitor::ProcessUtilization - Tracks how long Unicorns spend
processing requests. Optioanally sends metrics to a StatsD server.
This code has been extracted from GitHub.com and is used on
http://git.io currently.

141
Rakefile Normal file
Просмотреть файл

@ -0,0 +1,141 @@
require 'rubygems'
require 'rake'
require 'date'
#############################################################################
#
# Helper functions
#
#############################################################################
def name
@name ||= Dir['*.gemspec'].first.split('.').first
end
def version
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
end
def date
Date.today.to_s
end
def rubyforge_project
name
end
def gemspec_file
"#{name}.gemspec"
end
def gem_file
"#{name}-#{version}.gem"
end
def replace_header(head, header_name)
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
end
#############################################################################
#
# Standard tasks
#
#############################################################################
task :default => :test
if false
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end
else
task :test do
puts "haven't setup tests yet"
end
end
desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -r ./lib/#{name}.rb"
end
#############################################################################
#
# Custom tasks (add your own tasks here)
#
#############################################################################
#############################################################################
#
# Packaging tasks
#
#############################################################################
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
task :release => :build do
unless `git branch` =~ /^\* master$/
puts "You must be on the master branch to release!"
exit!
end
sh "git commit --allow-empty -a -m 'Release #{version}'"
sh "git tag v#{version}"
sh "git push origin master"
sh "git push origin v#{version}"
sh "gem push pkg/#{name}-#{version}.gem"
end
desc "Build #{gem_file} into the pkg directory"
task :build => :gemspec do
sh "mkdir -p pkg"
sh "gem build #{gemspec_file}"
sh "mv #{gem_file} pkg"
end
desc "Generate #{gemspec_file}"
task :gemspec => :validate do
# read spec file and split out manifest section
spec = File.read(gemspec_file)
head, manifest, tail = spec.split(" # = MANIFEST =\n")
# replace name version and date
replace_header(head, :name)
replace_header(head, :version)
replace_header(head, :date)
#comment this out if your rubyforge_project has a different name
replace_header(head, :rubyforge_project)
# determine file list from git ls-files
files = `git ls-files`.
split("\n").
sort.
reject { |file| file =~ /^\./ }.
reject { |file| file =~ /^(rdoc|pkg)/ }.
map { |file| " #{file}" }.
join("\n")
# piece file back together and write
manifest = " s.files = %w[\n#{files}\n ]\n"
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
File.open(gemspec_file, 'w') { |io| io.write(spec) }
puts "Updated #{gemspec_file}"
end
desc "Validate #{gemspec_file}"
task :validate do
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
unless libfiles.empty?
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
exit!
end
unless Dir['VERSION*'].empty?
puts "A `VERSION` file at root level violates Gem best practices."
exit!
end
end

279
lib/rack_monitor.rb Normal file
Просмотреть файл

@ -0,0 +1,279 @@
module RackMonitor
VERSION = "0.0.1"
# Simple middleware to add a quick status URL for tools like Nagios.
class RequestStatus
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
GET = 'GET'.freeze
PATH_INFO = 'PATH_INFO'.freeze
STATUS_PATH = '/status'
# Initializes the middleware.
#
# # Responds with "OK" on /status
# use RequestStatus, "OK"
#
# You can change what URL to look for:
#
# use RequestStatus, "OK", "/ping"
#
# You can also check internal systems and return something more informative.
#
# use RequestStatus, lambda {
# status = MyApp.status # A Hash of some live counters or something
# [200, {"Content-Type" => "application/json"}, status.to_json]
# }
#
# app - The next Rack app in the pipeline.
# callback_or_response - Either a Proc or a Rack response.
# status_path - Optional String path that returns the status.
# Default: "/status"
#
# Returns nothing.
def initialize(app, callback_or_response, status_path = nil)
@app = app
@status_path = (status_path || STATUS_PATH).freeze
@callback = callback_or_response
end
def call(env)
if env[REQUEST_METHOD] == GET
if env[PATH_INFO] == @status_path
if @callback.respond_to?(:call)
return @callback.call
else
return @callback
end
end
end
@app.call env
end
end
# Simple middleware that adds the current host name and current git SHA to
# the response headers. This can help diagnose problems by letting you
# know what code is running from what machine.
class RequestHostname
# Initializes the middlware.
#
# app - The next Rack app in the pipeline.
# options - Hash of options.
# :host - String hostname.
# :revision - String SHA that describes the version of code
# this process is running.
#
# Returns nothing.
def initialize(app, options = {})
@app = app
@host = options[:host] || `hostname -s`.chomp
@sha = options[:revision] || '<none>'
end
def call(env)
status, headers, body = @app.call(env)
headers['X-Node'] = @host
headers['X-Revision'] = @sha
[status, headers, body]
end
end
# Middleware that tracks the amount of time this process spends processing
# requests, as opposed to being idle waiting for a connection. Statistics
# are dumped to rack.errors every 5 minutes.
#
# NOTE This middleware is not thread safe. It should only be used when
# rack.multiprocess is true and rack.multithread is false.
class ProcessUtilization
# Initializes the middleware.
#
# app - The next Rack app in the pipeline.
# domain - The String domain name the app runs in.
# revision - The String SHA that describes the current version of code.
# options - Hash of options.
# :window - The Integer number of seconds before the horizon
# resets.
# :stats - Optional StatsD client.
# :hostname - Optional String hostname.
def initialize(app, domain, revision, options = {})
@app = app
@domain = domain
@revision = revision
@window = options[:window] || 100
@horizon = nil
@active_time = nil
@requests = nil
@total_requests = 0
@worker_number = nil
@track_gc = GC.respond_to?(:time)
if @stats = options[:stats]
@hostname = options[:hostname] || `hostname -s`.chomp
end
end
# the app's domain name - shown in proctitle
attr_accessor :domain
# the currently running git revision as a 7-sha
attr_accessor :revision
# time when we began sampling. this is reset every once in a while so
# averages don't skew over time.
attr_accessor :horizon
# total number of requests that have been processed by this worker since
# the horizon time.
attr_accessor :requests
# decimal number of seconds the worker has been active within a request
# since the horizon time.
attr_accessor :active_time
# total requests processed by this worker process since it started
attr_accessor :total_requests
# the unicorn worker number
attr_accessor :worker_number
# the amount of time since the horizon
def horizon_time
Time.now - horizon
end
# decimal number of seconds this process has been active since the horizon
# time. This is the inverse of the active time.
def idle_time
horizon_time - active_time
end
# percentage of time this process has been active since the horizon time.
def percentage_active
(active_time / horizon_time) * 100
end
# percentage of time this process has been idle since the horizon time.
def percentage_idle
(idle_time / horizon_time) * 100
end
# number of requests processed per second since the horizon
def requests_per_second
requests / horizon_time
end
# average response time since the horizon in milliseconds
def average_response_time
(active_time / requests.to_f) * 1000
end
# called exactly once before the first request is processed by a worker
def first_request
reset_horizon
record_worker_number
end
# resets the horizon and all dependent variables
def reset_horizon
@horizon = Time.now
@active_time = 0.0
@requests = 0
end
# extracts the worker number from the unicorn procline
def record_worker_number
if $0 =~ /^.* worker\[(\d+)\].*$/
@worker_number = $1.to_i
else
@worker_number = nil
end
end
# the generated procline
def procline
"unicorn %s[%s] worker[%02d]: %5d reqs, %4.1f req/s, %4dms avg, %5.1f%% util" % [
domain,
revision,
worker_number.to_i,
total_requests.to_i,
requests_per_second.to_f,
average_response_time.to_i,
percentage_active.to_f
]
end
# called immediately after a request to record statistics, update the
# procline, and dump information to the logfile
def record_request(status)
now = Time.now
diff = (now - @start)
@active_time += diff
@requests += 1
$0 = procline
if @stats
@stats.timing("unicorn.#{@hostname}.response_time", diff * 1000)
if suffix = status_suffix(status)
@stats.increment "unicorn.#{Gitio.host}.status_code.#{status_suffix(status)}"
end
if @track_gc && GC.time > 0
@stats.timing "unicorn.#{@hostname}.gc.time", GC.time / 1000
@stats.count "unicorn.#{@hostname}.gc.collections", GC.collections
end
end
reset_horizon if now - horizon > @window
rescue => boom
warn "ProcessUtilization#record_request failed: #{boom}"
end
def status_suffix(status)
suffix = case status.to_i
when 404 then :missing
when 422 then :invalid
when 503 then :node_down
when 500 then :error
end
end
# Body wrapper. Yields to the block when body is closed. This is used to
# signal when a response is fully finished processing.
class Body
def initialize(body, &block)
@body = body
@block = block
end
def each(&block)
@body.each(&block)
end
def close
@body.close if @body.respond_to?(:close)
@block.call
nil
end
end
# Rack entry point.
def call(env)
@start = Time.now
GC.clear_stats if @track_gc
@total_requests += 1
first_request if @total_requests == 1
env['process.request_start'] = @start.to_f
env['process.total_requests'] = total_requests
# newrelic X-Request-Start
env.delete('HTTP_X_REQUEST_START')
status, headers, body = @app.call(env)
body = Body.new(body) { record_request(status) }
[status, headers, body]
end
end
end

61
rack_monitor.gemspec Normal file
Просмотреть файл

@ -0,0 +1,61 @@
## This is the rakegem gemspec template. Make sure you read and understand
## all of the comments. Some sections require modification, and others can
## be deleted if you don't need them. Once you understand the contents of
## this file, feel free to delete any comments that begin with two hash marks.
## You can find comprehensive Gem::Specification documentation, at
## http://docs.rubygems.org/read/chapter/20
Gem::Specification.new do |s|
s.specification_version = 2 if s.respond_to? :specification_version=
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.rubygems_version = '1.3.5'
## Leave these as is they will be modified for you by the rake gemspec task.
## If your rubyforge_project name is different, then edit it and comment out
## the sub! line in the Rakefile
s.name = 'rack_monitor'
s.version = '0.0.1'
s.date = '2011-12-02'
s.rubyforge_project = 'rack_monitor'
## Make sure your summary is short. The description may be as long
## as you like.
s.summary = "Tools for monitoring Rack apps in production."
s.description = "Tools for monitoring Rack apps in production."
## List the primary authors. If there are a bunch of authors, it's probably
## better to set the email to an email list or something. If you don't have
## a custom homepage, consider using your GitHub URL or the like.
s.authors = ["Ryan Tomayko", "Rick Olson"]
s.email = 'technoweenie@gmail.com'
s.homepage = 'https://github.com/github/rack_monitor'
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
s.require_paths = %w[lib]
## List your runtime dependencies here. Runtime dependencies are those
## that are needed for an end user to actually USE your code.
#s.add_dependency('rack', "~> 1.2.6")
## List your development dependencies here. Development dependencies are
## those that are only needed during development
s.add_development_dependency('rack-test')
## Leave this section as-is. It will be automatically generated from the
## contents of your Git repository via the gemspec task. DO NOT REMOVE
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
# = MANIFEST =
s.files = %w[
LICENSE
README.md
Rakefile
lib/rack_monitor.rb
rack_monitor.gemspec
]
# = MANIFEST =
## Test files will be grabbed from the file list. Make sure the path glob
## matches what you actually use.
s.test_files = s.files.select { |path| path =~ /^test\/.*_test\.rb/ }
end